MQTT Discovery - New sensor

I am trying get a sensor to work via MQTT Discovery. I have published MQTT messages (examples below) and HA seems to discover it, but the temperature is not updated. Do I need more fields populated?

Pasted from developer-tools/mqtt…

Message 8 received on homeassistant/sensor/inkbird_f8300232744d/config/temperature1 at 8:41 PM:
{ "name": "temperature1", "device_class": "temperature", "state_topic": "inkbird_f8300232744d/state", "unit_of_measurement": "°C", "value_template": "{{value_json.temperature}}" }
QoS: 0 - Retain: true

Message 21 received on inkbird_f8300232744d/state at 8:50 PM:
{ "temperature1": 6552.6, "temperature2": 6552.6, "temperature3": 6552.6, "temperature4": 6552.6, "temperature5": 6552.6, "temperature6": 23 }
QoS: 0 - Retain: false

If you publish this payload (formatted for clarity):

{
   "name":"temperature1",
   "device_class":"temperature",
   "state_topic":"inkbird_f8300232744d/state",
   "unit_of_measurement":"°C",
   "value_template":"{{value_json.temperature1}}"
}

to this topic:

homeassistant/sensor/inkbird_f8300232744d/config

MQTT Discovery will create this sensor:

sensor.temperature1

If you then publish this payload:

{ "temperature1": 6552.6, "temperature2": 6552.6, "temperature3": 6552.6, "temperature4": 6552.6, "temperature5": 6552.6, "temperature6": 23 }

to this topic:

inkbird_f8300232744d/state

the sensor’s value_template

{{value_json.temperature1}}

will extract the value 6552.6

Now check the value_template of the sensor you created. It uses:

value_json.temperature

instead of:

value_json.temperature1

1 Like

Ah, lovely, thanks. Will give it a go tomorrow.

Is there any way to have an array of temperatures, rather than having to publish a config message for each of the 6 temperatures on my device?

You can make all the temperatures appear as sensor attributes. Only the first temperature will be used to represent the sensor’s state.

Add the line containing json_attributes_topic as shown here:

{
   "name":"temperature1",
   "device_class":"temperature",
   "state_topic":"inkbird_f8300232744d/state",
   "json_attributes_topic": "inkbird_f8300232744d/state",
   "unit_of_measurement":"°C",
   "value_template":"{{value_json.temperature1}}"
}

The received temperatures will appear like this:

Screenshot from 2020-06-20 16-51-31

It’s also possible to have the entire raw JSON payload appear in the sensor’s state. However, an entity’s state is limited to storing 255 characters. If the received payload exceeds this length, none of it is stored. In contrast, there’s no length restriction for attributes.

2 Likes

Not quite there, I have published 2 (or more) configs and my entity in home assistant seems to be a combination of the first and last configs. For the config messages below, I get a single entity in HA with,
Name: inkbird_f8300232744d_temperature2
Entity ID: sensor.inkbird_f8300232744d_temperature1

Published to

homeassistant/sensor/inkbird_f8300232744d/config/temperature1

Payload

{
"name":"inkbird_f8300232744d_temperature1",
"device_class":"temperature",
"state_topic":"inkbird_f8300232744d/state",
"unit_of_measurement":"°C",
"unique_id":"inkbird_f8300232744d_temperature1",
"value_template":"{{value_json.temperature1}}"
}

Published to

homeassistant/sensor/inkbird_f8300232744d/config/temperature2

Payload

{
"name":"inkbird_f8300232744d_temperature2",
"device_class":"temperature",
"state_topic":"inkbird_f8300232744d/state",
"unit_of_measurement":"°C",
"unique_id":"inkbird_f8300232744d_temperature2",
"value_template":"{{value_json.temperature2}}"
}

Can you explain to me why you are appending temperature1 and temperature2 to the discovery topic?

That’s not how the documentation explains to structure the discovery topic nor the way I used it in my example.

The discovery topic need to follow a specific format:

<discovery_prefix>/<component>/[<node_id>/]<object_id>/config

Yes that worked. Apologies for my lack of attention to the detail. Thanks for all your help! :grinning:

1 Like