How to publish json data form openhab2.5

Hi! I am new here and bigginner with openhab.
How to send from item out to other device such json fromat data topic ems-esp/thermostat {“cmd”:“nighttem”, “data”: 22, “id”:“hc1”. How the transformation should be in paperui.

  • Platform information:
    • Hardware: CPUArchitecture/RAM/storage
    • OS: what OS is used and which version
    • Java Runtime Environment: which java platform is used and what version
    • openHAB version:
  • Issue of the topic: please be detailed explaining your issue
  • Please post configurations (if applicable):
    • Items configuration related to the issue
    • Sitemap configuration related to the issue
    • Rules code related to the issue
    • Services configuration related to the issue
  • If logs where generated please post these here using code fences:

Welcome @pitulek!

Can you confirm what you’re trying to do? It sounds like:

  • You have an openHAB Item, which contains something - what does it contain? What type of Item is it? How does the Item get this ‘something’ in the first place?
  • You then have a Thing Channel, and you would like to publish the following string to ems-esp/thermostat. When would you like to publish this JSON string? Every time your Item changes? Or when you press a switch? And which part of the JSON string comes from your openHAB item?
 {"cmd":"nighttem", "data": 22, "id":"hc1"}

We could probably make a reasonable guess at what you’re after, but it’s probably best if you provide a little more detail on what you want! Though it does sound like you are going to need to use Rules here…


When copying and pasting code or snippets of data, please use code fences: sandwich your text in-between three back-ticks: ```

```
like this
```

This suggests you are using MQTT, but you did not say.

1 Like
rule "Send Command to Aircon through IRBLASTER"
when

    Item AirconditionerPower received command or
    Item AirconditionerSetTemp received command or
    Item AirconditionerSetMode received command
then
        val mqttActions = getActions("mqtt","mqtt:broker:myMQTTBroker")
        var int setTemp = (AirconditionerSetTemp.state as Number).intValue
        var jsonString = '{"Vendor":"GREE","Model":1,' + 
                         '"Power":"'+ AirconditionerPower.state.toString +'",' +
                         '"Mode":"'+ AirconditionerSetMode.state.toString +'",' +
                         '"Celsius":"On",' +
                         '"Temp":'+ setTemp +',' +
                         '"FanSpeed":"Auto",' +
                         '"SwingV":"Auto",' +
                         '"SwingH":"Off",' +
                         '"Quiet":"Off",' +
                         '"Turbo":"Off",' +
                         '"Econo":"Off",' +
                         '"Light":"On",' +
                         '"Clean":"Off",' +
                         '"Filter":"Off",' +
                         '"Beep":"Off",' +
                         '"Sleep":-1}'
        logInfo("IRhvac", " Aircon To " + jsonString )    
        mqttActions.publishMQTT("irBlaster2/cmnd/IRhvac",  jsonString )         
        
end

rule "If google control tuning aircon on/off"
when
   Item AirconditionerMode received command
then

          if (receivedCommand != OFF){ 
            AirconditionerPower.sendCommand(ON)
            AirconditionerSetMode.sendCommand(receivedCommand)
            
            } else {
                      AirconditionerPower.sendCommand(OFF)
                   }

end

A little something like this?

I want send temperature value from slider or knob when it change state.
The item -number gets set temperature from mqtt subscription topic ems-esp/thermostat with transformation
JSONPATH:$.hc1.temp and it is working.
but do not know what transformation should be to publish this when it change the state-new value.
the output shuld be like this:

{"cmd":"nighttem", "data": <changed state -value>, "id":"hc1"}

So:

  • Your Item is a Number type.
  • You have a Slider in your Sitemap which can change the value of the number.

If the above is true, you can use formatBeforePublish, which in paperUI is called Outgoing Value Format. In there you can put:

{\"cmd\":\"nighttem\", \"data\": %d, \"id\":\"hc1\"}

I’m not 100% certain whether you have to escape the " using paperUI. You could also try:

{"cmd":"nighttem", "data": %d, "id":"hc1"}

The %d is a guess, assuming you are using an integer - it might be that %s works instead. The description under Outgoing Value Format has some more information.

Just for clarity … when you tweak a UI control it generates a command to the Item, not a direct state change.
The binding channel is listening for Item commands to send outgoing.

not working , nothing happen

{\"cmd\":\"nighttem\", \"data\": %d, \"id\":\"hc1\"}

Where did you put that? What did you do, that you expected something to happen from? How do you know nothing happened, rather than something you did not want?
We can’t see what you are doing, you have to talk to us a bit more.

so i took screenshots, maybe easier

this my panel and slider connected to thermostat channel

i want the channel to send mqtt format to other davice when slider change this format command

{"cmd":"nighttem", "data": %d, "id":"hc1"} with the data from slider value

and i put the json format here

sorry for my bad english, i native language is polish

We’re going to have to assume that your openHAB Item is correctly linked to your Thing Channel, and to your HABPanel slider widget.

If so, the following should work:

{"cmd":"nighttem", "data": %s, "id":"hc1"}

I did recommend trying with %s and removing the escape slashes - did you try that already?


For completeness I have got what you want working fine. Here are my test configurations:

thing

Thing mqtt:topic:test_thermostat "Test Thermostat" (mqtt:broker:MosquittoMqttBroker){
    Channels:
        Type number:temperature "Temperature" [
            commandTopic="ems-esp/thermostat",
            formatBeforePublish="{\"cmd\":\"nighttem\", \"data\": %s, \"id\":\"hc1\"}"
        ]
}

In PaperUI the Channel configuration looks as below:

item

Number nThermostatTemperature "Thermostat Temperature" { channel="mqtt:topic:test_thermostat:temperature" }

In PaperUI, the Item configuration looks like below:

The Item is linked to the temperature Channel created above. This is shown in PaperUI as below:

sitemap

I first tested with a Sitemap, as I don’t use HABPanel.

Slider item=nThermostatTemperature icon="temperature" minValue=15 maxValue=21 step=0.1

image

Moving the slider sends the number over to ems-esp/thermostat as shown in MQTT Explorer output below:

image

HABPanel

In HABPanel, I added a Slider widget, and configured as follows:
image

Which ends up looking like:
image

And also seems to work.

In the pic you posted of the Generic MQTT Thing Configure channel

Set MIN to Zero and MAX to 100

Thank you very much for your help.You are great. It is working!

1 Like

Thank you very much for your help. Working!

Hey @denominator i know this thread is old but ur the brilliant when it comes to this, i used to have a normal relays with easyesp which was controlled using mqtt and i have rule to that when a button is clicked it inverts that status of another one its basically a 2 relays on a shutter window i use this rule to protect the motor from being fried and getting power on both up and down, i replaced this with shelly 1 plus which uses mqtt with json i have managed to send and received and control them successfully but the rule does not seem to work i think it needs to be json and i can’t figure it out, if you could help here is the the original rule

rule "S6S7”
when
Item S6S7 received update ON
then
S6LED8.sendCommand(OFF)
S6LED7.sendCommand(OFF)
S6LED7.sendCommand(ON)
end

rule "S6S8”
when
Item S6S8 received update ON
then
S6LED7.sendCommand(OFF)
S6LED7.sendCommand(OFF)
S6LED8.sendCommand(ON)
end

S6LED7 and S6LED8 are the relay lines
S6S7 and S6S7 are the manual push buttons

Really appreciate the help

You can configure you items to send json or you can send json directly to MQTT