Time of Day - Rule did not trigger, but why?

Hello,

I have put a door/window sensor from xiaomi in my mailbox so I can see when it was opened/closed.

What I now want to do is to light up my hue bulb whenever the mailbox was opened/closed but the rule should only trigger between 7:00 and 23:59

This is my current approach. The problem is that the light does not get turned on:

var Number currenthour = now.getHourOfDay
var String briefkastenvoll = "252,100,50"

rule "Briefkasten Status"
when
    Item XiaomiDoorWindowSensorBriefkasten_OpenStatus changed to CLOSED
then
	if(currenthour >= 7)
	{
		Lampe_Color.sendCommand(briefkastenvoll)
		sendBroadcastNotification("Briefkasten: Sie haben Post!")
		Briefkastengeleert.sendCommand(ON)
	}	
end		

As you can see in the logs the rule should have triggered, what did I do wrong ?

2018-03-15 11:39:08.769 [vent.ItemStateChangedEvent] - XiaomiDoorWindowSensorBriefkasten_OpenStatus changed from CLOSED to OPEN
2018-03-15 11:39:09.500 [vent.ItemStateChangedEvent] - XiaomiDoorWindowSensorBriefkasten_OpenStatus changed from OPEN to CLOSED

Your variable is not in the scope of the rule and will only be evaluated once at startup

rule "Briefkasten Status"
when
    Item XiaomiDoorWindowSensorBriefkasten_OpenStatus changed to CLOSED
then
        var Number currenthour = now.getHourOfDay
	if(currenthour >= 7)
	{
		Lampe_Color.sendCommand(briefkastenvoll)
		sendBroadcastNotification("Briefkasten: Sie haben Post!")
		Briefkastengeleert.sendCommand(ON)
	}	
end	
1 Like

I just made the change as you suggested but and tried if the lights get turned on once I open the mailbox. Still nothing happend. What could be the reason?

2018-03-15 13:13:46.054 [vent.ItemStateChangedEvent] - XiaomiDoorWindowSensorBriefkasten_OpenStatus changed from OPEN to CLOSED

Add some logging and change this line:

rule "Briefkasten Status"
when
    Item XiaomiDoorWindowSensorBriefkasten_OpenStatus changed to CLOSED
then
        var Number currenthour = now.getHourOfDay()
        logInfo("Rule Testing", currenthour.toString)
	if(currenthour >= 7)
	{
		Lampe_Color.sendCommand(briefkastenvoll)
		sendBroadcastNotification("Briefkasten: Sie haben Post!")
		Briefkastengeleert.sendCommand(ON)
	}	
end	

What does the log show?

1 Like

I am sorry it was working I had a mistake in the rule which is why it was ignored, now it works as it should.

Cool, mark the thread as solved and like if you want

1 Like

Did you understand why it was not working?
You needed to get the time value everytime the rule runs so I put the getHourOfDay inside the rule

1 Like

Yes I understood. But before your comment I thought it would always get refreshed even as global variable. Because I thought it would be refreshed with everytime the .rules file is used.

Thanks for pointing this out! This helps me a lot.