I have mqtt tasmota device which is sending three different JSONS into one topic which I need to monitor
one format is:
{"Time":"2024-01-12T19:19:27","TuyaSNS":{"Temperature":21.0,"TempSet":30.0},"Thermostat1":{"ThermostatModeSet":1,"TempTargetSet":18.00,"CtrDutyCycleRead":44,"ControlMethod":0,"HybridControllerPhase":1,"EmergencyState":0},"TempUnit":"C"}
second:
{"TuyaSNS":{"TempSet":31.0}}
third:
{"TuyaSNS":{"Temperature":27.0}}
This is thermostat, which first is periodic state published automatically, second is when physical button on the device is pressed and third is when internal temperature is changed. And this is published by tasmota itself.
What I need tho, is to catch TempSet from the device and use it as tempset for tasmota internal thermostat.
To avoid a lot of parsing errors in logs while I was doing parsing on incomming MQTT payload in things.file I’d like to use rule.
So i’ve done item which stores whole json. and then got testing rule
rule "Wellness floor temperature"
when
Item Wellness_Temperature changed or
Item Wellness_Heating_Json changed
then
val String json = (Wellness_Heating_Json.state as StringType).toString
val String type = transform("JSONPATH", "$.TuyaSNS._type", json)
if (type == "Temperature") {
val String temperature = transform("JSONPATH", "$.TuyaSNS.Temperature", json)
Wellness_Heating_Temp.sendCommand(temperature)
}
if (type == "TempSet"){
val String tempset = transform("JSONPATH", "$.TuyaSNS.TempSet", json)
Wellness_Temperature_Set.sendCommand(tempset)
}
end
But type is never populated with proper Strings, it’s always full json for some reason
when I’m trying this approach
val String temp = transform("JSONPATH", "$.TuyaSNS.Temperature", json)
val String set = transform("JSONPATH", "$.TuyaSNS.TempSet", json)
logInfo("x",temp)
logInfo("z",set)
That works if first format arrives.
if second or third, one of the variables holds whole json again instead being empty.
2024-01-12 19:24:58.564 [INFO ] [org.openhab.core.model.script.x ] - {"TuyaSNS":{"TempSet":35.0}}
2024-01-12 19:24:58.564 [INFO ] [org.openhab.core.model.script.z ] - 35.0
Is that expected behaviour?
Can it be done somehow dynamically? or is there a better way how to get value from these?
I do like first approach, but can’t get that example from here JSON Handling in rules? working.
Thanks