OK this is the last rule I need to make my house totally automated.
The rule works perfectly as a stand alone but I need to to run between 10pm and 6am
I’ve tried to add a time but on each save I get an error. I’m not sure if I add the time in the ‘when’ section or the ‘if’ section.
Side note, I’m hopeless when it comes to coding.
rule "LightSwitch"
when
Item LivingRoomLightTimer changed
then
if (LivingRoomLightTimer.state == ON){
GF_LivingDining_Light.sendCommand(ON)
} else {
GF_LivingDining_Light.sendCommand(OFF)
}
end
And since one should avoid copy/paste as much as possible in code, you could try that (not tested):
rule "LightSwitch"
when
Item LivingRoomLightTimer changed
then
val hour = now.getHourOfDay
val state = if (LivingRoomLightTimer.state == ON && hour >= 22 && hour <= 6) ON else OFF
GF_LivingDining_Light.sendCommand(state)
end
@H102 Thanks for the reply can you tell me where I need to add the ()?
Thank you
@lostcontrol I tried your suggestion, changed the time to 13 just to activate it. It didn’t error but the light doesn’t come on when the sensor is triggered.
But thank you both for your time. Much appreciated.
You can test it with this to simulate that it’s 23:00 (aka 11pm):
rule "LightSwitch"
when
Item LivingRoomLightTimer changed
then
// val hour = now.getHourOfDay
val hour = 23
val state = if (LivingRoomLightTimer.state == ON && hour >= 22 && hour <= 6) ON else OFF
GF_LivingDining_Light.sendCommand(state)
end
Oh wait but that boolean thing is wrong, it should be like this:
rule "LightSwitch"
when
Item LivingRoomLightTimer changed
then
val hour = now.getHourOfDay
val state = if (LivingRoomLightTimer.state == ON && (hour >= 22 || hour <= 6)) ON else OFF
GF_LivingDining_Light.sendCommand(state)
end
@Dreameruk both rules posted should work and for the sake of learning try them both. If your interested in learning more about rules take a look at this site. https://www.w3schools.com/js/js_if_else.asp This will help explain the some of the conditional statement with examples. Also look on the left and select some of the other tutorial items. I use this often to help write rules in OH, hope you find it useful as well.
@H102 Thank you for your time and help. I will follow the link and have a read. It all looks way too complicated but I’m sure I’ll eventually work it out.
My house is now fully automated (well it will be when I add the other lights to the rule).
I thought the same when I first started writing rules. The key is to understand what each line of code does. After that, things will start making sense and you’ll be helping others.
If your not using VSCode with OH extension you might want to take a look at installing it. It’s great for editing all files in OH and especially writing rules. Has a neat feature that will show all the available options for your rules.
@H102 That does look interesting. I’ll have a play
Just for clarity if someone finds this post and needs similar help.
@lostcontrol code worked but I needed to change it slightly. (still a work of genius).
If the lights were already on, after the motion sensor was activated, the lights turned off. I think it was the ‘else OFF’ part of the rule would run no matter what time it was.
I’ve changed it to this and it seems to work fine now. The lights are on and I’ve walked past the sensor and they’ve stayed on.
rule "LightSwitchON"
when
Item LivingRoomLightTimer changed
then
val hour = now.getHourOfDay
val state = if (LivingRoomLightTimer.state == ON && (hour >= 22 || hour <= 6)) ON
GF_Hallway_Light.sendCommand(state)
GF_LivingRoom_Light.sendCommand(state)
GF_LivingDining_Light.sendCommand(state)
GF_Dining_Light.sendCommand(state)
FF_Hallway_Light.sendCommand(state)
end
rule "LightSwitchOFF"
when
Item LivingRoomLightTimer changed
then
val hour = now.getHourOfDay
val state = if (LivingRoomLightTimer.state == OFF && (hour >= 22 || hour <= 6)) OFF
GF_Hallway_Light.sendCommand(state)
GF_LivingRoom_Light.sendCommand(state)
GF_LivingDining_Light.sendCommand(state)
GF_Dining_Light.sendCommand(state)
FF_Hallway_Light.sendCommand(state)
end
Basically I’ve made two rules. One to turn them on and one to turn them off. But only if the time is after 10pm and before 6am.
Just for learning sake you can eliminate the val state = and use the ON/OFF as the command to send when the statement is true, see below.
rule "LightSwitchON"
when
Item LivingRoomLightTimer changed
then
val hour = now.getHourOfDay
if (LivingRoomLightTimer.state == ON && (hour >= 22 || hour <= 6)){
GF_Hallway_Light.sendCommand(ON)
GF_LivingRoom_Light.sendCommand(ON)
GF_LivingDining_Light.sendCommand(ON)
GF_Dining_Light.sendCommand(ON)
FF_Hallway_Light.sendCommand(ON)
}
end
rule "LightSwitchOFF"
when
Item LivingRoomLightTimer changed
then
val hour = now.getHourOfDay
if (LivingRoomLightTimer.state == OFF && (hour >= 22 || hour <= 6)){
GF_Hallway_Light.sendCommand(OFF)
GF_LivingRoom_Light.sendCommand(OFF)
GF_LivingDining_Light.sendCommand(OFF)
GF_Dining_Light.sendCommand(OFF)
FF_Hallway_Light.sendCommand(OFF)
}
end
Also for learning, why did the code below not work(as expected)? Anything in the logs that might give a hint? If not then look at your modification to the code and notice the change in the if statement.
rule "LightSwitch"
when
Item LivingRoomLightTimer changed
then
val hour = now.getHourOfDay
val state = if (LivingRoomLightTimer.state == ON && (hour >= 22 || hour <= 6)) ON else OFF
GF_LivingDining_Light.sendCommand(state)
end