Wait Template just doing random times

I have a wait template in a blueprint with 2 inputs

  input:
    entity_trigger:
      name: Trigger Sensor *
      description: The Light switch.
      selector:
        entity
    trigger_wait_time:
      name: Trigger Wait Time
      description: The time the trigger (Light) must be ON before the fan will be turned ON.
      default: 3
      selector:
        number:
          min: 0
          max: 10
          step: 0.2
          unit_of_measurement: minutes

I cant make another trigger for this so I am trying to use this wait_template. The idea is to make sure the “entity_trigger” is in the ON state for X (say 60) amount of time.

              sequence:
                - alias: "wait for the fan to be on for given time period"
                  wait_template: "{{ ( states[entity_trigger].state == 'on') and (now()-states[entity_trigger].last_changed).total_seconds() > trigger_wait_time*60 | int }}"

It works but if I have the time set to 1 minute or 60 seconds it never is accurate. It could be out by 8 to 40 seconds, it is just random even if I try this.

              sequence:
                - alias: "wait for the fan to be on for given time period"
                  wait_template: "{{ ( states[entity_trigger].state == 'on') and (now()-states[entity_trigger].last_changed).total_seconds() > 60 }}"

Am I doing something wrong or maybe should I try another way?

Thanks for your help.

It is random because time refreshes at the start of every minute. See below.

Would anyone have a better way to achieve this and be more accurate?

All good worked it out.

First I used a delay and then I checked if the state is off and if so used “stop:” in conditions.

FYI, you can use wait_for_trigger within an automation action or script sequence. Not sure this works in your scenario, but it is a possibility.

Or, you could try something like:

- if: "{{ is_state(entity_trigger, 'on') }}"
  then:
    - wait_template: "{{ is_state(entity_trigger, 'off') }}"
      timeout:
        minutes: "{{ trigger_wait_time }}"
    - if: "{{ wait.completed }}"
      then: ...
      else: ...
1 Like