I just went through a similar issue so documenting my results in this thread. MQTT sensors are only updated in home assistant when their associated topic is updated. In other words:
- The sensor’s state is only updated when the
state_topic
is updated on the MQTT server - The sensor’s attributes are only updated when the
json_attributes_topic
is updated on the MQTT server - The sensor’s availability is only updated when the
availability_topic
is updated on the MQTT server - etc…
If you need multiple attributes, the best case scenario is that you have them all fed into a single MQTT topic in JSON format. You can’t collect information from different MQTT topics beyond what was mentioned above by @123 . However you can have templates reference whatever home assistant sensor or attribute you want. So if you have one home assistant sensor created for each MQTT topic, and you want to add a new sensor which contains all those other sensor states as attributes in your new “master” sensor, you could use something like the following:
- platform: mqtt
name: "master sensor"
state_topic: "test/sensor1"
json_attributes_topic: "test/sensor1/randomattribute"
json_attributes_template: >
{
"attribute1":"{{ states(/"sensor.mymqttsensor1/") }}",
"attribute2":"{{ states(/"sensor.mymqttsensor2/") }}"
}
This is all fine and dandy, except for what I wrote at the beginning of this reply, which is that the attributes won’t update unless there’s an update to the MQTT topic that the particular portion of the sensor is driven by. So state changes of the two sensors used in the template above won’t be reflected in the master sensor attributes until an update is posted to the MQTT topic defined by json_attributes_topic
which is test/sensor1/randomattribute
. This applies even though the value from that randomattribute topic isn’t used anywhere in the template.
A workaround could be devised by creating an automation which uses service: mqtt.publish
to post an update to that specific MQTT topic to force the attributes to update. Or you could instead use an automation to create a JSON payload to the json_attributes_topic
topic, which really just moves the complicated template out of your MQTT sensor config and into your automation. But I think the latter is the cleaner way to go.