Publish data from openhab in json format

I created rules to publish data in json format, I’m not really sure about the format of the rule… So my rules has errors, any help on what am I doing wrong…?

The format in which I want the data is

{
    "reference":"1234",
    "confirmed": true,
    "fPort": 10,
    "object": {
        "sep": 255,
        "control1": 1
  
    }
}


the control1 would display 1 or 0 depending on the status of mylight

The sitemap and items file

Switch item=mylight label="Light1"
Switch mylight "light1" <slider> ["Switchable"]  {mqtt=">[mosquitto:application/2/node/00000000000000:command:ON:1],>[mosquitto:application/2/node/00000000000000:command:OFF:0]"}

The rule I wrote has errors…

rule "mqtt update"
when
    Item  mylight received update
then
    mylight.SendCommand("{\"fport\":\"10\",\"object"\"control1\":\"mylight\"}")
    logInfo("Test2","running")
end	
	
   

So you need to send the json string to the light?
You might be able to achieve that by using the map transform

Create a mylight.map file in the transform folder with the following content:

ON={"reference":"1234","confirmed": true,"fPort": 10,"object": {"sep": 255,"control1": 1}}
OFF={"reference":"1234","confirmed": true,"fPort": 10,"object": {"sep": 255,"control1": 0}}

Change your item definition as follow:

Switch mylight "light1" <slider> ["Switchable"]  {mqtt=">mosquitto:application/2/node/00000000000000:command:*:MAP(mylight.map)]"}

If it doesn’t work you may need to ‘escape’ the double quotes in the .map file but it should work as is as specified here:

https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html#load-java.io.Reader-

oh… Never thought about transforms . Yeah I need to send the status of mylight to mqtt broker.
I did like you said but it is not working… I’m sorry but what exactly do you mean by escape double quotes

I read through the link you gave, but I really did not get it.

OK, what exactly do you need to send to the broker?

I need to send the following data,

{
“reference”:“1234”,
“confirmed”: true,
“fPort”: 10,
“object”: {
“sep”: 255,
“control1”: 1

}

}

control1 gives the status of mylight 1 if its ON, 0 if it’s OFF

Ok, do you have an MQTT monitor like mqtt.fx?
Can you check that the messages are being sent?

1 Like

I am able send it to mqtt lens, but that’s only 1 and 0. I need to send it with json payload… I tried doing so by writing rules, but like I said my rules have errors but I’m not sure what errors…

image

It errors because you try to send a string to a switch item. A switch item only accepts ON and OFF as commands. If you want to send something else to the actual device you need to specify that in the item definition, with a transform, like @vzorglub suggested. An alternative is to use the mqtt action to publish directly from the rule instead.

I’m trying to send to mqtt broker…

Mqtt action I’ll try with that.

1 Like

@mmj6
It should work as I set it up
What does the openhab.log show when you change the switch on the UI?
Is the MAP transform installed?
Is the mylight.map located in the /etc/openhab2/conf/transform folder?
Can you publish you code? rules, items and map file as they are, please?
Thanks

logs

2018-08-28 14:52:40.882 [INFO ] [io.openhabcloud.internal.CloudClient] - Disconnected from the openHAB Cloud service (UUID = df470386-e60e-4e6b-81bd-05f768013f55, base URL = http://localhost:8080)
2018-08-28 15:35:12.278 [INFO ] [io.openhabcloud.internal.CloudClient] - Connected to the openHAB Cloud service (UUID = df470386-e60e-4e6b-81bd-05f768013f55, base URL = http://localhost:8080)
2018-08-28 15:38:22.725 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'b.items'
2018-08-28 15:38:22.744 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'b.items'```

Transform

```php
ON={"reference":"1234","confirmed": true,"fPort": 10,"object": {"sep": 255,"control1": 1}}
OFF={"reference":"1234","confirmed": true,"fPort": 10,"object": {"sep": 255,"control1": 0}}

The mylight.map file is in C:\openHAB2\conf\transform
I deleted the rules I wrote

items

Switch mylight "light1" <slider> ["Switchable"]  {mqtt=">[mosquitto:application/2/node/0000000000000013/tx:command:*:MAP(mylight.map)]"}
Switch mylight "light1" <slider> ["Switchable"]  {mqtt=">[mosquitto:application/2/node/0000000000000013/tx:command:ON:1],>[mosquitto:application/2/node/0000000000000013/tx:command:OFF:0]"}

Although there are no errors this data is not getting sent to mqtt broker

You have two items named the same. That won’t work.
Rename the second one and
And add a space after the { abd before the } it helps the parser sometimes

Switch mylight "light1" <slider> ["Switchable"]  { mqtt=">[mosquitto:application/2/node/0000000000000013/tx:command:*:MAP(mylight.map)]" }
Switch mylight2 "light2" <slider> ["Switchable"]  { mqtt=">[mosquitto:application/2/node/0000000000000013/tx:command:*:default] }

I deleted one of it.
now my items file has only this

Switch mylight "light1" <slider> ["Switchable"]  {mqtt=">[mosquitto:application/2/node/0000000000000013/tx:command:*:MAP(mylight.map)]"}

logs

2018-08-28 16:16:11.665 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'b.items'
2018-08-28 16:17:29.945 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'b.items'
2018-08-28 16:17:29.961 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'b.items'
2018-08-28 16:22:52.543 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'b.items'
2018-08-28 16:22:52.556 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'b.items'

What does the both logs show when you action the switch in the sitemap?

openhab logs shows only refreshing model b.items,

event logs

2018-08-28 16:53:11.934 [ome.event.ItemCommandEvent] - Item 'mylight' received command ON
2018-08-28 16:53:11.936 [vent.ItemStateChangedEvent] - mylight changed from OFF to ON
2018-08-28 16:53:12.094 [ome.event.ItemCommandEvent] - Item 'mylight' received command OFF
2018-08-28 16:53:12.097 [vent.ItemStateChangedEvent] - mylight changed from ON to OFF

Ok and what do you see on mqtt lens when you action the switch?

Right now nothing…

Change the item to:

Switch mylight "light1" <slider> ["Switchable"]  {mqtt=">[mosquitto:application/2/node/0000000000000013/tx:command:*:default]"}

What do you get it MQTT lens?

Sorry for bringing this from the dead just wanted to mention that this in my things/mqtt.things saved me

   Thing mqtt:topic:LEDStrip_LivingRoom_Power "LEDStrip_LivingRoom_Power" {
     Channels:
         Type switch : switch "LEDStrip_LivingRoom_Power Switch" [
           commandTopic="hestia/livingroom/LED/set",
           on="{'state':'ON'}",
           off="{'state':'OFF'}"
         ]
   }

mixed double and single quotes right in the thing definition until mapping for outgoing MQTT (2.5) is relased. Currently on 2.4 and works fine.

1 Like