Hi. I have an ESP8266 with temperature probes. I’m sending temperature, wifi, and connection status (binary sensor) over MQTT. It all works fine except for two things:
When the ESP is disconnected, all values, badges, etc are just frozen as the last value
Binary sensor doesn’t change to ‘offline’
How should one go about having the state of all sensor being updated over MQTT. Is it somehow through birth/will messages?
Here’s my ESP Yaml (addresses and credentials are removed):
mqtt:
broker: x
discovery: true
username: x
password: x
port: 1883
topic_prefix: test/unit_1
birth_message:
topic: test/unit_1
payload: online
will_message:
topic: test/unit_1
payload: offline
I have both sensors registered in the ESPhome integration as well. But they are physically located away from the Pi (different buildings) so my solution is to use MQTT. My understanding is that the integration only detects online/offline if they’re on the same network as the Pi. Perhaps I am mistaken?
That worked brilliantly. Thanks a lot. Why exactly does your solution work though? Which message carries the info for the availability topic, and how does it force HA to switch status?
You’re publishing the “birth” and “will” messages to the availability topic. The “birth” message is sent by ESPHome once it connects to the MQTT broker, which will set the topic to the value “online”. This tells HA the device is online.
When the MQTT broker notices that the connection to your ESPHome device is gone, the broker will publish the “will” message (“offline”) to the availability topic, which tells HA the device is offline.