How can I modify this statement to include the time since it is on? This is used as a condition in an automation.
{{ (states("switch.test") == "on" ) }}
How can I modify this statement to include the time since it is on? This is used as a condition in an automation.
{{ (states("switch.test") == "on" ) }}
Dunno if there’s better ways, but I’d probably do this
condition:
- condition: state
entity_id: switch.desklamp
state: "on"
for:
seconds: 30
It’s more verbose, but then I wouldn’t need to mess about with comparing dates and times in templates
Almost, but I need the “Wait for a template to render true”. I now see that I didn’t mentioned this.
do a wait for trigger instead of wait_template
But this won’t work if the trigger is already triggered for x seconds. Or is it?
{{ as_timestamp(now()) | round - as_timestamp(states.switch.test.last_changed) | round > 10 and states('switch.test') == 'on' }}
This should render true if the switch is on and the time between last_updated and now is 10 seconds or more. If there is nothing else that is updated in the entity (for example attributes which regularly update) it should work.
Thanks, I will try that. Could I use last_changed
instead?
Yes, this would be even the better way as it only changes if the state changes. Then it should be bulletproof, i change my post to this.
that template won’t work for less than 1 minute. now only updates on the minute.
Will your suggestion work, even if the switch is already on for 2 minutes?
Not sure if I understand waiting for trigger correctly.
Why not post your yaml and what you’re trying to achieve overall, rather than specific technical asks, it sounds like you’re not sure what you need to know right now.
Good point.
I have scripts that set my receiver to specific setting.
This works instantly after it is online. But the receiver needs about 20 seconds to boot up. Since I can’t use the receiver’s state directly (other story) I will have to use a switch that I use to switch on the TV. After this switch has been on for 20 s the receiver is considered “online” and can receive the commands. Otherwise I wait.
This works well enough:
- wait_template: '{{ " second" not in relative_time(states.switch.tv.last_changed) and states("switch.tv") == "on" }}'
continue_on_timeout: true
timeout: '20'
The additional timeout helps to avoid waiting 59 seconds (worst case). Instead it will wait 20 seconds max. in the first 59 seconds. But yeah it is not perfect.