Automation Trigger based in Highest Timer

I am looking for something very specific and not sure if this question has been asked and answered before.

SCENARIO:

  • I have multiple timer helpers that are triggered on motion in certain parts of the house.
  • when the timer is triggered it in turn triggers a video feed on my tablet.
  • when the timer expires, the video feed reverts to a default stream (driveway let’s say)

So far so good. No issues with that.

However sometimes while one stream is running , another timer is triggered and the stream switches, which is fine.

But when the current timer expires, I want to check which other timer is running the longest and revert back to that, instead of the default view.

Sort of like if timerA is active, then stream such and such, else if timerB is active, then stream …which is also fine , I can do this under conditional executions.

But how do I do this based on which timer has the highest time left still?

Hope this makes sense.

Just as a follow up since I’m more of a PHP person, maybe explaining it that way would make more sense.

I would create an array of all timers, then sort by whichever is running the longest, and simply run through that to select the next stream to activate.

are all the timers running for the same length of time? if so, you can use the finishes_at attribute which tells you when they are set to finish. the one finishing soonest is the longest running.

so then you can do something like this:

{% set entities = ['timer.timer1', 'timer.timer2', 'timer.timer3'] %}
{% set first_to_finish = entities | expand | selectattr('attributes.finishes_at', 'defined') | sort(attribute='attributes.finishes_at', reverse=false) | first  %} 
{{ first_to_finish }}

Hey and thank you. I am sorry was a bit tied up on something else and I didn’t follow up til now.

I get what you are saying. But I have two questions.

  1. Where do I create this template? Meaning, under helpers or elsewhere or as part of scripts?

  2. Is there a way to define a default value for {{ first_to_finish }} ?