Automation trigger on hour+minute+second in variable (sensor/input_numbers)

I’m trying to build some automations which I want to trigger at an adjustable time. I will have the time hours, minutes, and seconds stored (or calculated) in sensors and input_number types.

Is there a good way to do this without having a bunch of separate template-sensors for hour, minute, and second to compare individually?

I’d like to do something like (psudocode):

  - alias: 'Do A Thing'
    trigger:
      - platform: time
        at: '{{input_number.selected_hour}}:{{sensor.computed_minute}}:{{sensor.computed_second}}'

The only thing I can think of right now would be a really convoluted template-sensor that does something like this:

{%
now().strftime('%H') == input_number.selected_hour  and
now().strftime('%M') == sensor.computed_minute  and
now().strftime('%S') == sensor.computed_second
%}

But this seems needlessly complex to me, surely there is a better way?

A Time Trigger’s at option doesn’t accept templates.

It seems complex because you’ve designed your time to be based on three separate entities. To compute the desired time, it’s obligated to use the combination of all three entities.

I suggest you create a Template Sensor with timestamp for its device_class.

template:
  - sensor:
      - name: Thing Time
        state: >
          {% set t = '{:02}:{:02}:{:02}'.format(states('input_number.selected_hour')|int(0), states('sensor.computed_minute')|int(0), states('sensor.computed_second')|int(0))%}
          {{ today_at(t) }}
        device_class: timestamp

Then you can easily reference it in your automation’s Time Trigger.

  - alias: 'Do A Thing'
    trigger:
      - platform: time
        at: sensor.thing_time
1 Like

Yeah, but I don’t know of a better way to get what I want (partly stored, partly computed, partly random pieced together time) since us silly humans think in hour/minute/second.

Having the intermediate sensor to combine the data into a timestamp seems like a good straightforward solution though, much better than what I had in mind.

If the suggestion meets with your approval, please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.