MQTT binary sensor - availability json template?

Hi,
is it possible to somehow specify an availability_template, same way as value_template?

I am setting up a MQTT device (Advantech ADAM-6250), unfortunatelly it’s availability topic payload looks like this:

{
    "status": "connect",
    "name": "ADAM6250",
    "macid": "00D0CAC47F44",
    "ipaddr": "10.10.1.58"
}

I need to extract the status key from the JSON payload to use it for the payload_available and payload_not_available values.
The MQTT Sensor page says something about the json_attributes_topic and json_attributes_template, that it also uses availability_topic, but I don’t know how/if this could be used.

Thank you

No, it doesn’t look like it’s possible. The payload is assumed to be a single value, not an array…and there doesn’t seem to be a way to subscribe to a topic/data_subset such that it only passes the right data.

Could do something like this. We’ll listen for all of these devices (assuming you have multiple of them) and republish their availability status on a new topic that you can use for the availability topic.

- alias: MQTT Availability Hack
  trigger:
    platform: mqtt
    topic: /devices/+/status
  action:
    - variables:
         # Extract which device this was from the topic. It will be the 3nd entry of the topic according to 
         # my current topic above (first slash is 0, 2nd is 1, + is 2)
         device: "{{ trigger.topic.split('/')[2] }}"
    - service: mqtt.publish
      data:
        topic: "/devices/{{device}}/availability"
        payload: "{{ trigger.payload.status }}"

Assuming you have a bunch of devices with the following state:

/devices/adam6250/state
/device/adam1234/state
/device/blah/state

This will parse their state topics and republish it to /device/<device>/availability. Now you can use this new topic as your availability topic for these devices.

If you only have the one, you can replace the + with the device name instead.

1 Like

That’s a nice and clean solution, thank you very much.

I was thinking about using the MQTT trigger and manually setting each binary_sensor and switch to unavailable, but that would get messy pretty fast, it didn’t even cross my mind I could just retransmit the parsed value.
They have a new FW for this device, where it should be possible to set your own topics and data, but their server is messed up, so it can’t be downloaded and support almost non existant, so I am glad it could be fixed this way.
Thanks again.

I just tested your solution and it is working nicely. I just had to do one small modification.
The {{ trigger.payload }} is not parsed as json, so the property cannot be read directly,
instead {{ trigger.payload_json.status }} needs to be used.

1 Like