Sun Position & Times Edge Driver

Hi everyone, I’m new to SmartThings. In my home I need to know the position of the sun to be able to open or close the blinds and dusk light automations. I wrote two DHs that determine the position of the sun as well as sun times (sunrise, sunset, etc.) and I am making it available in case anyone else feels the need for this type of automation. Constructive criticism and commentary are welcome. Best regards!

SunCalc (Parent DH)

The parent sensors are:

  • Contact Sensor: sunrise <= sun <= sunset (sun is up)
  • Motion Sensor: dawn <= sun <= dusk
  • Acceleration Sensor: night <= sun <= nightEnd
  • Infrared Level: dayNigthRatio

SunCalc Contact Sensor (Child DH)

Child Contact sensors are:

  • Sun Position (contact opens in the position, please refer to settings for ideal configuration for your home for these sensors):
    **SunNorth, SunSouth, SunEast, SunWest

Sun Time Interval (contact opens in the interval)

  • SunEarlyMorning: sunrise <= sun <= midmorning
  • SunLateMorning: midmorning <= sun <= noon
  • SunEarlyAfternoon: noon <= sun <= midafternoon
  • SunLateAfternoon: midafternoon <= sun <= sunset
  • SunEarlyNight: sunset <= sun <= nadir (solar midnight)
  • SunLateNight: nadir <= sun <= sunrise
2 Likes

Hello @Baranauskas,

Thank you for sharing!
For this use case, another option could be to create a SmartApp, so you wouldn’t need to create child devices and you can adapt your functions to get the times of sunset, sunrise, etc.
The Schedules endpoint would help you with the interval to refresh the Sun position.
Once you have the current Sun position, you can send commands directly to the devices included in the SmartApp configuration instead of creating extra automations.
Below you will find a sample that follows this workflow:

  1. The user selects the blinds and lights to control (the capability specifies what kind of devices can be selected)
  2. Configures the angle of north and incidence (similar to the settings in your DTH).
  3. Once installed, it gets the location information (latitude, longitude, etc.) and creates the schedule to refresh the Sun position in a 5-minutes interval.
  4. In the Schedule event handler, you could include the corresponding functions and then send a command to the device based on the Sun position.
const app = new SmartApp();
app.enableEventLogging()
.appId('Sun-time-calc')
.permissions(["r:devices:*","r:locations:*","x:devices:*"])
.page('mainPage', (context, page, configData) => {
    page.section('blinds', section => {
        section.deviceSetting('blindNorth')
            .name("Blind North")
            .capabilities(['doorControl'])
            .required(true);
        section.deviceSetting('blindSouth')
            .name("Blind South")
            .capabilities(['doorControl']).required(true);
    });
    page.section('lights', section => {
        section.deviceSetting('light1')
            .name("Set of lights 1")
            .description("Select devices")
            .capabilities(['switch'])
            .required(true).multiple(true);
        section.deviceSetting('light2')
            .name("Set of lights 2")
            .description("Select devices")
            .capabilities(['switch'])
            .required(true).multiple(true);
    });
    page.section('configAngles', section => {
        section.decimalSetting('angleNorth')
            .name("North Angle")
            .min(0).max(100).postMessage('º')
            .required(true);
        section.decimalSetting('angleIncidence')
            .name("Angle of Incidence")
            .min(00).max(100).postMessage('º')
            .required(true);
    });
})
.updated(async (context, updateData) => {
    //get Location info (latitude, longitude, timeZone, etc.)
    let locID=context.locationId;
    let locationInfo= await context.api.locations.get(locID)
    //Avoid duplicates by deleting all the schedules previously created 
    await context.api.schedules.delete();
    //Schedule config
    let scheduleName='refreshSunPosition';
    let intervalCronExpression='0/5 * * * ?';
    //Create the schedule to get Sun position
    return Promise.all([
        context.api.schedules.schedule(scheduleName,intervalCronExpression)
    ])
})
.scheduledEventHandler('refreshSunPosition', async (context)=>{
    //Function to refresh Sun position
    //...
    //Send commands to the device
    let northBlinds=context.config.blindNorth[0].deviceConfig.deviceId;
    command={
        component:"main",
        capability: 'doorControl',
        command:'close'
    }
    context.api.devices.executeCommand(northBlinds,command)
}) 

For more information:

  • In this document, you can see the different configurations you can add, for example, subscriptions to devices events, schedules, etc.
  • In the SmartThings Core SDK, you can find the different endpoints available for the SmartApp.
  • To start using SmartApps, take a look at this post:
1 Like

Thanks for sharing! I’ve tried suncalc implementation with different community and own smart apps to control my iblinds, but your implementation as a DH is the most elegant and flexible. I’ve implemented logic of closing/opening the blinds depending on sun altitude and azimuth in webcore within minutes and it works like a charm. Thanks a lot! Don’t see any flaws so far.

Sun Edge Drivers can be found in GitHub or ST Edge Channel or Channel Invitation

1 Like

@Baranauskas Any issues with the Sun v3.1? It was working for me until recently when the Sun Parts of the Day don’t calculate correctly. I am also using Sun Virtual Device and it is accurate.

1 Like

This looks very cool, but I am confused by the Terminology.

In a smartthings context,“Device Type Handlers” (DTHs) were written in Groovy and are part of the old architecture. They can no longer be added to an account.

“Edge Drivers” are written in lua and are part of the new architecture.

You appear to have written edge drivers, but describe them as device handlers? I think that might confuse quite a few people or am I misunderstanding what this is? :thinking:

Once you install the Edge Driver, it creates a device with the Sun information. I hope that helps as I’m new to this and not as familiar with the terminology you mention.

1 Like

Ok, I’ve updated the tags with the right terminology so this will show up on the current quick browse lists and more people will see it. :sunglasses:

https://thingsthataresmart.wiki/index.php?title=Quick_Browse_Lists_for_Edge_Drivers

In the future, refer to these as “drivers,” not “handlers,” so people will know they work with the current architecture. :tada:

1 Like

Thank you for the clarification! I’m learning this new architecture slowly but surely.

1 Like

Thank you so much for creating and sharing this. But I seem to be running into an issue when trying to set the location latitude in V3.1 The dialog disables the save button as soon as I enter the second number after the decimal. 34.06 specifically in my case. Same thing happens for the longitude 117.15

First of all, thanks for the driver, especially the home angle option.
It would be great to have the calculations bundled in an app or website where the user could enter geolocation, home angle from north, date and time. That way I could select the altitude and home azimuth to use in my routine based on measured angles.

Just installed V3.1 and I am having the same issue as @yashrg with only being able to populate the first decimal place. I doubt it affects accuracy that much however this hopefully can be fixed.

Also does the time offset automatically adjust to daylight saving changes?

@Baranauskas Has anyone noticed the Sun Parts of the Day not updating properly? It is part sunrise here and the percentage is still showing on night instead of updating to Morning. I didnt have any issues with it before but the last few days, it has not updated correctly.