Hello.
The reason why I made this topic, is because I can’t find the answer for my question.
Neither in the link you have sent, and I have seen the docs before.
Sorry we have to wait until more knowledgeable people join
But here a few pointers: id you get any errors in the editor VSC? did you et errors saving the file?
Try some other times (like every 10 seconds or so) add some logInfo statements to see when your rule triggers, and to log out all times, etc…
You can also just adapt the Day of Time pattern that I linked, it works great, I am using it for years.
The time of day pattern mentioned above will do what you want, but if you want a smaller starting point, you can set up two rules, one with a trigger of:
In the upper rule, change an item to indicate day time, something like dayTime.sendCommand(ON). In the lower rule, turn dayTime off: dayTime.sendCommand(OFF). Now the Item dayTime will indicate day or night. Use the state of the item along with your cron rule to decide to turn the light on or not.
Change the astro line appropriately, might be ‘local’ instead of ‘home’ in your case.
I find this easiest. It is also possible to do time comparison since the astro binding does indeed give you the sunrise time, but I find that more trouble than just triggering an item on and off at sunrise and sunset. You can see examples of this in the time of day pattern that was mentioned.
No. because you’re looking at an event, which exists for only a moment in time.
The design pattern shows how to use astro events to control a state item like “dark outside” on or off.
Then you can make a rule trigger at 07:00 that looks to see, “is it dark? Lights on”
Another rule to run at sunrise, “Are lights on? turn them off”
What you basically need to do, is to make two rules: 1 that turns on the light when it is 7 AM for example, but only if the current time is before the time at which the sun gets up (for which I use the civil dawn end, but you are free to change that of course)
Secondly I turn off the light when the sun gets up in a second rule.
rule "turn on lights if still dark"
when
Time cron "0 0 7 ? * MON-FRI *"
then
if (now.isBefore((astro_sun_local_civilDawn_start.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli)){
LightSwitch.sendCommand(ON)
}
end
rule "Turn off light when it gets light"
when
Channel "astro:sun:local:rise#event" triggered END
then
LightSwitch.sendCommand(OFF)
end
That was a nice and simple solution.
I wrote it in in VSC, and no errors accured, so I will see on Monday, if it works.
I did alter it a bit, so it fits my configuration.
Thank you.
rule “Tænd indkørsel om morgenen hvis der er mørkt”
when
Time cron “0 45 6 ? * MON-FRI *”
then
if (now.isBefore((astro_sun_local_rise_start.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli)){
sendCommand(Indkorsel, 25)
}
end
rule “Sluk indkørsel om morgenen”
when
Time cron “0 00 8 ? * MON-FRI *”
then
sendCommand(Indkorsel, 0)
end
You’re welcome. Please mark my answer as solution to help others find it easier.
As a general remark, it is preferred to use the method item.sendCommand(ON) rather than the function sendCommand(item, ON).
Also corrected the Command tags as described, and put in an OR for turning off the light
Thank you for the solution, I knew it could be done
rule "Tænd indkørsel om morgenen hvis der er mørkt"
when
Time cron "0 45 6 ? * MON-FRI *"
then
if (now.isBefore((astro_sun_local_rise_start.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli)){
Indkorsel.sendCommand(25)
}
end
rule "Sluk indkørsel om morgenen"
when
Time cron "0 00 8 ? * MON-FRI *"
or
Channel 'astro:sun:local:rise#event' triggered END
then
Indkorsel.sendCommand(0)
end
That’s not a magic phrase, by the way. It’s an Item that you need to somehow fill with the required time value.
You need to create an Item by that name, and connect it to an astro channel (hint: very similar channel name!), to get the astro binding to place its calculated value in there.
I apologize if this post looks odd, this is my first post
Hi,
Have spend evenings trying to figure out why above doesn’t work for me.
Turning to the forum, and hope someone can help me further.
I’m running Openhabian 2.4
My code is identical to the one Michael Pedersen added above.
timer.rules
rule "Tænd spot facade når det er mørkt"
when
Time cron "0 45 06 ? * MON-FRI *"
then
if (now.isBefore(("astro_sun_local_rise_start.state" as DateTimeType).getZonedDateTime.toInstant.toEpochMilli)){
Spot_Facade.sendCommand(ON)
Spot_Terrasse.sendCommand(ON)
}else{
Spot_Facade.sendCommand(OFF)
Spot_Terrasse.sendCommand(OFF)
}
end
And the log return this error:
2019-11-28 20:50:27.408 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'timer.rules', using it anyway:
Cannot cast from String to DateTimeType
2019-11-28 20:50:27.508 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'timer.rules'
==> /var/log/openhab2/events.log <==
2019-11-28 20:50:33.505 [vent.ItemStateChangedEvent] - mpower_socket_24_A4_3C_D7_4F_F3_1_power changed from 16.7 to 17.3
2019-11-28 20:50:34.853 [vent.ItemStateChangedEvent] - CurrentDate changed from 2019-11-28T20:49:34.792+0100 to 2019-11-28T20:50:34.800+0100
2019-11-28 20:50:34.862 [vent.ItemStateChangedEvent] - Sunset_Time changed from 2019-11-28T20:49:34.792+0100 to 2019-11-28T20:50:34.800+0100
2019-11-28 20:50:34.867 [vent.ItemStateChangedEvent] - Weather_LastUpdate changed from 2019-11-28T20:49:34.792+0100 to 2019-11-28T20:50:34.800+0100
2019-11-28 20:50:34.872 [vent.ItemStateChangedEvent] - Sunrise_Time changed from 2019-11-28T20:49:34.792+0100 to 2019-11-28T20:50:34.800+0100
Thanks for the fast reply - that actually made sense.
Now I’m back with the other error:
2019-11-28 22:44:00.635 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Tænd spot facade når det er mørkt': The name 'astro_sun_local_rise_start' cannot be resolved to an item or type; line 6, column 21, length 26
It’s self explanatory. You do not have an Item named astro_sun_local_rise_start
Have you installed Astro binding, do you have Astro Thing(s), have you linked any Items to channels of those Things, what are the Items named?