[SOLVED] The last rule I need but can't at a time to run

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

Can someone help??

Ben

Try this in your rule.

rule "LightSwitch"  
    when
        Item LivingRoomLightTimer changed
    then 
        if (LivingRoomLightTimer.state == ON) && (now.getHourOfDay() >= 22) && (now.getHourOfDay() <= 6){
            GF_LivingDining_Light.sendCommand(ON)
        } else {
            GF_LivingDining_Light.sendCommand(OFF)
        }
end

HI H102

Just tried that and I’m getting this error:

LightTimer.rules’ has errors, therefore ignoring it: [5,47]: no viable alternative at input ‘&&’

[7,11]: mismatched input ‘else’ expecting ‘end’

Have no idea what that means :frowning:

Ben

Like this:

rule "LightSwitch"  
    when
        Item LivingRoomLightTimer changed
    then 
        if ((LivingRoomLightTimer.state == ON) && (now.getHourOfDay() >= 22) && (now.getHourOfDay() <= 6)) {
            GF_LivingDining_Light.sendCommand(ON)
        } else {
            GF_LivingDining_Light.sendCommand(OFF)
        }
end

Yep, I forgot to add the extra ( ) on the if statement.:roll_eyes:

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.

Ben

Add an extra to the if statement at the start and one on the end like this:

rule "LightSwitch"  
    when
        Item LivingRoomLightTimer changed
    then 
        if ((LivingRoomLightTimer.state == ON) && (now.getHourOfDay() >= 22) && (now.getHourOfDay() <= 6)) {
            GF_LivingDining_Light.sendCommand(ON)
        } else {
            GF_LivingDining_Light.sendCommand(OFF)
        }
end 

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

I shouldn’t have copy/pasted @H102’s example :rofl:

1 Like

@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.:grinning:

@lostcontrol THat rule has worked thank you :smiley:

@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).

Much appreciated
Ben

@Dreameruk you’re welcome.

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.:sunglasses:

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 I was just using PuTTY. VSCode looks like something that would definitely help me.

Thank you.

Here’s a teaser.:wink:

@H102 That does look interesting. I’ll have a play :slight_smile:

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.

Hope that helps everyone.

Many thanks
Ben

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

I’ve just looked at the logs and I was getting an error with my ‘change’

2018-11-16 17:37:24.774 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘LightSwitchON’: The argument ‘command’ must not be null.

I have now removed the val state = as you recommended and that error has now gone.

Thank you.

The more I learn about OpenHab the more I realise I don’t know anything :sweat:

I see @Udo_Hartmann is currently replying, he’s very good with rules, prepare to learn.:grin: