Delay Automation action based on input_number

Hi everyone,

I’m trying to create an automation that will turn off the fan in our bedroom at night after X minutes. I want to be able to change the amount of minutes in the front end, based on an input number.

I can’t seem to figure out how to add the delay in the automation.

This is my automation so far:

- alias: Grote ventilator uitschakelen na X aantal tijd
  trigger:
    - platform: template
      value_template: "{{ states('sensor.time') == (states.input_datetime.big_fan_start.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
    - platform: state
      entity_id: switch.big_fan
      from: 'off'
      to: 'on'
  condition: 
    - condition: template
      value_template: "{{ states('sensor.time') >= (states.input_datetime.big_fan_start.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
    - condition: template
      value_template: "{{ states('sensor.time') <= (states.input_datetime.big_fan_stop.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
    - condition: state
      entity_id: switch.big_fan
      state: 'on'
  action:
    - service: homeassistant.turn_off
      entity_id: switch.big_fan
    - service: notify.pushover
      data_template:
        message: Grote ventilator uitgeschakeld.

This is the config for the input number:

input_number: 
  big_fan_delay:
    name: Grote ventilator minuten
    initial: 30
    min: 5
    max: 120
    step: 5

Use the developer tools template editor to check the sections of your templates. You are comparing a time string to a timestamp.

The automation works as it is right now. The only thing left to do is to add in a delay for x minutes based on the input number in the actions section of the automation.

But I don’t know how to do that.

Apologies I did not scroll far enough to the right to see all your template.

See the 4th and 5th example here: https://www.home-assistant.io/docs/scripts/#delay

Isn’t delay meant to be used in a script instead of an automation?

The easiest way is with the timer component. Have a look at the docs.

It can be used in either a script or automation.

I couldn’t get delay to work in the automation so I created a script. Whoever the script doesn’t seem to be running from the automation.

When I manually trigger the script everything works fine. Any Idea’s:

- alias: Grote ventilator uitschakelen na X aantal tijd
  trigger:
    - platform: template
      value_template: "{{ states('sensor.time') == (states.input_datetime.big_fan_start.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
    - platform: state
      entity_id: switch.big_fan
      from: 'off'
      to: 'on'
  condition: 
    - condition: template
      value_template: "{{ states('sensor.time') >= (states.input_datetime.big_fan_start.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
    - condition: template
      value_template: "{{ states('sensor.time') <= (states.input_datetime.big_fan_stop.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
    - condition: state
      entity_id: switch.big_fan
      state: 'on'
  action:
    - service: script.turn_off_fan

Script:

turn_off_fan:
  alias: "Ventilator uitschakelen met vertraging"
  sequence:
    - service: notify.pushover
      data_template:
        message: Grote ventilator wordt binnen {{ states('input_number.big_fan_delay') | int  }} minuten automatisch uitgeschakeld.
    - delay: 
        minutes: "{{ states('input_number.big_fan_delay') | int  }}"
    - service: homeassistant.turn_off
      entity_id: switch.big_fan
    - service: notify.pushover
      data_template:
        message: Grote ventilator automatisch uitgeschakeld.