Just occurred to me that it would be useful to ensure that my flows work after a HA restart. Does anyone know if sensor entities go to a status of ‘unknown’ during a restart? If so, is there a virtue of coding state trigger nodes with “is not: Off” in order to be triggered not only when the sensor changes, but also when HA restarts or a sensor connection is reestablished?
There are threr kinds of restart here.
- HA core restarts.
HA will show unavailable until the connection to NR is re-established. - Entire system restarts.
HA will startup with unavailable and they will stay like that until the flows starts to fill data in. - NR restarts.
Not really sure what happens here.
HA might keep the last data available until the flows starts to fill data in again, but it might also goto unavailable right away or with a delay depending on when it detects NR is not updating the data anymore.
A binary sensor, or other entity, created by an integration has its value set by the integration. In other words, on startup, it’s the integration’s responsibility to set the value of its entities.
Most of my entity states are ok across reboots so I do not have any additional checks in my trigger nodes for “unavailable” or “unknown”. I do have an Unavailable Template Sensor and a flow to notify me if there are any unavailable entities.
If you want to trigger a flow on Node-Red restart, you can check for that event:
https://zachowj.github.io/node-red-contrib-home-assistant-websocket/node/events-all.html#outputs
I wasnt really going to add different checks/extra nodes, but it occurred to me that if my HA instance goes down and some events get missed etc, using a state of ‘on’ is very limiting in terms of when it activates (i.e. only on state off) whereas the state of ‘is not on’ is much broader and would trigger on both unavailable and on. Thus there might be some extra resilience in the code on a restart or if a sensor runs out of battery.
If it does not trigger, then there isnt much point in me going to the extra effort!
You can check for state changes from unavailable or unknown to on or off and react to that.
You can also check for connectivity to HA in Node Red.
I think it is in the globals variable and maybe the last one or there about in the list.
True but it is also ambiguous. “is not on” could be “off”, “unknown”, or “unavailable” - this is not a discrete condition. This is ok if you want to treat each of these conditions the same. But I treat “off” completely different than “unknown” and “unavailable” (I know the sensor is off if it is reporting “off”, I don’t know if the sensor is on or off if the state is “unknown” or “unavailable” but I do know there is a problem with the sensor and that requires a different action).
You will not know state changes while HA is down. You can check the current state when HA restarts. You may be able to get the previous state before HA went down. If a sensor ran out of battery while HA was down, or any other time, and becomes unavailable that is an error condition - “is not on” or “is not off” may be a normal or an error condition.
Also, there are additional actions on the trigger:state and event:state nodes like Output on Connect that can run your flow on restart.