Page 1 of 1

Domoticz MQTT connection using Homie Convention

Posted: Sunday 24 November 2019 21:09
by jorgh
Hi All,

I've created a Python script that publishes Domoticz devices to MQTT using the Homie Convention (https://github.com/homieiot/convention).
Although it's still a raw 1st version, it seems to be running stable on my own install. There are probably devices in the wild that are not supported at this time. These devices will be reported on startup and excluded for MQTT publishing. Currently omong others, switches, dimmers, temperature, wind, uv, ozone, illuminance and thermostats are working. Adding additional devices should not be that difficult, due to the nature of the script.

It both publishes the states of the devices and makes them controllable through MQTT.

I've chosen the Homie convention, as this seems to be an effort to create an independent standard for MQTT IoT devices including support for auto discovery and configuration of exposed capabilities.

I'll attach the script to this post, so others can give it a go, and comment/adapt/improve the script.

Just a quick head-start: if you want to change the state of a device, you need to publish a message to the 'set' topic. (It's best to read the Homie convention for all the details.)

So for a switch published at:
homie/domoticz/1/switch - publishes the state (either On or Off)
To control the switch send On or Off to:
/homie/domoticz/1/switch/set

Oh, before I forget: Domoticz should be configured with the MQTT Client Gateway with LAN interface posting Domoticz messages using domoticz/out (flat format).

25/11/2019 UPDATE: Uploaded a new version, since the scipt failed to run on Linux due to a missing encoding definition.
On the RPi I needed to add the following libraries using pip3, additional libraries may be needed, as I've not tried on a clean install.

pip3 install requests
pip3 install paho-mqtt

Kind regards,

Jorg

Re: Domoticz MQTT connection using Homie Convention

Posted: Tuesday 04 February 2020 22:59
by xAPPO
Just wanted to let people know I have just released (beta) an application for the Hubitat Elevation hub that will import devices from homie3 - so it should work with this.

https://community.hubitat.com/t/beta-mqtt-app/32750

It also will advertise HE's own devices using both homie3 and HA MQTT Discovery protocol so you can link HE devices into apps that support either of those protocols..

Feedback and suggestions welcome..

Kevin

Re: Domoticz MQTT connection using Homie Convention

Posted: Wednesday 05 February 2020 17:57
by FireWizard
Hi,

Thank you for sharing this.

I did a test with the script on my test system.

Domoticz running on Raspbian Buster

Version: 4.11670
Build Hash: f6af0fa0c
Compile Date: 2020-02-02 12:21:53
dzVents Version: 2.5.7
Python Version: 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.2.0]

Currently this system runs only the "Motherboard sensors" and a single Switch(Test). Totaly 7 devices.
Device 2 (Temperature) and 7 (Switch) are published. The others are currently Unsupported.

Beside that, the script gives also some errors.

If I issue the command: ./DomMQTTHomie.py --domoticz http://192.168.10.51:8080 --mqtt 192.168.10.51
it returns the following:

Code: Select all

Unknown/Unsupported device type, not publishing: Raspberry Pi 1 CPU_Usage [1]
Unknown/Unsupported device type, not publishing: Raspberry Pi 1 Memory Usage [3]
Unknown/Unsupported device type, not publishing: Raspberry Pi 1 HDD /boot [4]
Unknown/Unsupported device type, not publishing: Raspberry Pi 1 HDD / [5]
Unknown/Unsupported device type, not publishing: Raspberry Pi 1 Process Usage [6]
Traceback (most recent call last):
  File "./DomMQTTHomie.py", line 492, in <module>
    objMQTTClient.loop_forever()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1481, in loop_forever
    rc = self.loop(timeout, max_packets)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1003, in loop
    rc = self.loop_read(max_packets)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1284, in loop_read
    rc = self._packet_read()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1849, in _packet_read
    rc = self._packet_handle()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2305, in _packet_handle
    return self._handle_publish()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2500, in _handle_publish
    self._handle_on_message(message)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2647, in _handle_on_message
    self.on_message(self, self._userdata, message)
  File "./DomMQTTHomie.py", line 394, in on_message
    DecodeDomoticzMessage(str(msg.payload, encoding='utf-8'))
TypeError: str() takes at most 1 argument (2 given)
If I issue the command: ./DomMQTTHomie.py --domoticz http://localhost:8080 --mqtt localhost, which points to the same IP address,
it returns the following:

Code: Select all

Traceback (most recent call last):
  File "./DomMQTTHomie.py", line 454, in <module>
    domoticzresponse = json.loads(result.text)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
Perhaps you have an idea?

No clue what the debug parameter has to be.

Regards