Timer in minutes only for a fan

Hi all,
I’m trying to set up a timer using an input_number with more than 60 minutes. Basically I want the laundryroom fan to turn off after x minutes, where x can be greater than 60.
This is what I have tried to far, but the automation with this template does not work if the input_number is set to >60 minutes.
Does anyone have a recommendation on how to automate this?

Thanks

- alias: Laundryroom ventilation timer
  description: ''
  trigger:
  - entity_id: fan.laundryroom_ventilation
    for: '{{ states(''input_number.laundryroom_ventilation_timer'')|int*60 }}'
    platform: state
    to: 'on'
  condition: []
  action:
  - data: {}
    entity_id: fan.laundryroom_ventilation
    service: homeassistant.turn_off
  mode: single

If the input number is set to 60, the fan will turn off after 60*60 = 3600 minutes or 60 hours.

Replace your * with a +.

That way the minimum time is 60 minutes and the maximum is 60 + whatever your input_number maximum is.

@tom_l thank you for the answer. What if my input_number ranges from 10 to 120 minutes? What I’m trying to do is to input one single number (in minutes) to have both an hour and/or a fraction of an hour.

PS: home assistant interprets my current template in seconds, if I input any number from 0 to 60 it is interpreted in minutes in the template with the current formula.

Maybe something like this?

'{{ states.input_number.laundryroom__ventilation_timer.state | int }}:{{ states.input_number.laundryroom__ventilation_timer.state | int*60 }}:00'

Change your input number to be 0 to 59 minutes and add it to 60 in the template. Or change you input number to be 60 to 119 minute and drop the math altogether.

But like this you cannot have a fraction of an hour (e.g. 10 minutes)

Tell me the exact range you want, forget fractions of an hour. What is the minimum, what is the maximum?

From 10 to 240 minutes. With one input_number only though

Then set your input number to have a minimum of 10 and a maximum of 240 and forget the math in your template.

But that will be in seconds, not minutes - am I wrong?

Not if you do this:

for:
  minutes: "{{ states('input_number.laundryroom_ventilation_timer') }}"
1 Like

I would say you will need to do a bit more fancy templating.

I haven’t been able to test it but I think this should work

trigger:
  - entity_id: fan.laundryroom_ventilation
    for: 
      hours: > 
        {% if  states(''input_number.laundryroom_ventilation_timer'')|int > 59 %}
          {{ (states(''input_number.laundryroom_ventilation_timer'')|int / 60 ) | int  }}
        {% else %}
          0
        {% endif %}
      minutes: >
        {% if  states(''input_number.laundryroom_ventilation_timer'')|int > 59 %}
          {{ states(''input_number.laundryroom_ventilation_timer'')|int % 60  }}
        {% else %}
          {{ states(''input_number.laundryroom_ventilation_timer'')|int  }}
        {% endif %}        
1 Like

Wasn’t there a recent change that made minutes over 59 no longer work and you have to use explicit hours & minutes?

Pretty sure that’s time_pattern triggers only. I’ll test it tomorrow. Bed time was hours ago.

2 Likes

@finity I’ll test this tomorrow, @tom_l unfortunately the most simple template did not work

Yeah, I think you’re right.

Maybe this was affected as well and undocumented?

Then again I’ve never templated “for:” before so maybe it has always been that way.

What was the error?

The timer is simply ignored and nothing happens

@finity I get the following error

Invalid config for [automation]: expected int for dictionary value @ data['trigger'][0]['for']['hours']. Got None
expected int for dictionary value @ data['trigger'][0]['for']['minutes']. Got None. (See ?, line ?).

When using the following code:

- alias: Laundryroom ventilation timer
  trigger:
  - entity_id: fan.laundryroom_ventilation
    for: 
      hours: > 
        {% if  states(''input_number.laundryroom_ventilation_timer'')|int > 59 %}
          {{ (states(''input_number.laundryroom_ventilation_timer'')|int / 60 ) | int  }}
        {% else %}
          0
        {% endif %}
      minutes: >
        {% if  states(''input_number.laundryroom_ventilation_timer'')|int > 59 %}
          {{ states(''input_number.laundryroom_ventilation_timer'')|int % 60  }}
        {% else %}
          {{ states(''input_number.laundryroom_ventilation_timer'')|int  }}
        {% endif %}
    platform: state
    to: 'on'
  condition: []
  action:
  - entity_id: fan.laundryroom_ventilation
    service: homeassistant.turn_off
  initial_state: true
  mode: single

I just created some test entities and re-created your automation above and ended up with a similar error.

I found that, strangely (or maybe not now that I think about it… :thinking:), the template interpreter didn’t like the double single quotes. Once I removed one set of single quotes it seems to be working fine (at least for values less than 60…I didn’t want to wait an hour to see if it turned back off :wink:).

So, try this:

- alias: Laundryroom ventilation timer
  trigger:
  - entity_id: fan.laundryroom_ventilation
    for: 
      hours: > 
        {% if  states('input_number.laundryroom_ventilation_timer')|int > 59 %}
          {{ (states('input_number.laundryroom_ventilation_timer')|int / 60 ) | int  }}
        {% else %}
          0
        {% endif %}
      minutes: >
        {% if  states('input_number.laundryroom_ventilation_timer')|int > 59 %}
          {{ states('input_number.laundryroom_ventilation_timer')|int % 60  }}
        {% else %}
          {{ states('input_number.laundryroom_ventilation_timer')|int  }}
        {% endif %}
    platform: state
    to: 'on'
  condition: []
  action:
  - entity_id: fan.laundryroom_ventilation
    service: homeassistant.turn_off
  initial_state: true
  mode: single