I’m using 0.104.3 with python 3.7. (OS is NetBSD 8 on amd64 hardware, but I doubt that matters.)
I have a few binary sensors that are derived from MQTT; they set name, state_topic and device class (connectivity). I am posting ON and OFF to the topic, aligning with the defaults in the MQTT binary sensor documentation. My binary sensors are working 100% ok. They show as “Disconnected” (I think) and “Connected” in the UI but seem to be "off’ and “on” in the code - which all seems fine. These sensors are supposed to be ‘on’ all the time (because the MQTT ESP8266 temperature sensor is connected, and the lwt to set the connectivity topic to OFF hasn’t fired), and change to ‘off’ when they go offline (which is bad).
I also have some binary sensors that are usually ‘off’, and ‘on’ when there is a bad condition, including zwave flood sensors and template sensors that check for too-low temperatures. Both of these are working great with alerts - but here the alert state is ‘on’, which is the default.
I want an alert on one of the normally-on sensors, where it fires if the state goes to off, telling me “sensor disconnected”. As I read the docs, I just have to add “state: off” to the alert. When I do this, I don’t get any alerts.
I added debugging code to the alert component, and found out that the “alert_state” variable was set to False instead of off. I added a line that if to_state is off and alert_state if False, to just set alert_state to off. Then I print the values again. With this change, I get alerts. Logs look like
2020-02-05 12:07:42 DEBUG (MainThread) [homeassistant.components.alert] Watched entity (binary_sensor.temp_sensor) has changed
2020-02-05 12:07:42 DEBUG (MainThread) [homeassistant.components.alert] to off alert_state False
2020-02-05 12:07:42 DEBUG (MainThread) [homeassistant.components.alert] to off alert_state off
2020-02-05 12:07:42 DEBUG (MainThread) [homeassistant.components.alert] Beginning Alert: Temp Sensor Disconnection
My extra lines look like:
async def watched_entity_change(self, entity, from_state, to_state):
"""Determine if the alert should start or stop."""
_LOGGER.debug("Watched entity (%s) has changed", entity)
_LOGGER.debug(" to %s alert_state %s", to_state.state, self._alert_state)
if to_state.state == "off" and self._alert_state == "False":
self._alert_state = "off"
_LOGGER.debug(" to %s alert_state %s", to_state.state, self._alert_state)
if to_state.state == self._alert_state and not self._firing:
await self.begin_alerting()
if to_state.state != self._alert_state and self._firing:
await self.end_alerting()
Is this a real bug? How is off/on supposed to map to python booleans? Why is there a string value False, vs off? Do alerts with state: off work for anyone else? Thanks for any help anyone can provide.