Set timer duration through input number

Hello everybody,
I have set-up an automation that automatically turns off the bathroom fan if it has been on for more than 15 minutes:

  - alias: Bathroom fan timer
    initial_state: true
    trigger:
        platform: state
        entity_id: fan.bathroom_ventilation
        to: 'on'
        for:
          minutes: 15
    condition:
        condition: and
        conditions:
        - condition: state
          entity_id: input_boolean.bathroom_ventilation_limit_15_min
          state: 'on'
    action:
        service: homeassistant.turn_off
        entity_id: fan.bathroom_ventilation

I would like now to make the 15 minutes a parameter that I can set through a slides in lovelace. Does anybody have a suggestion on how to approach the problem, in order to have a slider “feed” the
minutes: 15 variable inside the code?

input_number:
  bathroom_fam_limit_minutes:
    min: 0
    max: 20

Thank you very much!

I’m working on a PR that will add templating support to the state & numeric_state triggers’ for option. I’ve already submitted one for the template trigger, and it has been accepted. (Actually, it was two: 24810 and a follow up, 24893.) Once these make it to a release this will be much simpler.

In the meantime it can be done with a timer and a couple more automations. But if you can wait for the release with templating support in the for options, like I said, it will be much easier.

UPDATE: 24912 submitted to add template support to state trigger’s for option.

UPDATE: 24955 submitted to add template support to numeric_state trigger’s for option.

1 Like

@pnbruckner: I have tried your solution with an udated versione of home-assistant, but I cannot get it to work. What am I doing wrong?

automations.yaml:

  - alias: Irrigazione balconi timer
    initial_state: true
    trigger:
        platform: numeric_state
        entity_id: switch.irrigazione_balconi
        to: 'on'
        for:
          seconds: "{{ states('input_number.irrigazione_balconi_limit_seconds.seconds')|int }}"
    condition:
        condition: and
        conditions:
        - condition: state
          entity_id: input_boolean.irrigazione_balconi_limit
          state: 'on'
    action:
        service: homeassistant.turn_off
        entity_id: switch.irrigazione_balconi

configuration.yaml:

input_number:
  irrigazione_balconi_limit_seconds:
    min: 0
    max: 120 
input_boolean:
  irrigazione_balconi_limit:

I have also tried this, taken from the hass documentation, but this still doesn’t work:

  - alias: Irrigazione balconi timer
    initial_state: true
    trigger:
        platform: state
        entity_id: switch.irrigazione_balconi
        to: 'on'
    condition:
        condition: and
        conditions:
        - condition: state
          entity_id: input_boolean.irrigazione_balconi_limit
          state: 'on'
    action:
      - delay: "00:{{ states('input_number.irrigazione_balconi_limit_minutes.minutes') | int }}:{{ states('input_number.irrigazione_balconi_limit_seconds.seconds') | int }}"
      - service: switch.turn_off
        entity_id: switch.irrigazione_balconi

Problem solved!

  - alias: Irrigazione balconi timer
    initial_state: true
    trigger:
        platform: state
        entity_id: switch.irrigazione_balconi
        to: 'on'
    condition:
        condition: and
        conditions:
        - condition: state
          entity_id: input_boolean.irrigazione_balconi_limit
          state: 'on'
    action:
      - delay: "00:{{ states('input_number.irrigazione_balconi_limit_minutes') | int }}:{{ states('input_number.irrigazione_balconi_limit_seconds') | int }}"
      - service: switch.turn_off
        entity_id: switch.irrigazione_balconi

Well in the first automation you posted you had the trigger platform incorrect, which you fixed in the second post.

But in both cases your templates are incorrect. And, FWIW, your condition is more complicated than it needs to be.

Try:

  - alias: Irrigazione balconi timer
    initial_state: true
    trigger:
      platform: state
      entity_id: switch.irrigazione_balconi
      to: 'on'
      for:
        seconds: "{{ states('input_number.irrigazione_balconi_limit_seconds')|int }}"
    condition:
      condition: state
      entity_id: input_boolean.irrigazione_balconi_limit
      state: 'on'
    action:
      service: homeassistant.turn_off
      entity_id: switch.irrigazione_balconi

So this will not work the same as using the trigger’s for option. If the switch is turned on, and then off, and then back on before the delay completes, it will not trigger the automation to start again. Rather, it will abort the delay and turn the switch off immediately.

However, if you use the trigger’s for option then it will not turn off the switch until the switch has been turned on and stays on for the specified amount of time. So in the scenario above, it will turn the switch off the specified amount of time after the second time it turns on instead of immediately after the second time it is turned on.

I think you want this:

  - alias: Irrigazione balconi timer
    initial_state: true
    trigger:
      - platform: state
        entity_id: switch.irrigazione_balconi
        to: 'on'
        for:
          minutes: "{{ states('input_number.irrigazione_balconi_limit_minutes') | int }}"
          seconds: "{{ states('input_number.irrigazione_balconi_limit_seconds') | int }}"
    condition:
      - condition: state
        entity_id: input_boolean.irrigazione_balconi_limit
        state: 'on'
    action:
      - service: switch.turn_off
        entity_id: switch.irrigazione_balconi
3 Likes

Hi. Here: [Manual timer in frontend]

Is a solution which helped me with a similar problem. When you trigger a timer to start you also can set a duration. And if you use data_template, you can set the new duration from an input_number.

1 Like

I’m very sorry - I completely forgot to respond! @swingerman solution worked great! Many thanks to all of you.