Page 1 of 2
mqtt autodiscovery programmers guide
Posted: Friday 25 November 2022 18:02
by akamming
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?
Re: mqtt autodiscovery programmers guide
Posted: Friday 25 November 2022 18:14
by waltervl
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
Re: mqtt autodiscovery programmers guide
Posted: Friday 25 November 2022 18:27
by waltervl
By the way, there is a OpenTherm mqtt Autodiscover project here
https://github.com/rvdbreemen/OTGW-firmware
Re: mqtt autodiscovery programmers guide
Posted: Friday 25 November 2022 18:43
by akamming
Aha tx just what i was looking for
Re: mqtt autodiscovery programmers guide
Posted: Friday 25 November 2022 18:44
by akamming
Tx for the hint. But this opentherm gateway. I am using other hardware..
Re: mqtt autodiscovery programmers guide
Posted: Monday 28 November 2022 15:14
by akamming
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:
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 }
Re: mqtt autodiscovery programmers guide
Posted: Monday 28 November 2022 15:20
by waltervl
As indicated check the otgw as example, there are some setpoints examples in it.
https://github.com/rvdbreemen/OTGW-firm ... mqttha.cfg
Re: mqtt autodiscovery programmers guide
Posted: Monday 28 November 2022 15:38
by akamming
a check tx, so it should be something like this:
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" }
will try .. tx!
Re: mqtt autodiscovery programmers guide
Posted: Tuesday 29 November 2022 14:45
by akamming
now the next issue: domoticz logs it creates new devices. However they are not created anymore.
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 }'
results in a what seems to be a correct device
Code: Select all
2022-11-29 12:59:39.465 Status: zwave2mqtt: discovered: kitchen_light_3/Kitchen3 (unique_id: kitchen_light_3)
however no device kitchen3 or kichten_light_3 is created... (and yes.. accept new sensors is switched on)
how can i troubleshoot?
Re: mqtt autodiscovery programmers guide
Posted: Tuesday 29 November 2022 14:52
by waltervl
I would switch on debugging in Domoticz if this is all logging you get.
Re: mqtt autodiscovery programmers guide
Posted: Tuesday 29 November 2022 15:53
by akamming
waltervl wrote: ↑Tuesday 29 November 2022 14:52
I would switch on debugging in Domoticz if this is all logging you get.
this is weird.. restarted domoticz with loglevel all. and now the device is created

which is weird, cause the command is in a script and i ran the same script... gonna do some more testing where this weird behaviour comes from.
tx for the tip anyway..
Re: mqtt autodiscovery programmers guide
Posted: Tuesday 06 December 2022 10:50
by akamming
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?
Re: mqtt autodiscovery programmers guide
Posted: Tuesday 06 December 2022 16:56
by waltervl
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

Re: mqtt autodiscovery programmers guide
Posted: Tuesday 06 December 2022 20:34
by akamming
waltervl wrote: ↑Tuesday 06 December 2022 16:56
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
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
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.
where i read somewhere else this is bad practive (if you delete a device it will be rediscovered). Anyway not happy with it.
I will do some testing, see what works better: Retained messages or periodically publishing the discovery messages.
Re: mqtt autodiscovery programmers guide
Posted: Tuesday 06 December 2022 22:52
by waltervl
I guess you won't delete a lot of devices. At least I don't.
Re: mqtt autodiscovery programmers guide
Posted: Thursday 08 December 2022 9:43
by mgugu
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:
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"
}
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
Re: mqtt autodiscovery programmers guide
Posted: Thursday 08 December 2022 13:31
by waltervl
There is no documentation. Best to do some sniffing in the code
https://github.com/domoticz/domoticz/bl ... scover.cpp
Re: mqtt autodiscovery programmers guide
Posted: Thursday 08 December 2022 13:42
by mgugu
@waltervl Thanks for the link
Re: mqtt autodiscovery programmers guide
Posted: Monday 26 December 2022 8:11
by akamming
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:
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"
}
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?
Re: mqtt autodiscovery programmers guide
Posted: Monday 26 December 2022 10:42
by mgugu
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