[SOLVED] Morning Routine: Rule with 2 conditions is not working consistently

Hi guys,

fairly new to the openhab setup but so far I was able to implement nearly all of my devices at home into openhab in a short time. I am playing around with rules now and I have a problem setting up my morning routine.

I have a PIR sensor connected to my raspberry pi with openhab which sits in my lobby.
What I want to implement is to trigger a morning routine once I get up in the morning and go from my bedroom to the lobby whereby triggering the PIR sensor with my movement which in turn starts the morning routine.

For the morning routine itself I wanted to implement two rules:

  1. Only let the morning routine get triggered once, then do not permit an additional trigger (since I have sonos grouping and playing music, traffic alerts in the morning routine which only make sense to be triggered once)
  2. Limit the morning routine and the ability to get triggered independent from the first rule between 6’o clock and 9’o clock
rule "Morgen-Routine"
when 
		Item Passlobby received update OPEN
then 
        var Number hour = now.getHourOfDay
	var Number day = now.getDayOfWeek
        if(RunToday.state==1 && (hour >= 6 && hour <= 9)){
        logWarn(ruleName,"Morning Routine")
        Alexa_TTS.sendCommand('Guten Morgen Henning, ich starte deine Morgen-Routine')
        Thread::sleep(5000) 
        DenonInput.sendCommand('CD')
        AlexaTraffic.sendCommand("Traffic")
        gLobbyLights.sendCommand(ON)
        gLivingroomLights.sendCommand(ON)
        gOfficeLights.sendCommand(ON)
        gKitchenLights.sendCommand(ON)
        str_sonos_bad_add.sendCommand("RINCON_000E582AC07E01400")
        str_sonos_bad_add.sendCommand("RINCON_B8E9375835EC01400")
        str_sonos_bad_add.sendCommand("RINCON_000E582AC07601400")
        str_sonos_bad_add.sendCommand("RINCON_5CAAFD1670C201400")
        executeCommandLine("python /home/openhabian/addons/BlackBeanControl/BlackBeanControl.py -c NUBERTON -d RM-mini-office", 5000)
        Thread::sleep(10000) 
        pl_sonos_bad_control.sendCommand("PLAY")
        Thread::sleep(3000) 
        executeCommandLine("python /home/openhabian/addons/BlackBeanControl/BlackBeanControl.py -c NUBERTCHANOPTICAL -d RM-mini-office", 5000)
        // postUpdate(RunToday, 0) 
        createTimer(now.plusHours(3), [|postUpdate(RunToday, 1)])
        }
        else
        {
			logWarn(ruleName, "Morning Routine not starting because of timer constraints")
		}

        
end

However, after hours of digging in the forums for those rules and I can’t see any error, both rules dont seem to work consistently together. Alone they seem to work but not in combination.

Can somebody help me out here? Thanks in advance :slight_smile:
Henning

You need to provide more information. What specifically do you mean “don’t seem to work consistently together”? You’ve only posted one Rule but talk about two Rules. Where is the other one?

First, I recommend using proper indentation. When you have a { all the lines after that should be indented until the closing } so you can see what code is inside the if more easily.

Log running rules (anything more than 500 msec is considered long) are a bad idea. It is probably OK here but you should avoid Thread::sleep. This one takes 20 seconds to run, maybe more. I bet that Passlobby can receive an update OPEN more than once over that 20 seconds which will trigger the Rule to run again.

Use a Switch for RunToday, or use a global boolean variable instead of a Number which will be easier to understand.

Set RunToday as soon as possible to prevent the Rule from running the scene if it gets triggered again while an old one is still running.

Hi @rlkoshak

thanks for your reply, sorry maybe I put in the wrong way with 2 rules, in this case it is about the 2 conditions which are in the code:

Is there any other preferred way to put two conditions in here or is this the preferred way from your point?
A good hint from you that the script is long, this is due to the sonos grouping which takes time, I will use a global boolean and let you know the result.

You are right one of the main issues was that the rule keeps on firing although the timer is set.

I will have a look at it again.

Thank you.
Henning

So, I just tested it @rlkoshak.

With this code, where I did at least put the timer creation in front of the morning routine still does not prevent to keep the script from firing again and again. I did change the hours to outside of the required ones and this works so the problem must be the timer recognition and setting of it I guess.


rule "Morgen-Routine"
when 
		Item Passlobby received update OPEN
then 
        var Number hour = now.getHourOfDay
		var Number day = now.getDayOfWeek
        if(RunToday.state==1 && (hour >= 20 && hour <= 23)){
        // postUpdate(RunToday, 0) 
        createTimer(now.plusHours(3), [|postUpdate(RunToday, 0)])
        logWarn(ruleName,"Morning Routine")
        Alexa_TTS.sendCommand('Guten Morgen Henning, ich starte deine Morgen-Routine')
        Thread::sleep(5000) 
        DenonInput.sendCommand('CD')
        AlexaTraffic.sendCommand("Traffic")
        gLobbyLights.sendCommand(ON)
        gLivingroomLights.sendCommand(ON)
        gOfficeLights.sendCommand(ON)
        gKitchenLights.sendCommand(ON)
        str_sonos_bad_add.sendCommand("RINCON_000E582AC07E01400")
        str_sonos_bad_add.sendCommand("RINCON_B8E9375835EC01400")
        str_sonos_bad_add.sendCommand("RINCON_000E582AC07601400")
        str_sonos_bad_add.sendCommand("RINCON_5CAAFD1670C201400")
        executeCommandLine("python /home/openhabian/addons/BlackBeanControl/BlackBeanControl.py -c NUBERTON -d RM-mini-office", 5000)
        Thread::sleep(10000) 
        pl_sonos_bad_control.sendCommand("PLAY")
        Thread::sleep(3000) 
        executeCommandLine("python /home/openhabian/addons/BlackBeanControl/BlackBeanControl.py -c NUBERTCHANOPTICAL -d RM-mini-office", 5000)
        }
        else 
        {
			logWarn(ruleName, "Morning Routine not starting because of timer constraints")
		}

        
end

You never update RunToday to 1.

Oh, one other thing: https://www.openhab.org/docs/configuration/rules-dsl.html#myitem-sendcommand-new-state-versus-sendcommand-myitem-new-state

There is nothing wrong with your if statement. Though it might be worth while looking at Design Pattern: Time Of Day.

Hi @rlkoshak,

thanks for the hint, it is now working as expected with this code by updating RunToday to 1 and putting it in front of the execution of the routine, I will now work on proper indentation :slight_smile:

rule "Morgen-Routine"
when 
		Item Passlobby received update OPEN
then 
        var Number hour = now.getHourOfDay
		var Number day = now.getDayOfWeek
        if(RunToday.state==0 && (hour >= 6 && hour <= 9)){
        // postUpdate(RunToday, 1) 
        postUpdate(RunToday, 1)
        createTimer(now.plusHours(3), [|postUpdate(RunToday, 0)])
        logWarn(ruleName,"Morning Routine") 
        Alexa_TTS.sendCommand('Guten Morgen Henning, ich starte deine Morgen-Routine') 
        Thread::sleep(5000) 
        DenonInput.sendCommand('CD') 
        AlexaTraffic.sendCommand("Traffic") 
        gLobbyLights.sendCommand(ON) 
        gLivingroomLights.sendCommand(ON) 
        gOfficeLights.sendCommand(ON) 
        gKitchenLights.sendCommand(ON) 
        str_sonos_bad_add.sendCommand("RINCON_000E582AC07E01400")
        str_sonos_bad_add.sendCommand("RINCON_B8E9375835EC01400")
        str_sonos_bad_add.sendCommand("RINCON_000E582AC07601400")
        str_sonos_bad_add.sendCommand("RINCON_5CAAFD1670C201400")
        executeCommandLine("python /home/openhabian/addons/BlackBeanControl/BlackBeanControl.py -c NUBERTON -d RM-mini-office", 5000)
        Thread::sleep(10000) 
        pl_sonos_bad_control.sendCommand("PLAY")
        Thread::sleep(3000) 
        executeCommandLine("python /home/openhabian/addons/BlackBeanControl/BlackBeanControl.py -c NUBERTCHANOPTICAL -d RM-mini-office", 5000)
        }
        else 
        {
			logWarn(ruleName, "Morning Routine not starting because of timer constraints")
		}

        
end