Sunrise/Sunset Rule

Can you help me out and point it out to me. I just can’t seem to see it.

Double check the Channel name

Another example:

rule "Set to day"
  Channel "astro:sun:local:civilDawn#event" triggered START

rule "Set to night"
  Channel "astro:sun:local:nauticDusk#event" triggered START

Thanks for the help, I eventually found it, but it was very hard to see. But why did you both not just tell me there was a “t” at the end if dusk instead of making me feel like a complete idiot? There is a reason we ask others to proof read stuff, and that is precisely because small errors like this are actually impossible to see (there is tons of psych research on this) and I did end my original post saying it was probably something simple.

Anyways, I will see at dusk tonight if this works, if it does, then I’ll do the proxy item, since that does make sense and grants a fair bit of flexibility down the road.

You will probably want to also add a rule to the System started event to set the dummy item (or zwave switch) to the proper value when openHab starts. Something along the lines of:

rule SystemStartUp
when System started  
	 switch now {
   	   case now.isAfter(Datetime(Sunrise.state.toString)) && now.isBefore(Datetime(Sunset.state.toString)):isNight.postUpdate(OFF)
	   default: isNight.postUpdate(ON)

My Items are:

Switch      isNight            "Night"      
DateTime    Sunrise            "Sunrise"     {channel="astro:sun:local:rise#start"}
DateTime    Sunset             "Sunset"      {channel="astro:sun:local:set#end"} 


That’s pretty slick. I think I’ll steal that from you. Thanks!

Is Datetime() a built in defined function, or does that need to be defined elsewhere? VSCode is throwing out an “Undefined method” error for it.

I’m new to openHab, but I believe it is built in a Java function converting the Sunset item’s state to a Datetime variable. I’m assuming that you have that entire line of code on one line. I’ve occasionally found that closing VSCode and restarting openHab sometime helps.

I also noticed that i left the “end” off at the end of the rule, I’ve updated the original post.

Try this rule instead

rule SystemStartUp
when System started  
    var vSunrise = new DateTime(Sunrise.state.toString)
	var vSunset  = new DateTime(Sunset.state.toString)
	switch now {
	  case now.isAfter(vSunrise) && now.isBefore(vSunset): isNight.postUpdate(OFF)
	  default: isNight.postUpdate(ON)

It was much happier with that. Thanks.

It still does not work.

I create two Items Sunrise and Sunset. I linked sunrise to Civil Dawn Start and I linked sunset to Civil Dusk End. When I go to the control section I see these there, but only Sunset has a value (which looks right), Sunrise shows a - I used @JimH rule from above to get a value into these Items

I fixed the typo, and now the rule looks right (same as I posted above), but the light did not turn on at Sunset. The log shows me nothing of use.

Where do I got to work out if all these pieces are working? Is there another I need to look at to help me trouble shoot rules or the Astro binding?

I have spent and hour googling this to no avail, I read the entire wiki page on the Astro binding and found nothing there that could help.

You have to be very careful with channel linking with Astro binding, it is easy to get it wrong.
Astro channels come in two “flavours”, state channels like
and trigger event channels like
You cannot mix which types link to Items and which trigger rules.

Okay, that looks like a simple one to tackle.
Start by double checking the Item definition and channel.
Note this Item is not populated from rule, it is populated by binding channel.
Binding calculates values at midnight and startup time, so if you clear or create an Item inbetween it won’t get repopulated.

Makes sense, the items bound to the state channel both have data now. So that appears to be working.

I am not too sure what you mean. My rule for turning on the light uses the trigger event channel, but the rule I created (separate rule/file) uses the state channel, which is linked to my Sunrise and Sunset *Items. The rule that is not working, has nothing to do with the Sunrise/Sunset Items

This is what it looks like now

 rule "Sunset Rule"
    Channel 'astro:sun:local:civilDusk#event' triggered START
    // Do what you want to do at start of sunset for example:
    // Outside_Lights.sendCommand(ON)

rule "Sunrise Rule"
    Channel 'astro:sun:local:civilDawn#event' triggered START
    // Do what you want to do at start of sunset
    // Outside_Lights.sendCommand(ON)

I am at a loss as to why it is not working.

That’s good. It really isn’t clear to many.

What is the state of play now? If neither rule works, that would be two separate not-workings.
How are you determining not-working, lack of commands in events.log I suppose.
You already know to look for the events in events.log, what’s there now?
It takes half a day for the events to roll around, obviously.

I am hesitant to recommend this as it seems like the suggested cure all for all things evil in openHAB without any reason, but it is possible that if you stop, clear the cache, and restart you may have better luck. I’ve personally seen some odd things using the astro binding that were fixed with that. Ignore this if you’ve already tried it.

I actually have no idea if @JimH’s rule works, but I don’t really care about it (yet). It;s the rule that I started with on this thread that does not work, and I care about getting to work.

That and the fact that the light is not on after Civil Sunset (19h45).

This rule has been in place for 3 days now, and has not worked on any of those days.

Okay, at least one rule wasn’t going to work due to typo until more recently. Don’t despair :smiley:

I just had a look at my own event triggered sunrise/sunset rules.

    rule "sunrise"
        Channel 'astro:sun:thissite:rise#event' triggered START

No surprises there. Why not set up another pair of rules on simple sun rise/set events, just in case something screwy about civilDawn (you’ve seen civilDusk works fine).

Hi there,

I don’t know if that solves your problem, but I’m using very successfully the time of day pattern

So this randomly started working this week. I was waiting to get some time to dive into it and then I noticed the light turned at sunset. I don’t know what changed, and I really don’t like it when things like this sort fix themselves. There it is.

Thanks everyone for the help! I am sure as go deeper down this rabbit hole you will see me on here again :smiley:

Hi Lutiana,

just in the case you want a more customizable solution and if you want to modify your switching times over the UI, you can try this solution: