hneel
(Hans)
December 13, 2021, 6:45pm
1
I’m developing an ESP8266 board with 2 temperature sensors. They use mqtt over wifi to connect to HA.
At first I defined the devices in the yaml files like this:
- platform: mqtt
name: "mqtt_dev0_temp1"
state_topic: "cv_temp/ECFABC5ECDBB/temp"
value_template: "{{ value_json.temp1 }}"
qos: 1
This worked fine.
But then I wanted to improve things and add MQTT device discovery.
Like here
And I just can’t get this right.
If I send a straightforward json like this one, it is seen by HA:
{
"name": "mqtt_ECFABC5ECD98_temp1",
"stat_t": "cv_temp/ECFABC5ECD98/temp",
"uniq_id": "cvtemp-ECFABC5ECD98-temp1",
"dev_cla": "temperature",
"unit_of_meas": "°C",
"val_tpl": "{{ value_json.temp1 }}"
}
But I want to add the proper device too. When I try something like this, HA won’t accept it.
{
"name": "mqtt_ECFABC5ECD98_temp1",
"stat_t": "cv_temp/ECFABC5ECD98/temp",
"uniq_id": "cvtemp-ECFABC5ECD98-temp1",
"dev_cla": "temperature",
"unit_of_meas": "°C",
"val_tpl": "{{ value_json.temp1 }}",
"dev": {
"identifiers": "cvtemp-ECFABC5ECD98",
"manufacturer": "Me",
"model": "model",
"name": "CV_Temp_module",
"sw_version": "1.0"
}
}
What am I doing wrong here?
francisp
(Francis)
December 14, 2021, 5:36am
2
try
{
"name": "mqtt_ECFABC5ECD98_temp1",
"stat_t": "cv_temp/ECFABC5ECD98/temp",
"uniq_id": "cvtemp-ECFABC5ECD98-temp1",
"dev_cla": "temperature",
"unit_of_meas": "°C",
"val_tpl": "{{ value_json.temp1 }}",
"dev": {
"identifiers": ["cvtemp-ECFABC5ECD98"],
"manufacturer": "Me",
"model": "model",
"name": "CV_Temp_module",
"sw_version": "1.0"
}
}
hneel
(Hans)
December 15, 2021, 8:43pm
3
Thanks for your suggestion. I’ve changed it a bit and the serializeJsonPretty commands now prints it as:
{
"name": "mqtt_cvtemp_ECFABC5ECD98X_temp1",
"stat_t": "cv_temp/ECFABC5ECD98X/stat",
"uniq_id": "cvtemp-ECFABC5ECD98X-temp1",
"dev_cla": "temperature",
"unit_of_meas": "°C",
"val_tpl": "{{ value_json.temp1 }}",
"dev": {
"identifiers": [
"cvtemp-ECFABC5ECD98X"
],
"manufacturer": "Me",
"model": "model",
"name": "CV_Temp_module",
"sw_version": "1.0"
}
}
That still doesn’t work however.
hneel
(Hans)
December 15, 2021, 9:57pm
4
Never mind. The problem was somewhere else. pubsubclient publish was limited to 256 bytes and my message was longer.
2 Likes
solmoller
(Henrik Møller Jørgensen)
January 23, 2022, 10:55am
5
Had the exact same problem! Off to see where to increase message sizes
hneel
(Hans)
January 26, 2022, 7:35pm
6
Basically it’s making use of beginPublish and endPublish instead of just publish functions of mqtt client.
See my sketch:
covert
(covert)
April 2, 2023, 7:00am
8
Thanks for this. Spent why more time than I wanted to try to get this working and your code was the answer.
[edit] This was the simple solution
mqttPubSubObject.setBufferSize(1024);
i add device sucess but not online
message:
{
"command_topic":"homeassistant/light/moorgen-light/switch",
"state_topic":"homeassistant/light/moorgen-light/state",
"brightness_command_topic":"homeassistant/light/moorgen-light/brightness_set",
"brightness_state_topic":"homeassistant/light/moorgen-light/brightness_status",
"brightness_scale":100,
"name":"moorgen-light",
"unique_id":"moorgen-light",
"brightness_value_template":"{{value_json.brightness}}",
"device":
{
"model":"model",
"hw_version":"1",
"sw_version":"1",
"manufacturer":"moorgen",
"identifiers":"moorgen"
},
"retain":true,
"availability": [
{
"payload_available": "online",
"payload_not_available": "offline",
"topic": "homeassistant/light/moorgen-light/online"
}
],
"payload_available": "online",
"availability_mode":"any"
}
Thanks ,i sovle it
{
"~": "homeassistant/light/moorgen",
"name": "moorgen",
"uniq_id": "moorgen",
"cmd_t": "~/set",
"stat_t": "~/state",
"availability":
[
{
"topic":"~/online",
"value_template": "{{ value_json.state }}"
}
],
"schema": "json",
"brightness": true,
"brightness_scale":100,
"retain":true,
"optimistic":false,
"device":
{
"name":"moorgen",
"model":"moorgen",
"manufacturer":"moorgen",
"identifiers":"moorgen"
}
}