I have a Sonoff RF Button paired to a Sonoff RF Bridge.
The RF Bridge has been flashed with Tasmota firmware
OH and the RF Bridge have been configured as MQTT clients
When the Button is pressed, the RF Bridge publishes a MQTT message with the payload
I’m gessing that Transformation can parse the JSON payload and tell me
RfRecived
Sync value
Low value
High value
Data value
RfKey value
The “Data” element seems to be the button identifier
This is the only MQTT message sent
I’d like the have all of the values available when writing rules for a button press event.
How would I define an Item that can parse the payload?
How would I make each of the payload elements available in the rule handling the button press event?
You can create one Item for each piece of data and configure them all to subscribe to the same topic. The difference will be you will use a different JSONPATH to extract different data for each Item. This is a good approach when you don’t necessarily need to do anything beyond putting this data into an Item.
The other option is to use a String Item that subscribes to the MQTT topic and this Item gets updated to the full JSON message. Trigger a Rule when this String Item is updated then use the transform Action to extract each piece of data for use in that Rule. This is a good approach when you need to be sure you have all the fields from that message available at the same time (in the previous approach each Item gets updated in parallel) or you don’t necessarily need to store the data in an Item.
rule "RfBridge send message"
when
Item SonoffRF changed
then
var mqttMsg = SonoffRF.state.toString
logInfo("SonoffRFMsg", mqttMsg)
var dataElement = transform("JSONPATH", ".$.RfReceived.Data", SonoffRF.state.toString)
logInfo("SonoffRFData", dataElement)
end
I also notice that if the bridge sends the exact same message as the previous message, the rule does not fire. Is there a nifty way to fire the rule 100% of the time?
rule "RfBridge send message"
when
Item SonoffRF changed
then
var mqttMsg = SonoffRF.state.toString
logInfo("SonoffRFMsg", mqttMsg)
var dataElement = transform("JSONPATH", "$.RfReceived.Data", mqttMsg)
logInfo("SonoffRFData", dataElement)
end
Well, that did not seem to do anything, any other pointers?
rule "RfBridge send message"
when
Item SonoffRF received update
then
var mqttMsg = SonoffRF.state.toString
logInfo("SonoffRFMsg", mqttMsg)
var dataElement = transform("JSONPATH", ".$.RfReceived.Data", mqttMsg)
logInfo("SonoffRFData", dataElement)
end
rule "RfBridge send message"
when
Item SonoffRF received update
then
var mqttMsg = SonoffRF.state.toString
logInfo("SonoffRFMsg", mqttMsg)
var dataElement = transform("JSONPATH", "$.RfReceived.Data", mqttMsg)
logInfo("SonoffRFData", dataElement)
end