I reloaded MQTT entities at about 00:45. At 07:35 it went unavailable.
What’s wrong with that?
Coincidently, I did use ‘Listen to the topic’ available in Mosquito Broker settings (official add-on) to check the value of the availability topic. Using it has fixed the entity state immediately
I had 3 such entities and this phenomenon repeated for each of them.
Can anyone explain why it happened?
The only explanation I think of is, that:
HA didn’t register new (availability) topic for listening after I configured entities and reloaded them
‘Listen to the topic’ feature uses the same mqtt client connection as HA does. So attempt to listen to it, loaded data needed for HA entity
It still doesn’t answer the question why the entity went unavailable after about 7 hours
And after about 2h 20 minutes one of those entities went unavailable again. Why?
And again resurrected by enabling listening to its availability topic in Mosquito config.
Just curious if battery-powered devices need to use the availability topic at all.
Anyway I have enabled debug logging for core and mqtt components
Probably I found the answer. Actually two answers
At first: for battery-powered devices, I should not use the availability topic. Shelly sets it to false when going to deep sleep. I found it sets it to true later (after some hours), cannot get the reasons, but anyway, I should use expire_after without the availability topic.
The second issue lies down probably in Shelly recent firmwares. I found similar behavior in DW and DW2 with firmware v1.11.7 and 1.11.8. The shelly provides a setting to ensure a periodical refresh of mqtt topics. Unfortunately for some reason, it doesn’t refresh the state topic. It refreshes battery and tilt topics (if tilt sensor is enabled) but never the state one. So if a window is not used for some time, it properdly reports unavailability.
Here is debug log for core and mqtt components:
2022-05-20 08:27:19 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=binary_sensor.pcroom_window_state, old_state=<state binary_sensor.pcroom_window_state=on; device_class=window, friendly_name=PCroom Window State @ 2022-05-20T08:16:26.617687+02:00>, new_state=<state binary_sensor.pcroom_window_state=off; device_class=window, friendly_name=PCroom Window State @ 2022-05-20T08:27:19.147289+02:00>>
2022-05-20 08:27:19 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on shellies/shellydw-pc/sensor/battery: b'100'
2022-05-20 08:29:16 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=binary_sensor.pcroom_window_state, old_state=<state binary_sensor.pcroom_window_state=off; device_class=window, friendly_name=PCroom Window State @ 2022-05-20T08:27:19.147289+02:00>, new_state=None>
2022-05-20 08:29:18 DEBUG (MainThread) [homeassistant.components.mqtt] Unsubscribing from shellies/shellydw-pc/sensor/state, mid: 4202
2022-05-20 08:29:20 DEBUG (MainThread) [homeassistant.components.mqtt] Unsubscribing from shellies/shellydw-pc/sensor/battery, mid: 4344
2022-05-20 08:29:20 DEBUG (MainThread) [homeassistant.components.mqtt] Subscribing to shellies/shellydw-pc/sensor/state, mid: 4372
2022-05-20 08:29:20 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on shellies/shellydw-pc/sensor/state (retained): b'close'
2022-05-20 08:29:20 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=binary_sensor.pcroom_window_state, old_state=None, new_state=<state binary_sensor.pcroom_window_state=off; device_class=window, friendly_name=PCroom Window State @ 2022-05-20T08:29:20.435033+02:00>>
2022-05-20 08:29:23 DEBUG (MainThread) [homeassistant.components.mqtt] Subscribing to shellies/shellydw-pc/sensor/battery, mid: 4458
2022-05-20 08:29:23 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on shellies/shellydw-pc/sensor/battery (retained): b'100'
2022-05-20 19:43:20 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on shellies/shellydw-pc/sensor/battery: b'100'
2022-05-21 07:03:31 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on shellies/shellydw-pc/sensor/battery: b'100'
2022-05-21 09:29:20 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=binary_sensor.pcroom_window_state, old_state=<state binary_sensor.pcroom_window_state=off; device_class=window, friendly_name=PCroom Window State @ 2022-05-20T08:29:20.435033+02:00>, new_state=<state binary_sensor.pcroom_window_state=unavailable; device_class=window, friendly_name=PCroom Window State @ 2022-05-21T09:29:20.435968+02:00>>
For sensor above I have set expirer_after to 25hours (90000 secs) which matches results found in the log.
Of course I’m going to report this issue to Shelly. Just letting you know if you are using mqtt for Shelly devices. BTW… how many users use that config? I can see no similar reports at all. Maybe nobody uses expiration settings…
While another shelly sensor uses mqtt, and the status is displayed, (motion sensor), i cannot get why this door sensor does not show up in home assistant.
No, not at all.
BTW one of my door-window v1 sensor, mounted on doors to the garden has registered 4700 state changes for more than 3 years without battery change. And still counting
Thanks. I will give it a try once i resurrect my server . I had an issue with my server and at the moment my backup is running in a HAOS and no mqtt broker is set up there.