Received Event only works with channels, not with items - how can i do this?

Hi,
my xiaomi button switches send a trigger-command to a channel and then i can get the command with this lines inside my rule:

rule "Xiaomi Button Switch 1"
when
    Channel "mihome:sensor_switch:158d00012345678:button" triggered
then
    var actionName = receivedEvent.getEvent()
    switch(actionName) {
        case "SHORT_PRESSED": {
            logInfo("RULES", "Xiaomi 1 triggered SHORT_PRESSED")
end

But this is only working with this channel. How can i do this with a switch or another item?

rule "Test Item 1"
when
    item test_item_1 received update
then
    var actionName = receivedEvent.getEvent()
    logInfo("RULES", "test_item_1 received the following: " + actionName)
end

Can someone tell me, how i have to change / rewrite my second rule, so that this is working?

I currently get this error-message:

2018-05-04 10:42:38.161 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Test Item 1': The name 'receivedEvent' cannot be resolved to an item or type; line 182, column 19, length 13

I highly recommend using VSCode with the openHAB extension.

You should have seen all sorts of errors in the log.

  • Capitalize the “i” in Item in the trigger
  • Rules triggered by Items receive commands or changes, but not Events so I suspect receivedEvent is null
rule "Test Item 1"
when
    Item test_item_1 received update
then
    var actionName = test_item_1.state.toString
    logInfo("RULES", "test_item_1 received the following: " + actionName)
end

This was not my original rule, i have item already written with a capitalized “I”. I already use vscode and i was knowing before, that the rule is not correct, but i had no idea, how to solve this.


But if i check for the state.toString of the item, i will only get the state after the command? In my case i can send “100” or “down” to my rollershutter item. Both states after the command are “100”. But i want to get the real command, not the result of this command.

Example:
I send “down” to the rollershutter, after half of the way i send “stop”. Now i want to get the command “stop” as actionName. But i will get “50” - for 50% closed.

You are triggering the rule using received update. The rule will not trigger until test_item_1 is set to the new state from the update so using test_item_1.state.toString will reflect the latest state of the Item.

Then you need to use received command instead of received update and use the receivedCommand implicit variable.

var actionName = receivedCommand.toString