Set number of hours in automation trigger by use of input_number

Is there any way of setting the number of hours in the trigger in the automation below, by use of an input_number slider?

- id: '1549643044151'
  alias: Ventilation reset input boolean (min)
  trigger:
  - entity_id: input_boolean.vent_min
    for: 04:00:00
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - data:
      entity_id: input_boolean.vent_min
    service: input_boolean.turn_off

I tried to make an input_number with range 1 to 6, and step 1, and include this in the automation by using following code:

- id: '1549643044151'
  alias: Ventilation reset input boolean (min)
  trigger:
  - entity_id: input_boolean.vent_min
    for: '{{ states.input_number.vent_min_timer.state | int }}:00:00'
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - data:
      entity_id: input_boolean.vent_min
    service: input_boolean.turn_off

But that results in the following error:

Invalid config for [automation]: offset {{ states.input_number.vent_min_timer.state | int }}:00:00 should be format 'HH:MM' or 'HH:MM:SS' for dictionary value @ data['trigger'][0]['for']. Got None. (See /config/configuration.yaml, line 388).

  trigger:
  - entity_id: input_boolean.vent_min
    for: '04:00:00'
    from: 'off'
    platform: state
    to: 'on'

or

  trigger:
  - entity_id: input_boolean.vent_min
    for:
      hours: 4
    from: 'off'
    platform: state
    to: 'on'

I use a script and a delay in the script. My automation turns on a script when triggered, and the action starts a script where the delay is the input number value. When the script ends (delay is over) it turns my switch off. This also allows me to stop the script or “cancel” it. The delay is not dynamic though, so if I change the input number while the script delay is running, it will continue with whatever value it started with.

I dont see how this will let me use input_number to define number of hours?

got it, read too fast. Look at @squirtbrnr’s post above

Thanks for the suggestion. Will take a look into scripts.

that’s because triggers do not support templating, they are straight.

Do I get it right that you want to switch your fan off after a number of hours set by input_number?
If so, I think there is another way to do that.

  1. The automation that reacts to input_boolean off->on writes current time into input_datetime entity
  2. You need a template binary_sensor with entity_id: input_boolean, input_datetime (and sensor.time, for example, to update it every minute). In value_template return now() | as_timestamp - input_datetime | as_timestamp < input_number.state
  3. An automation that turns input_boolean off (and resets to None input_datetime) on that binary_sensor on → off

I think it might do the job.
Actually, instead of the binary_sensor you can have a time_pattern automation with condition: platform: template where you will do exactly what that value_template did.

Another option is to use async_call_later service in a python script, but I have no idea if it will work at all.

Let us know how it goes.

I think this would work. If boolean is on and the difference in time is greater than or equal to the input_slider in hours then true. Maybe check in the template editor to see what you get.

  trigger:
  - platform: template
    value_template: >
      {% set bool = states('input_boolean.vent_min') %}
      {% set now = as_timestamp(now()) | int %}
      {% set last_changed = as_timestamp(states.input_boolean.vent_min.last_changed) | int %}
      {% set limit = states('input_number.vent_min_timer') | int %}
      {% if bool == 'on' and now - last_changed >= ( limit * 3600 ) %}true
      {% else %}false
      {% endif %}

Thanks for the replies AhmadK and walrus_parka! I actually made it work by using a script as suggested by squirtbrnr.

vent_min_timer:
  alias: Ventilation input boolean min timer
  sequence:
  - delay:
      hours: '{{ states(''input_number.vent_min_timer'') | round() }}'
  - service: input_boolean.turn_off
    data:
      entity_id: input_boolean.vent_min

mate, I think it’s even easier than you 2 suggest.

- id: '1549643044151'
  alias: Ventilation reset input boolean (min)
  trigger:
    - entity_id: input_boolean.vent_min
      from: 'off'
      platform: state
      to: 'on'
  condition: []
  action:
    - delay:
      hours: '{{ states("input_number.vent_min_timer") | round() }}'
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.vent_min

Sorry, that didn’t work.

Invalid config for [automation]: [delay] is an invalid option for [automation]. Check: automation-&gt;action-&gt;0-&gt;delay. (See /config/configuration.yaml, line 395). Please check the docs at https://home-assistant.io/components/automation/

don’t copy-paste, it’s yaml.
the delay bit should be the same as in your code here, I just moved your for statement into action.