Sun Position & Times DH

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
1 Like

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: