Control remote switch by JSON mqtt (connect Domoticz to openHAB)

2,354 views
Skip to first unread message

Sebastiaan Schlepers

unread,
Sep 1, 2015, 2:13:18 AM9/1/15
to openhab
Hi experts,

I am trying to connect Domoticz to my openHAB install (first of all I would like to say that I have no other option because openhab just does not run properly on RPI1, whereas domoticz does).
So I have a domoticz install running with MQTT binding installed and this one is publishing and accepting JSON messages. This looks like:

  --> PUBLISH received, TOPIC:domoticz/out, QoS:0, Retained:false
                        DATA:{
   "Battery" : 100,
   "RSSI" : 12,
   "dtype" : "Temp + Humidity",
   "id" : "513",
   "idx" : 8,
   "name" : "Unknown",
   "nvalue" : 0,
   "stype" : "WTGR800",
   "svalue1" : "25.3",
   "svalue2" : "54",
   "svalue3" : "1",
   "unit" : 0
}

  --> PUBLISH received, TOPIC:domoticz/out, QoS:0, Retained:false
                        DATA:{
   "Battery" : 100,
   "RSSI" : 12,
   "dtype" : "Lighting 2",
   "id" : "0000200",
   "idx" : 6,
   "name" : "Sensor",
   "nvalue" : 1,
   "stype" : "ZWave",
   "svalue1" : "255",
   "unit" : 1
}

  --> PUBLISH received, TOPIC:domoticz/out, QoS:0, Retained:false
                        DATA:{
   "Battery" : 100,
   "RSSI" : 7,
   "dtype" : "Lighting 2",
   "id" : "0000301",
   "idx" : 10,
   "name" : "SwitchPump02",
   "nvalue" : 1,
   "stype" : "ZWave",
   "svalue1" : "0",
   "unit" : 1
}

  --> PUBLISH received, TOPIC:domoticz/out, QoS:0, Retained:false
                        DATA:{
   "Battery" : 100,
   "RSSI" : 7,
   "dtype" : "Lighting 2",
   "id" : "0000301",
   "idx" : 10,
   "name" : "SwitchPump02",
   "nvalue" : 0,
   "stype" : "ZWave",
   "svalue1" : "0",
   "unit" : 1
}

So I have this link to my mqtt established. With Paho MQTT Utility I am able to send different commands to: domoticz/in:
{"command": "switchlight", "idx": 10, "switchcmd": "Off"}
{"command": "switchlight", "idx": 10, "switchcmd": "On"}
{"command": "getdeviceinfo", "idx": 8 }

Al this will give me the correct action, this having said I get my readings or a switch will be switched. Concluding this link is working as well.
Next is my openHAB config. I have created an item to test postings on my mqtt:
Switch<>Dishwasher<---->"Switch 1"<----><bedroom><----->{ mqtt=">[localbroker:domoticz/in:command:on:0],>[localbroker:domoticz/in:command:off:default]" }

Result of above switch is:
  --> PUBLISH received, TOPIC:domoticz/in, QoS:0, Retained:false
                        DATA:0
  --> PUBLISH received, TOPIC:domoticz/in, QoS:0, Retained:false
                        DATA:OFF


So I can conclude that my link from openHAB to mqtt is working as well. However to be able to switch the switch (or do whatever I want I need to send JSON formatted data to mqtt to be able to do something on my Domoticz install (currently Domoticz says: Invalid...

What I would like to send is:
  --> PUBLISH sent,     TOPIC:domoticz/in, QoS:0, Retained:false
                        DATA:{"command": "switchlight", "idx": 10, "switchcmd": "Off"}
  --> PUBLISH received, TOPIC:domoticz/in, QoS:0, Retained:false
                        DATA:{"command": "switchlight", "idx": 10, "switchcmd": "Off"}


However I have no idea how to accomplish this. I have found the transform plugin for openhab, but this does the translation from mqtt to openhab, while I need the other way around.
I am probably missing something here, but I cannot figure out myself.

Thank you all for your advice and tips.

K.r.

Matt Callow

unread,
Sep 1, 2015, 3:10:32 AM9/1/15
to ope...@googlegroups.com
I think you can do this with a map.

With this in your items file

 { mqtt=">[localbrokert:domoticz/in:*:MAP(test.map)]" }

And this in transform/test.map

ON={"command": "switchlight", "idx": 10, "switchcmd": "On"}
OFF={"command": "switchlight", "idx": 10, "switchcmd": "On"}

Matt



--
You received this message because you are subscribed to the Google Groups "openhab" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openhab+u...@googlegroups.com.
To post to this group, send email to ope...@googlegroups.com.
Visit this group at http://groups.google.com/group/openhab.
To view this discussion on the web visit https://groups.google.com/d/msgid/openhab/c8c34bef-78db-4034-a28f-f49426e2dc6a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sebastiaan Schlepers

unread,
Sep 1, 2015, 4:26:49 AM9/1/15
to openhab
Matt, thanks a lot. I needed to make a small adjustment:

mqtt.items
Switch<>Dishwasher<---->"Switch 1"<----><bedroom><----->{ mqtt=">[localbroker:domoticz/in:command:*:MAP(test.map)],<[localbroker:/domoticz/out:state:default]"}

test.map
ON={"command": "switchlight", "idx": 10, "switchcmd": "On"}
OFF={"command": "switchlight", "idx": 10, "switchcmd": "Off"}


The above piece is working excellent in switching ON and OFF, however now diving into the opposite direction in getting thee status update (switching is done from other software). Update is posted by Domoticz.

Can I re-use/add code into the test.map or do I need to create for every switch I have a different map file. And a map file added for the mqtt status updates?
Do you have by any chance an example concerning reading JSON from mqtt?

Bas

Op dinsdag 1 september 2015 09:10:32 UTC+2 schreef Matt Callow:

Matt Callow

unread,
Sep 1, 2015, 4:52:24 AM9/1/15
to ope...@googlegroups.com

Sorry a couple of typos in my previous mail. Looks like you found them.
I'm no expert buy as far as I know you would need a separate map file per switch.
For the status updates take a look at the jsonpath example in the wiki
https://github.com/openhab/openhab/wiki/Transformations

Matt

Sebastiaan Schlepers

unread,
Sep 1, 2015, 7:28:22 AM9/1/15
to openhab
Correct, it was enough to put me in the right direction. Thank again for that.
Still working fine.

At the moment I am still looking for the right way to extract the JSON message.
I have already experimented with the data coming from Domoticz in online testers for JSON.
If I do it with a rule or alike (map file), then I need to be able to work with the JSON string.

Problem I am running in at the moment is that I have no clue how to get that string. Might be that there are better ways, but I do not have them atm.

Hope someone else can assist me with the status part.

Op dinsdag 1 september 2015 10:52:24 UTC+2 schreef Matt Callow:

Sebastiaan Schlepers

unread,
Sep 1, 2015, 10:10:31 AM9/1/15
to openhab
Okay, after a day experimenting I have created something which works, but still would like to hear if there are better methods.
At this moment I created an string item which receives all status changes from Domoticz. It checks in a rule if this is a change applicable for a switch or whatever item. Then if correct it check the status (ON or OFF for example).
Then it updates the state of the item. This way you can handle the switch by openhab, but also hardcore and of course by domoticz...

Rule:
import org.openhab.core.library.types.DecimalType

rule "Pump01 Status"
when
    Item StringTest received update
then
//Kitchen_Coffee_Watts.value ="Test"
var String json = StringTest.state.toString
var test = transform("JSONPATH","$.idx",json)
if test=="10" then
{
var test = (transform("JSONPATH","$.nvalue",json)
if test=="1" then
{
postUpdate(Dishwasher, ON)
}
else
{
postUpdate(Dishwasher, OFF)
}
}
//Dishwasher
end

Item:
Switch<>Dishwasher<---->"Switch 1"<----><bedroom><----->{ mqtt=">[localbroker:domoticz/in:command:*:MAP(test.map)]" }
String<>StringTest<---->(mqtt)<><------>{ mqtt="<[localbroker:domoticz/out:state:default]" }

Again: I am open for suggestions...

Op dinsdag 1 september 2015 13:28:22 UTC+2 schreef Sebastiaan Schlepers:
Reply all
Reply to author
Forward
0 new messages