Ok, I am trying to create a rule on which certain things happen. Ill try to be as clear as possible.
In the evening when the sun sets I want the lights to turn on, and go off at 11pm.
In the morning at 7am the lights need to go on, and go off when the sun rises.
But the lights only need to go on when it is actually “dark” dark in this case means that in the morning the lights should go on at 7am but only if the sun hasn’t rised yet.
And in the evening the light need to go on at sunset, but need to go off at 11pm…
My rules file contains this:
rule "Lights On"
when
// Morning Lights On :
Time cron "0 0 7 ? * * *" // Go on at 7am
// ## and sunrise has not happend yet ##
// Evening Lights On :
Item Sunset_Event received update ON // Sunset happend
then
wcd001_schakelaar.sendCommand(ON) // Put lights on
end
rule "Lights Off"
when
// Morning Lights Off :
Item Sunrise_Event received update ON // Sunrise happend
// Evening Lights Off:
Time cron "0 0 23 ? * * *" // go off at 11pm
then
wcd001_schakelaar.sendCommand(OFF) // Put lights off
end
But I commented out a piece in which I don’t understand what to do with. It is in the Lights on rule at the morning part: // ## and sunrise has not happend yet ##
I’m using the Astro “Local Sun Phase Name” for this - Basically this is stepping through the following values: SUN_RISE, ASTRO_DAWN, NAUTIC_DAWN, CIVIL_DAWN, CIVIL_DUSK, NAUTIC_DUSK, ASTRO_DUSK, SUN_SET, DAYLIGHT, NIGHT
My rule then has something like the following:
if (astro_sun_local_phase_name.state != "DAYLIGHT" ) {
// switch your light on
}
I meant to say i’m using that instead of your Sunset_Event. My rule does something else, but here it is:
rule "Randomly turn on/off lights"
when
Time cron "0 */30 5-22 ? * MON-FRI *" or
Time cron "0 */30 8-00 ? * SAT *" or
Time cron "0 */30 8-22 ? * SUN *"
then
if (astro_sun_local_phase_name.state != "DAYLIGHT" ) {
gRandomLights.members.forEach[lamp|
var int randomTime = (new java.util.Random).nextInt(10)
val stateNow = lamp.state
val stateNew = if(Math::random > 0.6) OFF else ON
if( stateNew != stateNow) {
logInfo("org.openhab","Switching light " + lamp.name + " from " + stateNow
+ " to " + stateNew + " in " + randomTime + " Minutes")
createTimer( now.plusMinutes( randomTime )) [|
lamp.sendCommand( stateNew.toString )
]
}
]
}
end
So WHEN these cron statements come true, IF it is not daylight, it randomly turns some lights on and off. You could do the same for your purpose: WHEN it is 7pm: IF it is not daylight, turn on the light. You just need to replace my cron statements with yours and the statement in the IF with your sendcommand.
I think what you want to achieve can’t be done easily in the “when” section of a rule as there is no “and”, at least i’m not aware of it.
i’m planning on working on this as well this week.
check the astro binding (provides flexible times in the day that change with the seasons) and setup the channels+items you need. There shouldn’t be much more needed. if i find any weird stuff I will write, but i suspect it’s goiung to be as simple as Max described.
I tried to wrap my head around that and came with the following rules configuration:
rule "lights - Morning - On"
when
Time cron "0 0 7 ? * * *"
then
if (astro_sun_local_phase_name.state != "DAYLIGHT" )
wcd001_schakelaar.sendCommand(OFF) else wcd001_schakelaar.sendCommand(ON)
end
rule "lights - Morning - Off"
when
Channel 'astro:sun:thuis:rise#event' triggered START
then
wcd001_schakelaar.sendCommand(OFF)
end
rule "lights - Evening - On"
when
Channel 'astro:sun:thuis:set#event' triggered START
then
wcd001_schakelaar.sendCommand(ON)
end
rule "lights - Evening - Off"
when
Time cron "0 0 23 ? * * *"
then
wcd001_schakelaar.sendCommand(OFF)
end
It might be a bit lengthy this way and may be improved or shortened somehow. But openHAB accepts the rules this way.
As far as I can see this should work as intended?
In the morning at 7am the light should turn on IF the sun hasn’t come up yet and stay off when the sun is up already.
And when on it should turn off when the sun comes up.
In the evening when the sun goes down the light should turn on IF the sun is down and stay off if the sun is still up.
And when on it should turn off turn the light at 11pm.
I have read the astro-binding multiple times but it is a bit overwhelming. But thanks for the tip.
Good luck with writing your own rules haha. It’s fun to do but sometimes quite frustrating and overwhelming.
I can confirm that the configuration from above works with the sunset, Ill inform tomorrow if all other stuff works. @Max1968, thank you for your help. You got me on the right track! THANK YOU
Error during the execution of rule 'lights - Morning - On': The name 'astro_sun_local_phase_name' cannot be resolved to an item or type; line 10, column 8, length 26
So that tells me that I do not have a thing and item configured for it. Which is true. But I cannot find an example file for it. Can anybody help? The astro binding is not clear to me in this case.
The morning on rule is like so:
rule "lights - Morning - On"
when
Time cron "0 0 7 ? * * *"
then
if (astro_sun_local_phase_name.state != "DAYLIGHT" )
wcd001_schakelaar.sendCommand(OFF) else wcd001_schakelaar.sendCommand(ON)
end
Just “link” that channel in PaperUI in case it is not linked automatically. Go to “Things”,type “astro” in the search box and select your “Local Sun” Thing. Then you will see most of the channels related to that thing. At the very bottom you should find the “Sun Phase Name” Channel, just click on the circle on the left to link/unlink it to an item. Using the button with the two arrows on the right you can also see what item is linked to this channel, see below.
Depending on if you are using “auto link” or something like this it might work a bit different than this, but this should give you a start.
Yeah, that’s the point, I don’t really use the paper UI. But I managed to link it in the configuration files. I think it will work now. But we’ll see tomorrow morning.