Basic Scene rule not working as Id expec

Hi All

I’m using a Smart Implant with a button to send a scene, of 1.0 which works well. It seems it doesnt like my syntax on the if statement, what am I missing? I dont want to actually check the value is 1.0, I want to check the received command is 1.0

1.0 is being sent correct as per the previous log entry and the items status is indeed, 1.0


rule "Side Gate"
when
        Item SideGateScene received update
then
     var int sceneNumber = SideGateScene.state as DecimalType
        if (receivedCommand == 1.0){
         logInfo("Side Gate", "Scene: " + sceneNumber)
         BedRoom1Sw1.sendCommand(ON)
}
end

Log entry:

19:42:21.682 [INFO ] [openhab.event.ChannelTriggeredEvent  ] - mqtt:broker:MQTT_Broker:remote_updates triggered House/out/SideGateScene/state#1.0
19:42:21.723 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID 'sidegate-1' failed: The name 'receivedCommand' cannot be resolved to an item or type; line 6, column 13, length 15 in sidegate

Thanks!

Maybe Just trigger on that command will work

rule "Side Gate"
when
        Item SideGateScene received command 1.0
then
         logInfo("Side Gate", "Scene: 1.0" )
         BedRoom1Sw1.sendCommand(ON)
end

When the rule is triggered by this state update event …

…there is no spoon… uh, there is no command.
receivedCommand doesn’t even exist unless the rule is triggered by received command.

But there’s more -

You’ve got to do something else altogether if you want to trigger the rule from that event.
We already know it isn’t a command, but look closer.
It’s not an Item based event at all - there is no Item mentioned, certainly not SideGateScene.

This is a channel trigger event - a transient “thing that happens”, not a state, not a command.

Trigger your rule from the channel trigger event.

I think maybe you’ve misconfigured your MQTT though, did you intend for this to be broker event rather than Item update?
Working with events is not bad thing to do - it’s entirely sensible for dealing with button clicks. But I think you got here by accident perhaps?

1 Like

MQTT works just fine. But I do such rules based off an event update without an issues in the past, this is the MQTT bus which is publishing commands to the item.

Channel trigger isnt possible, the item is not bound to any channel, its updated by the MQTT event bus from another downstream openhab

Okay, makes sense, the events.log extract is irrelevant, so long as your Item updates. But there is still no command here.

Just to summarize and point out one other thing I see here.

  • @rossko57 is absolutely right, you’ve triggered the rule by update. There is no command to receive so receivedCommand doesn’t exist. That’s your root error.

  • var int sceneNumber = SideGateScene.state as DecimalType is, to put it bluntly, a bunch of nonsense.

    1. It’s good practice that when you create a variable that will not be changed later, use val instead of var.
    2. It’s possible, though probably unlikely, that SideGateScene’s state might have changed in the time from when the rule was triggered and this run executes. Do you really want the state as it is now or do you want the state that actually triggered the rule? Usually it’s the latter. As documented, rules triggered by an update or change will have a newState variable that has the state that triggered the rule.
    3. You’ve defined sceneNumber as an int. Then you pull the state and cast it to DecimalType. A DecimalType isn’t an int. An int isn’t a DecimalType. I’m actually a little surprised that the rule didn’t completely blow up on that line. Though I’ve no idea what type sceneNumber is going to be in. Will it be an int? Will it be a DecimalType? Will it be something else entirely (e.g. BigDecimal)? Who knows? As a general rule, never provide the type when you define the variable (i.e. no int). For most flexibility, cast the state of a plain old Number Item (i.e. on without units) to Number instead of DecimalType. Finally, avoid primitives (e.g. int, float, double, etc.) as much as possible in rules DSL. They only cause problems.
    4. Why go through all the effort of extracting and casting the sceneNumber only to ignore it on the next line in your if statement? Even if you know that two variables have the same value, always be consistent in which one you use. If you want to use newState (since receivedCommand doesn’t exist) always use newState. If you want to use sceneNumber, always use sceneNumber. Don’t mix and match.

Thanks guys, i made some changes and its working now!