Sun state fail randomally


I have variable declared in the rules, and then changed during cron or Astro
In the rules I look for the variable “sun = true” to decide if I should turn lights on or not.

Sometimes, too much, the state is wrong, even during morning the lights turn on becuase the “sun” is true

How can I enhance this sun state ?

/ Vars
var boolean sun = false

// Sun

rule "Sunset var"
                Channel 'astro:sun:home:set#event' triggered START 
                Time cron "0 0 19 ? * *"
                logInfo("home.rules", "Sun False") 
                sun = false

rule "Sunrise var"
                Channel 'astro:sun:home:rise#event' triggered START 
                Time cron "0 0 6 ? * *"
                logInfo("home.rules", "Sun True") 
                sun = true

I remember doing something like this. Sort of the opposite but likely could be adapted.

Note that this approach requires that your .rules file not be touched or reloaded after the rise event or 06:00. When your file reloads sun gets reset to false. A better approach would be to use a Switch Item and implement restoreOnStartup on that Item so no matter when the .rules file reloads, it will retain the correct sun state.

If all you care about is whether the sun is up, Link an Item to the Elevation or Azimuth Channels and check to see if these are > 0 and you can do away with these Rules entirely.

When you are ready to have more times of day, see Design Pattern: Time Of Day.


@rlkoshak Dammit, that’s probably the reason I have problem, Never taught about that it reloads everytime. Thanks.

I’ll check the Elevation one, Azimuth maybe too much