Extracting variables from result of Jsonpath transformation - Ikea Switches

Hi,

I am using a number of Ikea switches via a sonoff zigbee bridge flashed with Zigbee2Tasmota.

Whilst i have them working, my solution is clumsey and i am sure it could be better. In summary:

Thing:
Thing mqtt:topic:Tas_Bridge “Zigbee Bridge” (mqtt:broker:MosquittoMqttBroker) {
Channels:
Type string : lounge “Lounge Switch” [
stateTopic=“tele/Tas_Bridge/A50F/SENSOR”,
transformationPattern=“JSONPATH:$.ZbReceived.Ikea_Switch_2”
]
}

Item:
String Ikea_Sw_2 { channel=“mqtt:topic:Tas_Bridge:lounge” }

The above sets Ikea_SW_2 to {Device=0xA50F, Name=Ikea_Switch_2, 0102!01=, ShutterClose=true, Endpoint=1, LinkQuality=63} When fired.

Currently because the switch uses different variables for different actions ShutterOpen=True, ShutterClosed=True etc I am having to do a string search to fine the relevant text “ShutterOpen” or ShutterClosed.

If it was a Json String I could use $.Ikea_Switch_2.ShutterOpen or $.Ikea_Switch_2.ShutterClosed, but this does not work as it s not a Jason string.

Is there a way to extract the data from this string in a more structured way?
I feel there should be as it is very similar to Json, but used = instead of : ]

{
Device=0xA50F,
Name=Ikea_Switch_2,
0102!01=,
ShutterClose=true,
Endpoint=1,
LinkQuality=63
}

I have searched the forums in vain so any help most welcome

What is the full message that is published to the tele/Tas_Bridge/A50F/SENSOR topic in each of the ShutterOpen=True and ShutterClosed=True situations?

It seems a little strange that Tasmota would send strings which aren’t in a JSON format. This part of the Z2T docs seem to suggest that it would normally send a properly formatted JSON string:

The full message is proper Jason

‘{“ZbReceived”:{“Ikea_Switch_2”:{“Device”:“0xa
A50F”,“Name”:“Ikea_Switch_2”,“ShutterOpen”:”True”,“Endpoint”:1,“LinkQuality”:79}}}’

It’s only when I use the Jsonpath transform
transformationPattern=“JSONPATH:$.ZbReceived.Ikea_Switch_2” That it delivers the sting with = instead of : so I think it’s something to to with the jsonpath transform in openhab

Ah, that makes more sense!

OK, if this was me I would create two channels because I only like to write simple rules: one for the ShutterOpen=True case, and one for the ShutterClosed=True case.

The message for an open shutter is:

{"ZbReceived":{"Ikea_Switch_2":{"Device":"0xaA50F","Name":"Ikea_Switch_2","ShutterOpen":"True","Endpoint":1,"LinkQuality":79}}}

Presumably the message for a closed shutter is:

{"ZbReceived":{"Ikea_Switch_2":{"Device":"0xaA50F","Name":"Ikea_Switch_2","ShutterClosed":"True","Endpoint":1,"LinkQuality":79}}}

I’d use the following Thing definition:

Thing mqtt:topic:Tas_Bridge "Zigbee Bridge" (mqtt:broker:MosquittoMqttBroker) {
    Channels:
        Type string : lounge_open "Lounge switch: open" [
            stateTopic="tele/Tas_Bridge/A50F/SENSOR",
            transformationPattern="REGEX:(.*ShutterOpen.*)∩JSONPATH:$.ZbReceived.Ikea_Switch_2.ShutterOpen"
        ]
        Type string : lounge_closed "Lounge switch: closed" [
            stateTopic="tele/Tas_Bridge/A50F/SENSOR",
            transformationPattern="REGEX:(.*ShutterClosed.*)∩JSONPATH:$.ZbReceived.Ikea_Switch_2.ShutterClosed"
        ]
}

I’d use the following Items:

String LoungeOpen "Lounge switch: open" (gLoungeSwitch) {channel="mqtt:topic:Tas_Bridge:lounge_open"}
String LoungeClosed "Lounge switch: closed" (gLoungeSwitch) {channel="mqtt:topic:Tas_Bridge:lounge_closed"}
Switch LoungeSwitch "Lounge switch"

And the following rule:

rule "Lounge switch changed"
when
    Member of gLoungeSwitch received update
then
    if(triggeringItem.name == "LoungeOpen"){
        LoungeSwitch.sendCommand("ON")
    }
    else{
        LoungeSwitch.sendCommand("OFF")
    }
end

You will need to have the REGEX transformation service installed.

That really helpfully, thank you. However when I try to use regen like this
REGEX:(.ShutterClosed.)∩JSONPATH:$.ZbReceived.Ikea_Switch_2.ShutterClosed

I get the following in the log when I fire the ShutterClosed event
2020-12-03 21:41:52.646 [WARN ] [t.generic.ChannelStateTransformation] - Transformation service REGEX for pattern (.ShutterClosed.) not found!

Ah!

I was being dim, Regex transformation was not installed. Installed it and it’s now working. Thanks for your help

1 Like

No problem. It’s still quite convoluted to be honest, but I can’t think of a simpler method at the moment!

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