MQTT sensor discovered

Hi
I have a zigbee2mqtt integration on which I enabled the option to report the status of devices. Since then the mqtt broker publishes me availability topics like this zigbee2mqtt/zb-temp-wojtek/availability online. As IT people are lazy by nature and prefer to automate everything, so I decided not to define the corresponding binary sensors manually, but to write a corresponding python script, which thanks to the discovery option will add these sensors for me. I wrote the script. The script generates a json that creates the sensors for me. Unfortunately, I have a problem with the sensors created this way, which do not change state. To make it easier to imagine everything, I paste the actual code :

mosquitto_pub -u mqtt -P mqtt -t homeassistant/binary_sensor/zb-bathroom/config -m '{
"name": "zb-bathroom status", 
"device_class": "connectivity", 
"state_topic": "zigbee2mqtt/zb-bathroom/availability", 
"availability": {
                  "payload_available"   : "online",
                  "payload_not_available": "offline", 
                  "topic": "zigbee2mqtt/zb-bathroom/availability"
}, 
"device": {
                  "identifiers": ["zb-bath   room"], 
                  "name": "Zb-bathroom"
}
}'

The sensor so defined does not change state on availability topic for online or offline payload , because it gets such values from zigbee2mqtt.
The state changes if I set ON or OFF in payload, but such a value does not receive from zigbee2mqtt.
I’ll add that I tried various combinations, but it seems that I missed the right one. I made use of this documentation
Question - how correctly should the json defining the sensor look like ?

availability is an array, not an object.
image

Try

"availability": [
         {
                  "payload_available"   : "online",
                  "payload_not_available": "offline", 
                  "topic": "zigbee2mqtt/zb-bathroom/availability"
         }
     ], 

Below is a screenshot from the console.

further does not work.
Only the third attempt to change the status worked, but it is not consistent with what I get from zigbee2mqtt

Hold on.
The state topic is not supposed to be the same as the availability one…
Availability is a HA concept, telling whether the sensor is available or not.

There is some confusion, here


I think we are moving forward, because after changing the state_topic to zigbee2mqtt/zb-bathroom - it stopped working. The conclusion is that this configuration with the availability table is not correct, because it was not taken into account. However, it looks like this payload json is consistent with the documentation. I don’t know what to think about it. I must have an error somewhere, but I can’t see where.
obraz

I’m super confused regarding what you have in MQTT.
What topic contains the actual state you want in HA, and what is its value?

OK, then maybe I’ll write from the beginning what I want to achieve and how I want to get there.
All sensors “zb” ( almost ) are created through zigbee2mqtt integration. It works. However, I did not have a sensor for the availability of a particular device. I enabled the corresponding option in the integration and now by subscribing to the corresponding topic I get a list of devices with their connection status.

ihome@hajime:~/hassio$ mosquitto_sub -u mqttbroker -P mqttbroker -v -t zigbee2mqtt/+/availability
zigbee2mqtt/zb-aquarium/availability online
zigbee2mqtt/zb-temp-wojtek/availability online
zigbee2mqtt/zb-switch-wojtek/availability online
zigbee2mqtt/zb-anteroom/availability offline
zigbee2mqtt/zb-kitchen-sconce/availability online
zigbee2mqtt/zb-bathroom-move/availability online
zigbee2mqtt/zb-outdoor-contactor/availability offline
zigbee2mqtt/zb-shake/availability offline
zigbee2mqtt/zb-wardrobe-move/availability online
zigbee2mqtt/zb-thermostat-wojtek/availability online
zigbee2mqtt/zb-thermostat-zofia/availability online
zigbee2mqtt/zb-bed-room/availability online
zigbee2mqtt/zb-bathroom/availability offline
zigbee2mqtt/0x00158d0007e4dd59/availability offline

Thanks to such data, I want to create a binary sensor showing me the connection status of the type connectivity

Thanks to such data, I want to create a binary sensor showing me the connection status of the type :
As there are some of these sensors and more may appear, I would like to automate the process of adding them, thanks to the discovery option. Below I paste the code of the script, perhaps someone will find it useful.

# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt
import json

MQTT_BROKER = "192.168.1.88"
MQTT_TOPIC = "zigbee2mqtt/+/availability"
MQTT_USER = "mqttbroker"  
MQTT_PASSWORD = "mqttbroker" 

def on_message(client, userdata, msg):
    print(msg.payload)
 
    device_name = msg.topic.split("/")[1]
    print(device_name)
    data = {}

    data["name"] = '{} status'.format(device_name)
    data["device_class"] = "connectivity"
    data["state_topic"] = 'zigbee2mqtt/{}'.format(device_name) #msg.topic
    #data["availability_topic"] = msg.topic
    #data["topic"] = msg.topic
    data['availability'] = [{
        "payload_available" : "online",
        "payload_not_available" : "offline",
        "topic" : msg.topic
        }
    ]
    data["device"] = {
            "identifiers": [device_name],
            "name": device_name.capitalize()
    }
    json_payload = json.dumps(data)
    print(json_payload)

    client.publish("homeassistant/binary_sensor/{0}/config -m '{1}'".format(device_name), jso

client = mqtt.Client()
client.username_pw_set(MQTT_USER, password=MQTT_PASSWORD)  # Ustaw użytkownika i hasło MQTT
client.on_message = on_message
client.connect(MQTT_BROKER, 1883, 60)

client.subscribe(MQTT_TOPIC)

client.loop_forever()

This script generates a JSON payload which creates a binanry sensor of the connectivity type containing the status.

{
  "name": "zb-aquarium status",
  "device_class": "connectivity",
  "state_topic": "zigbee2mqtt/zb-aquarium",
  "availability": [
    {
      "payload_available": "online",
      "payload_not_available": "offline",
      "topic": "zigbee2mqtt/zb-aquarium/availability"
    }
  ],
  "device": {
    "identifiers": [
      "zb-aquarium"
    ],
    "name": "Zb-aquarium"
  }
}

Unfortunately it doesn’t work the way I would like it to.
he most important thing is this topic zigbee2mqtt/zb-aquarium/availability
Because it is in this topic that zigbee2mqtt publishes availability information.

It may just be that the way I want to do it is wrong, or the JSON itself contains errors.

I am counting on the experience of other users who can point out the error to me

But you have enabled Homeassistant support in Z2M, right? Home Assistant | Zigbee2MQTT

If so, Z2M will create the config topic for you and (I guess) the entities will simply have an “unavailable” state in HA if, well, unavailable.
If not, show the config topic created by Z2M, not using it myself.

Configuration via Z2M
obraz

My automatic created sensors with status “offline”


There is no sensor here that is just for me to determine whether the device is online or offline. That is why I want to create such a sensor. I would like to have consistency across all devices when it comes to showing availability. I mean to achieve the kind of state I have for other devices.

The above are EspHome devices, where device type is connectivity