MQTT Channel string not working

Raspberry PI Openhab 3
MQTT binding 3

My channel “mqtt:topic:b8be661fe1:slaapkamer_wireless_wall_switch:action” can produce 3 strings “single_right”, “single_left” and “single_both”

I can see this in my event.log

2021-01-20 22:18:14.821 [INFO ] [openhab.event.ChannelTriggeredEvent ] - mqtt:topic:b8be661fe1:slaapkamer_wireless_wall_switch:action triggered single_left

How do i use this in a rule to react to one of the values?

The rule below gives no error , but its not working

//Wireless 2 button switch
rule "lamp on/off"
when
    Channel "mqtt:topic:b8be661fe1:slaapkamer_wireless_wall_switch:action" triggered 
then
    var actionName = "receivedEvent.getEvent"
    switch(actionName) {
        case "single_left": {
            StudeerkamerHuewhite_Dimmer.sendCommand(100)
			logInfo("test", "Light ON")
        }
        case "single_right": {
            StudeerkamerHuewhite_Dimmer.sendCommand(0)
			logInfo("test", "Light OFF")
        }
    }
 end

How do i get the value off my channel in my variable “actionName”

Something which might make this easier is to link your String Channel to a String Item, and then trigger when that Item has changed.

I think you can then use triggeringItem.state in your subsequent rule, i.e.

switch(triggeringItem.state) {

...
}

Scroll down to the DSL rule in the post below, where I do what you’re ultimately after, in a slightly different way:

1 Like

This line of code sets actionName to the String “receivedEvent.getEvent”. That’s not meaningful. You want the actual string value from the event itself.

val actionName = receivedEvent

In OH 3 receivedEvent was changed from being an object that carries an event to just be the String value of the event.

But, if I were to set this up I’d do it in the Channel and use a mapping or JS transformation to convert the values to an appropriate Item state. Then there’s be no need for a rule at all.

1 Like

Both of you explanations were very educational.
Thanks for that.
I’ve gotten it working with "val actionName = receivedEvent

I get the concept, but sometimes find it difficult to find the correct spelling (syntax) in DSL and other languages used in openhab

Thanks.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.