Binary sensor - attributes from MQTT

Struggling a bit googling this.

Is there a way I can get attributes on a mqtt binary sensor from MQTT? e.g. I have my alarm setup to publish the sensors to MQTT - some of the information punlished also includes battery levels, etc. - it would be great if I can have the normal MQTT Binary Sensor, and then use some of the published information as attributes.

Here’s an example - the top MQTT topic is the one I use for the binary sensor - I’d like to use the other highlighted topics as attributes of that binary sensor.

Yes you can. But the screenshot is insufficient to tell you anything more. Go to developer tools ->states and show an output of one of your sensors.

The sensor state currently only has on or off - I want to add the above as attributes?

As far as I know there is no simple way to achieve what you want.

MQTT Binary Sensor uses json_attributes_topic and json_attributes_template to incorporate additional data into its attributes property.

The hurdle you must overcome is the constraint imposed by json_attributes_topic. It is designed to subscribe to a single MQTT topic and acquire all attribute values from the topic’s payload (and use json_attributes_template to parse the payload). You are using multiple MQTT topics where each topic’s payload contains a single attribute value.

You need to devise a means of combining the payloads of your multiple topics into a single topic containing one payload (a JSON dictionary). Effectively, you’re creating a pre-processor and it would probably have to reside outside of Home Assistant, possibly as a Node-Red flow.

Anther idea (more of a guess) is to create an automation that subscribes to paradox/states/zones/Entrance_door/# and, when triggered, passes the last portion of the received topic (say it’s rf_low_battery_trouble) and its payload to a python_script. Given the additional flexibility of python, it writes the data directly to the binary_sensor’s attributes property. How? I don’t know; I have never tried it.

This is exactly how I see it at the moment, I was kind of hoping someone had an alterntive.

I think what I might to is create multiple individual sensors for the above list - and then combine them into a single sensor using a template sensor. Might be a lot of work - but most of the code can be generated as the MQTT topics are consistant.

If you intend to create multiple individual sensors, at that point what’s the advantage of combining them into a single sensor? You’ve already done the work of defining them individually.

I suppose mainly for display purposes - using secondary info in lovelace - and also - searching through the states in Developer tools - it’ll be easier if you can view them all grouped

1 Like