Testing OpenHAB with Domoticz via MQTT

I have a full working Domoticz solution with RFLink with light switches and temperature sensors, Life 360 and Gigaset Elements running on a RPi.

I would like to test OpenHAB to see if it’s something for me and wondering if I can sync Domoticz with OpenHAB via MQTT to see what it looks like and how it works. Want to try Google Home as myopenhab have support for it.

If it’s possible, how do I setup that? Maybe there is people here who have already done this?

Should be possible. Start here:

Okey, it’s up and running. Let’s see if it’s possible to make it even better.

  • Had Domoticz running on RPi at port 8080 (and 443)
  1. Installed Mosquitto and configured Domoticz to connect to it
  2. Installed zulu java for embedded as it wasn’t installed in the Domoticz image
  3. Installed OpenHAB and change port to 8081. After installation I had to wait 10-15 minutes before it was up and running and listening at port.
  4. Installed following OpenHAB addons:
  • MQTT binding
  • MQTT Actions
  • JSONPath, Map, Javascript and RegEx Transformation
  • openHAB Cloud Connector
  1. Configure MQTT binding
  2. Configured rules, items and javascript as below
  3. Configured openHAB Cloud Connector
  4. Now, I can control my domoticz devices with my Google Home and ask for temperature. If I want I can use the openHAB webinterface as they sync with each other. I can do stuff via myopenHAB if I want to. And everything without open any ports in my firewall.
    I will only add domoticz items in openHAB that I think is secure to let Google Home to have control over.

So this is how I solved it and it works for me. I would prefer to not enter idx number two times on same row in items-file, but I couldn’t solve it any other way.

Items:

Switch idx123 "Outdoorlamp" (gLight) [ "Lighting" ] {mqtt="<[mqttBroker:domoticz/out:state:JS(get_state.js):(?s).*idx\".?.?.?123,.*]"}
Number Outdoor_Temperature "Temperature [%.1f]" <temperature> ["CurrentTemperature"] {mqtt="<[mqttBroker:domoticz/out:state:JSONPATH($.svalue1):(?s).*idx\".?.?.?124,.*]"}

123 and 124 is device idx in Domoticz. mqttBroker is what I configured the mqtt-connection to be called in mqtt binding.

Rules:

rule "Send MQTT message on Light command"
when
	Member of gLight received command
then
	var idxStartPos = triggeringItem.name.indexOf("idx") + 3
	var idxStopPos = triggeringItem.name.length()
	var  myString = ""

	if (triggeringItem.state.toString() == "ON" || triggeringItem.state.toString() == "OFF")
	{
		var cmd = triggeringItem.state.toString().toLowerCase.toFirstUpper()
		// {"command": "switchlight", "idx": 2450, "switchcmd": "On" }
		myString = '{\"command\": \"switchlight\", \"idx\": ' + triggeringItem.name.substring(idxStartPos,idxStopPos) + ', \"switchcmd\": \"' + cmd + '\"}'
	}
	else
	{
		// {"command": "switchlight", "idx": 2450, "switchcmd": "Set Level", "level": 100 }
		myString = '{\"command\": \"switchlight\", \"idx\": ' + triggeringItem.name.substring(idxStartPos,idxStopPos) + ', \"switchcmd\": \"Set Level\", \"level\": ' + triggeringItem.state.toString() + '}'
	}

	publish("mqttBroker", "domoticz/in", myString)
end

get_state.js

(function(i){
    var parsed = JSON.parse(i);
    if (parsed.nvalue == 0)
    {
        return "OFF"
    }
    else if (parsed.nvalue == 1)
    {
        return "ON"
    }
    else if (parsed.nvalue == 2)
    {
        return parsed.svalue1
    }
})(input);

Hi,

I’m very interested in this approach and tried it, but I can’t make it work…
I installed openhabian on a separate PI. I installed MQTT on that PI as well and configured Domoticz to update that MQTT broker. That seems working, because when I use a tool like mqtt-spy-1.0.0.jar I can indeed view domoticz messages coming out.
I tried to follow the guidelines from this thread, but I can’t see anything in Openhab… So I probably made a mistake somewhere.
First, I installed openhab2.4, the latest version… I initially installed the new MQTT binding. As I couldn’t get any success with the procedure, I uinstalled (using PaperUI) that binding and used PaperUI again to install the “MQTT Binindg 1.x”
-> now, I get an error in Openhab, stating that the Openhab Thing “MQTT Broker” is “uninitialized” (even after having rebooted the PI) : Is it because I only used the PaperUI to install the binding? Do I need to create a configuration file manually ? In a openhab tutorial for MQTT 1.x, I could read that a file mqtt.cfg has to be created or modified manually in /services folder : is it something to be done though not described in the step by step guide of this thread ?
I read in an openhab doculentation (MQTT Things and Channels - Bindings | openHAB) that some files in .things are required, like the demo1.things described in that page. Is it something to be done and is it not overlapping with the .items definitions described by Ankan ?

-> Another question is how to understand naming strategy :

Where should I setup “idx123” ? Is it Domoticz, but then we lose a full description ? Or in openhabian, but then I don’t understand where to define that… Are they created and named automatically thanks to the .items file suggested by Ankan ? The first item in the file from Ankan starts with “idx123”, but not the second line (for outdoortemperature), so I feel lost : does it mean that naming system is constrained (with need to use idx+number) only for devices subject to “2 ways updates” (ie from domoticz to openhabian and from openhabian to domoticz) ?

-> also, as I like understanding (at least trying), could someone explain to me the syntax of the jsonpath:

JSONPATH($.svalue1):(?s).idx".?.?.?124,.
I understand it looks for svalue1, but I don’t understand the remaining structure (the ?s, and the multiple dots ?.?.?124 ). I tried to look at jsonpath syntax, but could not find such example

→ Last but not least : despite this work was done using MQTT Binding 1.x, do you think it will work with the new MQTT Binding 2 for openhab 2.4 ?

I’d highly appreciate if anyone could help me. This is driving me nuts ! :shock:

br,
Ricorico94

I can only answer your last question. It should be possible to use the MQTT2 binding with an adapted approach of connecting two openhab instances. I use this to connect openhab to nodered over mqtt.
You would have to add two publish channels to your mqtt broker thing, one for incoming and one for outgoing messages. And than add rules like this but adapted to your topics.:

rule "Publish all"
when 
      Member of OutItems changed
then
   val actions = getActions("mqtt","mqtt:broker:myunsecureBroker")
   actions.publishMQTT("allItemsout/"+triggeringItem.name,triggeringItem.state.toString)
end

rule "Receive all"
when 
      Channel "mqtt:broker:myunseciureBroker:TriggerIn" triggered
then 
    val parts1 = receivedEvent.toString.split("/").get(1)
    val parts2 = parts1.split("#")
    sendCommand(parts2.get(0), parts2.get(1))
end

I think you would have to use the 2.5M1 build for all the transformations in the mqtt2 binding to work.

Hi!

You are right. You have to change that mqtt.cfg file to get mqtt 1.x client to work. Even if mqtt server is on same machine openhab have no information about that. That is why you have to setup that mqtt binding.

The name format does only affect the name on openhab. And only the name (identity). The displayed name can be different.
Idx123 is a switch that you want to control from openhab. That is why the name is so important so the outgoing mqtt message to domoticz will be right.
The second device is a temperature sensor on domoticz that you want to show in openhab, and that’s it. You can’t charge temperature. It’s a one way device, so the name is not important. Only the incoming mqtt message filter in the end of that row is important wher you tell it to listen to messages from idx124 for temperature.

That expression is a regex expression to filter messages with only that idx.

Explanation from regex101.com

(?s) match the remainder of the pattern with the following effective flags: gms
s modifier: single line. Dot matches newline characters
.* matches any character 
* Quantifier — Matches between  zero and unlimited times, as many times as possible, giving back as needed (greedy)
idx matches the characters idx literally (case sensitive)
\" matches the character " literally (case sensitive)
.? matches any character 
? Quantifier — Matches between  zero and one times, as many times as possible, giving back as needed (greedy)
.? matches any character 
? Quantifier — Matches between  zero and one times, as many times as possible, giving back as needed (greedy)
.? matches any character 
? Quantifier — Matches between  zero and one times, as many times as possible, giving back as needed (greedy)
124, matches the characters 124, literally (case sensitive)
.* matches any character 
* Quantifier — Matches between  zero and unlimited times, as many times as possible, giving back as needed

Hi Ankan,

Thanks a lot for your explanations. At least, now, I understand the structure of your MQTT action with the JSONpath and the regex to filter !

After 4 hours of investigation (tests, retests, reboots, readings and almost despair), I finally made it work. I’ve not yet tried the steps with the Cloud, etc., but at least I get reflected devices between domoticz and openhab…
What helped me, if it can help others…

broker.url=tcp://localhost:1883
broker.user=openhabian
broker.pwd=abcdefg

but in Ankan’s examples, he uses another name:

mqttBroker.url=tcp://localhost:1883
mqttBroker.user=openhabian
mqttBroker.pwd=abcdefg

as long as I kept the default, the domoticz devices were not detected by openhab… and once I made the change, it worked…
And I realized that Ankan had briefly mentioned that fact, but I had not understood the importance of that short statement…
The weird thing is that the test switch created as per tutorial mentioned above is still working despite its definition in items is:

Code: Select all

Switch MQTT_Test "Testing..." { mqtt="<[broker:testing/mqtt/topic:state:default], >[broker:testing/mqtt/back-topic:command:*:default]" }

thus using “broker” name instead of “mqttBroker”… I don’t know why this one is still working.

So, Ankan, thanks a lot !!! I hope I’ll face less issues with the next steps. :roll: I must admit that I find openHab far more obscure and complex than Domoticz.

As a recap for newcomers to openhab and MQTT like me, here are a couple links I found interesting to duplicate the trick from Ankan:

Hi JGKK,

This is a good news. Now that I can make it work with Binding 1.x and that I start understanding the mechanisms, I’ll try investigate further and see if we can use your suggestion. I must admit I’d have a long way to go, as I’m not sure I fully understand what you mean with the 2 channels : my first “guess” is that it would relate to channels to be created by clicking on the Broker Thing (creating one for the Out and on for the In directions, but I feel confused by vocabulary as “publish” sounds to me “To” MQTT and not “In”… so Im afraid I’ll have to read many things before fully understanding… and unfortunately I consider Openhab more complex to understand than Domoticz for instance.
Another thing that I wonder is whetehr or not a kind of rule could be created to check if a MQTT message relates to a device which would not be existng already in Openhab so that that rule also reates it automatically. Target being haveing an openhab which would be fully synchronized with Domoticz without having to update manually the .items file…

Thanks again, and I’ll look at it in future!