Check if command is same as current state

When I add this to my script they both show same output.

console.log("command: ", event.itemCommand.toString());
console.log("state: ", items.getItem(event.itemName).state);

How do I check if the state is changed or if the incomming command is same as current state?

There is event.oldItemState and event.newItemState available that you can compare

How is the Item triggered? While there is an oldItemState and newItemState on the event, that only exists if the rule is triggered by a change.

If you triggered the rule by the command, which has to be the case since itemCommand exists, there is no guarantee that the Item has changed state in response to the command. It looks like it has based on your description but you can’t count on that because the rule is triggered before the Item is updated. It happens in parallel so most of the time the Item won’t have changed yet when the rule is running.

Furthermore, the command may not have much relation to the state. For example, you can send an INCREASE command to a Dimmer Item but a Dimmer’s state can never be INCREASE.

Therefore, if you care whether or not the Item changed state, you should use a changed trigger in the first place. You can even specify a from and to to capture only specific state changes (e.g. changed from OFF to ON).

1 Like

What I am trying to do is to add a “hidden” function to my wallswitches.

If the lamp in the room is already in the position that the wall switch sent a command for, I want to turn on the power switch for the speaker in the room.
This is because the speaker draws 7W in standby mode and I don’t want put up another wallswitch just for the speaker.

After the ON command for the speaker is sent, a timer in openhab will be added to turn off the speaker after let’s say 2 hours.

So how do I check if the lamp is already in the state as the sent wallswitch command?

Maybe a workaround could be using a variable to compare current command with. And then before the script ends, the variable is updated with the value of the current command.

Maybe have a look to trigger the rule but an item update.

The update will also be executed if the items state is not changing.

The problem with item update is that oldItemState is undefined while itemState is correct.

You’ve got to think this through, and be very careful about timing effects.
Say, you receive wallswitch command ON.
When a rule runs in response to the ON command, it’s no use looking at the lamp state.
You might see OFF - because it has not yet acted on the new command.
You might see OFF - but it is really ON and the report that it is now ON has not yet been processed.
You might see ON because it has just turned on and reported its new state.
You might see ON because it was on already before the new command came along - the one case you are trying to detect.

And its no use running a rule on lamp state change - you will never see the event of a second do-the-same-thing command, the one you want to detect.

What you’d really like to know is the state of the lamp just before this new command came along. Looking at the current reported state won’t do because you don’t know if the new command has affected it yet or not.

I think you might need to use an Item or variable to represent the state a little while ago, and figure out how to keep that updated. Probably just mirroring the live reported state - but with a delay.

The problem solved by creating a global variable to compare with state on item update. Then I update the global variable with current state.