I am struggling with an unusual logic situation in an automation. The problem is that binary sensors have THREE states inside HA; on, off and undefined. However, the scripting engine only supports two (near as I can tell), on and off.
Here’s my problem.
There’s a float switch that controls a pump, turning it on automatically when the water level rises. And when the pump gets power, an ESPHome device senses it.
The ESPHome device then sends HA one of two messages, pump is on or pump is off.
This is very useful to me because I can see how often the pump comes on, and for how long. (Ain’t logging great?) Dramatic changes require investigation.
But being a belt and suspenders kinda guy, I also have an automation that emails me when the pump turns on and when it turns off. (Hopefully, I see an off within a minute or so of the on.)
My problem is that the network connection is sometimes a bit flakey, and the ESPHome device gets disconnected from the HA system. The pump status is then neither off nor on; HA shows it as undefined.
The ESPHome device soon reconnects, HA now sees the switch as Off and sends me a message. The HA logic appears to be “switch was not in the off state” (which is technically correct, Undefined is not Off) “and since the switch is now off, I better email Alan.”
And it does, religiously, often sending me a dozen messages in the space of a few minutes, until the network connection (Powerline Ethernet) sorts itself out.
What I need is a test that allows me to see if the switch went from “On” to “Off”, as opposed to the switch going from “Not Off, for one reason or the other” to “Off”
I can’t quite wrap my head around how to do this …