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