Calculate remaining time and put into a delay or wait template for an automation

Is there a way to calculate the remaining time from when a sensor numeric state is met and 7:00 AM? The sensor monitors power and I want it to trigger an automation but if the sensor numeric state is met at night I want a delay or wait template but since the trigger time is random I want something that calculates how long until 7:00 AM so that once the time is met the actions of the automation will run. I don’t really want to use Time Pattern as the trigger since I want the sensor state to be the trigger. Is this possible?

Are you saying that you always want the actions to run at 7am or later even if the trigger happens before then?

you could use a “wait_for_trigger:” and set 0700 as the trigger in the wait condition.

you may need to use a “choose:” action depending on if the trigger occurs before or after 0700 depending on your requirements. Before 0700 wait till 0700 or if after 0700 run the actions immediately.

Is there an example somewhere that I can use to implement in my automation? I can’t figure out what goes where for the wait.

I was planning on using a "choose"action. It’s the “wait until 0700 then run” that I have trouble figuring out how to implement. How do I do that?

Here is the documentation for that specific thing:

but the example is a bit lacking.

but basically you use it in your actions just like any other action.

example:

action:
  - service: some_service
    entity_id: blah.blah
  - wait_for_trigger:
      platform: time
      at: '07:00:00'
  - service: some_other_service
    entity_id: yada.yada

in the above the “some_service” will run then wait till 0700 the run the “some_other_service”.

if you are using it in a “choose” you can use it exactly the same way under the action sequence.

action:
  choose:
    - conditions: 
        your_pre_0700_conditions:
      sequence:
        - service: some_service
          entity_id: blah.blah
        - wait_for_trigger:
            platform: time
            at: '07:00:00'
        - service: some_other_service
          entity_id: yada.yada
    - conditions:
        your_after_0700_conditions:
      sequence:
        some_other_actions

Obviously just an outline and not checked for correct syntax but it should give you the gist.

For some reason I never noticed the Add Trigger option that shows up after choosing Wait for Trigger until just now. That’s what was all I was missing. Thanks for the help.

1 Like