Is there a way to do this without using any other intermediate sensors/helpers given that the binary sensor will change state to unknown during a restart?
I seem to remember some discussion around this when the binary sensor was changed to be unknown at start up in that there was a way to persist the previous state. But I can’t find it now so I could be wrong.
And no need to rehash the debate about whether a binary sensor is indeed a binary sensor if it can have three possible states
However, all bets are off if there’s a restart involved because, as you said, that’s a momentary but legitimate state-change to unknown. It affects the calculation of the state value’s duration.
I know you don’t want to use templates, but they are very powerful and to do anything sophisticated you need them. I probably have 10x more template sensors than device sensors. Anyways using templates:
It’s easy to do using a trigger template. Just trigger on the state changing from Off to On and in the template expression use now() to record the current time. This value will persist restarts and will only update when state goes from Off to On and will not update when it goes from Unavailable to On.
Then create another template sensor that calculates the difference between now() and the value of the trigger template - call this latency.
Then create a binary sensor that is true if the latency >= to 23 hours and the target sensor is On
Yes it should be. A trade off between one more complicated sensor and simpler set of sensors. For things like this, I like to have the latency sensor so I can see how far away it is from 23 hours. Also I’d likely create an input number to hold the limit to 23 so that I can change it when needed without going back to YAML.
The great thing with HA is there are many ways to do just about anything.
Might be more challenging to sell klogg on the idea of using multiple “intermediate sensors/helpers” than just a single one. Nevertheless, I agree, Home Assistant’s flexibility often allows for more than one way to do something.
I currently do it with an intermediate sensor but for various reasons I wanted it to be just one. It isn’t a hard limit though, just something I wondered if it could be done.
Also I’m not averse to templates. On the contrary, over the years I’ve ‘acquired’ hundreds of them
Those attributes are ones I created in the template sensor. this is the state object for whatever it is self-referencing. This page has all the information on what is in a state object. Trigger data can be found here, the data is different depending on which trigger platform is used.
So, for example, since the code I posted made up an attribute called last_confirmed_state I can reference it from within that sensor by using this.attributes.last_confirmed_state. But I could have called it anything I wanted. So no, the attribute last_confirmed_state doesn’t exist elsewhere in other home assistant entities.
The trigger variable is only populated when the trigger is fired. And I don’t have triggers that fire upon a restart directly. However the state of the source sensor changing after a restart (from unavailable to off for example) will fire the state trigger. But then the template will check to see if the state is different from what the previous state (last_confirmed_state) was, and if it isn’t different, the timestamp doesn’t get updated.