- >
{{is_state('binary_sensor.XXXXX','off') and
(now() - states.binary_sensor.XXXXX.last_changed).total_seconds() >
states('input_number.interval')|int}}
unless you’re specifically interested in the syntax request, this would come close to what you are looking for?
got to remember though this is a condition checked on the moment of the trigger. It won’t wait for the time being set in the input_number if it hadn’t yet reached that. It would simply be false.
if you want to check that, you got to reference the last_changed.
Yes I tried this and it worked,
but there is a problem doing the “elapsed time check” in the trigger,
because if I remember, if HA restarts in-between (after the on-off state change),
it doesn’t work anymore, or the time is not respected (I don’t remember the exact effect)
At startup, the entity’s last_changed is set to the startup time so that screws up ‘elapsed time’ calculations. There’s been a long-standing request to preserve the entity’s last_changed time (like its state) but, for whatever reason, it’s never been implemented. I believe the most recent request was made during the Month of What the Heck.
Given that you can’t template for in a State Condition and elapsed-time calculation based on last_changed can be unreliable, can you explain how you are using the Time Pattern Trigger as a way of getting around those two limitations? I’m not seeing how a Time Pattern Trigger can do that …
EDIT
Here you go:
225 votes in favor of the proposal to allow last_changed to survive a restart.
OK, so since there is no way to overcome the last_changed problem anyway (for the moment) :
trigger:
- platform: state
entity_id: binary_sensor.XXXXX
to: "off"
for: "00:{{ states('input_number.interval') | int }}:00"
So, no need for the “for” template in condition (even if it would be nice)
Thank you guys for your eyes-opening help
EDIT: ah, there is a problem when this is a trigger and not a condition :
the automation must be ON when the state is changing, if not, it won’t go and check the ‘last_changed’.
(so the time trigger is an improvement in my case, even if the HA reboot problem remains )
How is this still not implemented after 7 years? Why I can’t use for at numeric_state condition? It works with normal state check and even with the numeric state check in the trigger! Why it does not in conditions?
I would really need this and unable to find a workaround without creating unnecessary entities just for this simple specific check.
That’s about triggers. I want to check if a state has been at least x in x minutes in conditions.
I tried it with templates but that also does not work because it does not know if it was below a specific state. Probably I need to add for each check an extra binary sensor like the answer of this thread is.
Not a good solution but at least it should work.
Edit: Sorry, my fault. This is about the for check in triggers. I misread that an thought it is about that it works in trigger but not in conditions because he compared the two.
All good, so what’s your actual problem? Think about opening a new thread, this is very outdated. And feel free to tag me, I’ll be happy to jump in, if I can give a quality answer.
Sorry for necroposting, but do we have a more elegant solution for the problem in the meantime?
The previous answer is an answer - but only a workaround in my eyes.
It’s neither pretty nor intuitive and the use friendliness is pretty limited as well.
You can do for on numeric_state triggers. That’s what this feature request is about. If you want this on a condition, it’s not possible at this time.
It’s actually unlikely to ever get implemented for a condition due to the limitations of the system. It would need to traverse the database and look at all values over the specified duration. The limiting factor is that it would require users to use history and recorder, which are optional integrations.
I do understand that it’s not trivial to do that like you explained.
What I do not get: why can’t it do what is explained above in the background? Create hidden sensor or the like that.