Rule triggered on Item command uses old state

Hi community,

I am using the rule template Time based state machine which works very well. I use it to determine TimeOfDay states and for the purpose of this question, I enabled log:debug. The output is (and this is expected so correct):

2025-04-07 00:05:10.982 [DEBUG] [omation.rules_tools.TimeStateMachine] - All etod Items are configured correctly

2025-04-07 00:05:10.982 [DEBUG] [omation.rules_tools.TimeStateMachine] - Cancelling existing timers

2025-04-07 00:05:10.984 [DEBUG] [omation.rules_tools.TimeStateMachine] - Acquiring today's state start times

2025-04-07 00:05:11.203 [INFO ] [omation.rules_tools.TimeStateMachine] - Today is a default day.

2025-04-07 00:05:11.204 [DEBUG] [omation.rules_tools.TimeStateMachine] - Creating timers for times that have not already passed

2025-04-07 00:05:11.267 [DEBUG] [omation.rules_tools.TimeStateMachine] - Creating timer for MORNING at 2025-04-07T07:08+02:00[Europe/Amsterdam]

2025-04-07 00:05:11.276 [DEBUG] [omation.rules_tools.TimeStateMachine] - Creating timer for DAY at 2025-04-07T07:38+02:00[Europe/Amsterdam]

2025-04-07 00:05:11.286 [DEBUG] [omation.rules_tools.TimeStateMachine] - Creating timer for AFTERNOON at 2025-04-07T19:53+02:00[Europe/Amsterdam]

2025-04-07 00:05:11.296 [DEBUG] [omation.rules_tools.TimeStateMachine] - Creating timer for EVENING at 2025-04-07T20:23+02:00[Europe/Amsterdam]

2025-04-07 00:05:11.305 [DEBUG] [omation.rules_tools.TimeStateMachine] - Creating timer for NIGHT at 2025-04-07T21:30+02:00[Europe/Amsterdam]

2025-04-07 00:05:11.319 [DEBUG] [omation.rules_tools.TimeStateMachine] - There is no date time for today before now, we can't know what the current state is, keeping the current time of day state of NIGHT.

Then, I have a rules that fire on the item TimeOfDay receives command. This rule is indeed fired when the ToD item changes but the state is wrong…
Rule:

var lights = items.gGardenLights
var ToD = items.TimeOfDay.state
var christmas = time.toZDT().isBetweenDates(time.toZDT().year() + '-12-23', time.toZDT().year() + '-12-27')
var atSix = time.toZDT().isBetweenTimes("06:00", "06:01")
var newYear = actions.Ephemeris.getBankHolidayName(0, '/openhab/conf/services/custom_day.xml') == 'Oudjaarsdag'
console.log("ToD is " + ToD)

if(christmas && (ToD != "NIGHT" || atSix)){
  lights.sendCommand("ON");
  console.log("Christmas on")
} else if(newYear){
  lights.sendCommand("NY ON");
  console.log("NY on")
} else if((ToD != "DAY" && ToD != "NIGHT") || atSix){
  lights.sendCommand("ON");
  console.log("Normal on")
} else{
  lights.sendCommand("OFF");
  console.log("Off")
}

Log:

2025-04-07 06:00:00.942 [INFO ] [nhab.automation.script.ui.7bfa2714ab] - ToD is NIGHT

2025-04-07 06:00:00.943 [INFO ] [nhab.automation.script.ui.7bfa2714ab] - Normal on

2025-04-07 07:08:00.001 [INFO ] [omation.rules_tools.TimeStateMachine] - Transitioning Time State Machine from NIGHT to MORNING

2025-04-07 07:08:00.028 [INFO ] [nhab.automation.script.ui.7bfa2714ab] - ToD is NIGHT

2025-04-07 07:08:00.029 [INFO ] [nhab.automation.script.ui.7bfa2714ab] - Off

2025-04-07 07:38:00.000 [INFO ] [omation.rules_tools.TimeStateMachine] - Transitioning Time State Machine from MORNING to DAY

2025-04-07 07:38:00.031 [INFO ] [nhab.automation.script.ui.7bfa2714ab] - ToD is MORNING

2025-04-07 07:38:00.031 [INFO ] [nhab.automation.script.ui.7bfa2714ab] - Normal on

The first part, at 6 O’clock, it goes well. Thereafter, at 7:08, we see the ToD item change to MORNING but somehow, the rule logs it as NIGHT. At 7:38 we see the ToD item change to DAY but the rules logs it as MORNING. In other words, every time the ToD received a command with the new state, the old state ‘hangs’ somewhere and is then grabbed by the rule.

Anyone with a similair issue? I am on OH 4.3.3

Hey… What is your rule trigger?
‘Changed’ or ‘received command’?
greets

Received command. Could that be an issue? If you receive a command, is the 0.33 seconds not enough to update and the rule get’s the old state?

I had no issues in OH2.5 where I upgraded from though…

Yes, that’s the issue.
if your item receive a command it is not changed :wink:
So the rule is fired before the item has changed its state…
Try changing your trigger to ‘changed’
greets

1 Like