Merging few item states into JSON

Hello
I’m trying to integrate my openhab system with Arduino (ESP8266) automatic humidifier. Is there any way to collect few item states or Dimmer values into single JSON, for example using JS transformation, before pushing the data into MQTT topic? Here are my items definitions

Switch Bedroom_LimeWatering_Status "System Status" <switch> {channel="mqtt:topic..."}
Dimmer Bedroom_LimeWatering_Humidity "Humidity [%s Units]" <humidity> {channel="mqtt:topic..."}
Dimmer Bedroom_LimeWatering_Threshold "Threshold [%s Units]" <rain> {channel="mqtt:topic..."}

So, JSON data from all this things should be like this

{
  "Status" : "ON",
  "Humidity" : 255,
  "Threshold" : 500
}

and only after such transformation that json should be passed to some MQTT topic.

Thanks

Yes you can, and easiest is probably using rules to create your JSON string, then the MQTT Action to send it out: https://www.openhab.org/addons/bindings/mqtt.generic/#rule-actions

Or you could send the rule-created JSON string through a Thing channel - see an example here: Activate (arm) timers via mqtt to Tasmota flashed Sonoff Basic R2

Hey @NeverGiveUp

Welcome to openhab it looks like you are having fun. Please read all the docs https://www.openhab.org/docs/

Nah just joking here is easy example for you.

Please change your item types from Dimmer to Number as its only a number with no unit and is 1-100

rule "automatic humidifier Format to json and send to MQTT"
when

  Item Trigger received command  

then

    val mqttActions = getActions("mqtt","mqtt:broker:myMQTTBroker") // mqtt:broker:myMQTTBroker is the name of my broker please change 
    var jsonString = '{"Status" : "' + Bedroom_LimeWatering_Status.state + '", "Humidity" : ' + Bedroom_LimeWatering_Humidity.state + ', "Threshold" : ' + Bedroom_LimeWatering_Threshold.state + ' }'        
    mqttActions.publishMQTT("Bedroom/send", jsonString)   // Bedroom/send is the topic in mqtt
    logInfo("Json Data", "" + jsonString )

end
1 Like