mqtt autodiscovery programmers guide
Moderator: leecollings
-
- Posts: 386
- Joined: Friday 17 August 2018 14:03
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
mqtt autodiscovery programmers guide
I build my own ESP8266 solution for managing my heating using this library: http://ihormelnyk.com/opentherm_library
made a python plugin which retrieves the Opentherm values every 10 sec and then converts them to domoticz devices (and with http commands to control the heating)
but actually i don't like this 10 sec heartbeat, so would like to start using mqtt to update realtime when needed instead of this polling mechanism. using normaal mqtt updates commands with manually devices created in domoticz is quite easy (did a test). But i would like to use the autodiscovery protocol, so the devices are created automatically
where can i find info on the autodiscovery and how to program this?
made a python plugin which retrieves the Opentherm values every 10 sec and then converts them to domoticz devices (and with http commands to control the heating)
but actually i don't like this 10 sec heartbeat, so would like to start using mqtt to update realtime when needed instead of this polling mechanism. using normaal mqtt updates commands with manually devices created in domoticz is quite easy (did a test). But i would like to use the autodiscovery protocol, so the devices are created automatically
where can i find info on the autodiscovery and how to program this?
- waltervl
- Posts: 5904
- Joined: Monday 28 January 2019 18:48
- Target OS: Linux
- Domoticz version: 2024.7
- Location: NL
- Contact:
Re: mqtt autodiscovery programmers guide
In the home assistant documentation. A link to this HA documentation is in the Domoticz MQTT wiki. But to make it easy It is here https://www.home-assistant.io/integrati ... -discovery
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
- waltervl
- Posts: 5904
- Joined: Monday 28 January 2019 18:48
- Target OS: Linux
- Domoticz version: 2024.7
- Location: NL
- Contact:
Re: mqtt autodiscovery programmers guide
By the way, there is a OpenTherm mqtt Autodiscover project here https://github.com/rvdbreemen/OTGW-firmware
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
-
- Posts: 386
- Joined: Friday 17 August 2018 14:03
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: mqtt autodiscovery programmers guide
Aha tx just what i was looking forwaltervl wrote: ↑Friday 25 November 2022 18:14 In the home assistant documentation. A link to this HA documentation is in the Domoticz MQTT wiki. But to make it easy It is here https://www.home-assistant.io/integrati ... -discovery
-
- Posts: 386
- Joined: Friday 17 August 2018 14:03
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: mqtt autodiscovery programmers guide
Tx for the hint. But this opentherm gateway. I am using other hardware..waltervl wrote: ↑Friday 25 November 2022 18:27 By the way, there is a OpenTherm mqtt Autodiscover project here https://github.com/rvdbreemen/OTGW-firmware
-
- Posts: 386
- Joined: Friday 17 August 2018 14:03
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: mqtt autodiscovery programmers guide
tx for the hints here. Already much further. can create mqtt messages for switches and sensors. However i struggle to get one for a setpoint
does anyone have sample mqtt payloads for the autodiscovery mqtt message which creates a setpoint device?
Most ideally a "mosquitto_pub full commandline to create a setpoint device
e.g. how should i change the one belew so it creates a setpoint instead of switch:
does anyone have sample mqtt payloads for the autodiscovery mqtt message which creates a setpoint device?
Most ideally a "mosquitto_pub full commandline to create a setpoint device
e.g. how should i change the one belew so it creates a setpoint instead of switch:
Code: Select all
mosquitto_pub -r -h 127.0.0.1 -p 1883 -u <user> -P "<pw>" -t homeassistant/light/kitchen/config -m '{ "~": "homeassistant/light/kitchen", "name": "Kitchen", "unique_id": "kitchen_light", "cmd_t": "~/set", "stat_t": "~/state", "schema": "json", "brightness": true }
- waltervl
- Posts: 5904
- Joined: Monday 28 January 2019 18:48
- Target OS: Linux
- Domoticz version: 2024.7
- Location: NL
- Contact:
Re: mqtt autodiscovery programmers guide
As indicated check the otgw as example, there are some setpoints examples in it.
https://github.com/rvdbreemen/OTGW-firm ... mqttha.cfg
https://github.com/rvdbreemen/OTGW-firm ... mqttha.cfg
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
-
- Posts: 386
- Joined: Friday 17 August 2018 14:03
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: mqtt autodiscovery programmers guide
a check tx, so it should be something like this:
will try .. tx!
Code: Select all
%homeassistant%/sensor/%node_id%/TrSet/config ; {"avty_t": "%mqtt_pub_topic%", "dev": {"identifiers": "%node_id%", "manufacturer": "Schelte Bron", "model": "otgw-nodo", "name": "OpenTherm Gateway (%hostname%)", "sw_version": "%version%"}, "uniq_id": "%node_id%-TrSet", "device_class": "temperature", "name": "%hostname%_Room_setpoint", "stat_t": "%mqtt_pub_topic%/TrSet", "unit_of_measurement": "°C", "value_template": "{{ value }}", "state_class" : "measurement" }
-
- Posts: 386
- Joined: Friday 17 August 2018 14:03
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: mqtt autodiscovery programmers guide
now the next issue: domoticz logs it creates new devices. However they are not created anymore.
e.g.
results in a what seems to be a correct device
however no device kitchen3 or kichten_light_3 is created... (and yes.. accept new sensors is switched on)
how can i troubleshoot?
e.g.
Code: Select all
mosquitto_pub -r -h 127.0.0.1 -p 1883 -u <user> -P "<pwd>" -t homeassistant/light/kitchen3/config -m '{ "~": "homeassistant/light/kitchen3", "name": "Kitchen3", "unique_id": "kitchen_light_3", "cmd_t": "~/set", "stat_t": "~/state", "schema": "json", "brightness": true }'
Code: Select all
2022-11-29 12:59:39.465 Status: zwave2mqtt: discovered: kitchen_light_3/Kitchen3 (unique_id: kitchen_light_3)
how can i troubleshoot?
- waltervl
- Posts: 5904
- Joined: Monday 28 January 2019 18:48
- Target OS: Linux
- Domoticz version: 2024.7
- Location: NL
- Contact:
Re: mqtt autodiscovery programmers guide
I would switch on debugging in Domoticz if this is all logging you get.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
-
- Posts: 386
- Joined: Friday 17 August 2018 14:03
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: mqtt autodiscovery programmers guide
this is weird.. restarted domoticz with loglevel all. and now the device is created

tx for the tip anyway..
-
- Posts: 386
- Joined: Friday 17 August 2018 14:03
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: mqtt autodiscovery programmers guide
Steadily progressing. My devices are now autodiscovered. Works great.
Have another question i cannot find in mentioned autodiscovery docs above.
I programmed my ESP to send the discovery config message on (re)connect to MQTT and the update messages whenever a states changes. Works perfect, i can see realtime the values of my boiler in domoticz... and i can control the boiler setpoint, heating, hotwater, etc... using these autodiscovered devices.
That is: Until I restart domoticz. I then get a lot of "invalid/unhandled data received messages". which is fixed when i force the discovery mqtt messages being sent again (which results in domoticz recognizing the messages again and are mapped to the correct devices).
For sensors a fix could be to always send the disovery message before the value is sent to domoticz. However this will not fix the setpoints or the used switches. Whatever interval i choose to regularly send discovery messages: These will be unusable until the next discovery interval.
Is there some way domoticz can cache this info? If not: what is the best practice for when and how often the discovery messages should be sent?
Have another question i cannot find in mentioned autodiscovery docs above.
I programmed my ESP to send the discovery config message on (re)connect to MQTT and the update messages whenever a states changes. Works perfect, i can see realtime the values of my boiler in domoticz... and i can control the boiler setpoint, heating, hotwater, etc... using these autodiscovered devices.
That is: Until I restart domoticz. I then get a lot of "invalid/unhandled data received messages". which is fixed when i force the discovery mqtt messages being sent again (which results in domoticz recognizing the messages again and are mapped to the correct devices).
For sensors a fix could be to always send the disovery message before the value is sent to domoticz. However this will not fix the setpoints or the used switches. Whatever interval i choose to regularly send discovery messages: These will be unusable until the next discovery interval.
Is there some way domoticz can cache this info? If not: what is the best practice for when and how often the discovery messages should be sent?
- waltervl
- Posts: 5904
- Joined: Monday 28 January 2019 18:48
- Target OS: Linux
- Domoticz version: 2024.7
- Location: NL
- Contact:
Re: mqtt autodiscovery programmers guide
I believe this can be solved by setting retained mqtt messages on. Downside is that after deleting of the device and not deleting the retained mqtt message you get the deleted device back in Domoticz 

Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
-
- Posts: 386
- Joined: Friday 17 August 2018 14:03
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: mqtt autodiscovery programmers guide
The ha docs specifically State not to use retained mesaages for this Reason. Any other suggestions?
UPDATE: The HA docs seem to contradict themselves. Now also found this link: https://github.com/home-assistant/home- ... y.markdown, stating
where i read somewhere else this is bad practive (if you delete a device it will be rediscovered). Anyway not happy with it.Retain: The -r switch is added to retain the configuration topic in the broker. Without this, the sensor will not be available after Home Assistant restarts.
I will do some testing, see what works better: Retained messages or periodically publishing the discovery messages.
- waltervl
- Posts: 5904
- Joined: Monday 28 January 2019 18:48
- Target OS: Linux
- Domoticz version: 2024.7
- Location: NL
- Contact:
Re: mqtt autodiscovery programmers guide
I guess you won't delete a lot of devices. At least I don't.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
-
- Posts: 220
- Joined: Friday 04 November 2016 12:33
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta
- Location: France
- Contact:
Re: mqtt autodiscovery programmers guide
I Use this discovery method to automate domoticz devices creation.
Works fine for switch and light but for sensors, looks like a bit more complicated and undocumented.
For example when I try to create a gas counter (would like a P1 smart meter for example) with these config messages:
I get a General/Custom Sensor and not a gas counter.
(Note that a first state update is necessary to see the device in domoticz, not the case for switch or light).
This works if I use "energy" device_class.
Is there some documentation about device classes already implemented in domoticz ?
Thanks
Works fine for switch and light but for sensors, looks like a bit more complicated and undocumented.
For example when I try to create a gas counter (would like a P1 smart meter for example) with these config messages:
Code: Select all
Arduino/sensor/gas/config
{
"device_class": "gas",
"name": "gas",
"state_topic": "Arduino/sensor/gas",
"unique_id": "0x0015bc004f00d3ab_gas",
"unit_of_measurement": "m3"
}
(Note that a first state update is necessary to see the device in domoticz, not the case for switch or light).
This works if I use "energy" device_class.
Is there some documentation about device classes already implemented in domoticz ?
Thanks
- waltervl
- Posts: 5904
- Joined: Monday 28 January 2019 18:48
- Target OS: Linux
- Domoticz version: 2024.7
- Location: NL
- Contact:
Re: mqtt autodiscovery programmers guide
There is no documentation. Best to do some sniffing in the code https://github.com/domoticz/domoticz/bl ... scover.cpp
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
-
- Posts: 220
- Joined: Friday 04 November 2016 12:33
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta
- Location: France
- Contact:
Re: mqtt autodiscovery programmers guide
@waltervl Thanks for the link
-
- Posts: 386
- Joined: Friday 17 August 2018 14:03
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: mqtt autodiscovery programmers guide
tx for all the help. It is working better and better.
But i do have an additional question: I'd also like to have the opentherm faultcode of my boiler published to mqtt/domoticz. (use case is i can set alarms on it. E.g. send push notification when domoticz detects an error)
So íd like to have a device with just a number and no unit of measurement. According to HA docs this shoulld be possible ("A sensor is a read-only entity that provides some information. Information has a value and optionally, a unit of measurement.")
so i created using the following autodiscovery message:
The issue is that devices is created in domoticz, the value is also correctly sent to domoticz, however unit of measurement becomes "??" in domoticz... (instead of no unit of measurement)
I already tried adding
to the discovery message the result is the same (unit of measurement becomes "??" instead of having no unit of measurement)
any idea how to fix this?
But i do have an additional question: I'd also like to have the opentherm faultcode of my boiler published to mqtt/domoticz. (use case is i can set alarms on it. E.g. send push notification when domoticz detects an error)
So íd like to have a device with just a number and no unit of measurement. According to HA docs this shoulld be possible ("A sensor is a read-only entity that provides some information. Information has a value and optionally, a unit of measurement.")
so i created using the following autodiscovery message:
Code: Select all
{
"value_template": "{{ value_json.value }}",
"device_class": "None",
"state_topic": "domesphelper/sensor/Faultcode/state",
"json_attributes_topic": "domesphelper/sensor/Faultcode/state",
"name": "Faultcode",
"unique_id": "Faultcode"
}
I already tried adding
Code: Select all
"unit_of_measurement": "",
to the discovery message the result is the same (unit of measurement becomes "??" instead of having no unit of measurement)
any idea how to fix this?
-
- Posts: 220
- Joined: Friday 04 November 2016 12:33
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta
- Location: France
- Contact:
Re: mqtt autodiscovery programmers guide
I also needed such device. The best I found for 0-100 numbers is to setup a light with only brightness : "brightness_state_topic". No need to specify a device class
Who is online
Users browsing this forum: No registered users and 1 guest