Automation to stop a fan after variable time

Have been trying some different solutions from the forum that i couldn’t get to work and have now tried to do my own solution but got a bit stuck.

I want to turn on a fan. The fan must turn off after the time that i set on a slider.

My first post so i hope the code formatting i correct

Edited: Edited the code input, removed spaces between lines and used ``` befor and after code.

input_number:
  toiletfan_input:
    name: toiletfan_input
    min: 0
    max: 120
    step: 30
    step: 1

timer:
  fan_timer:
    duration: '00:01:00'

script:
  start_fantimer:
    sequence:
      - service: timer.start
        data_template:
          entity_id: fan_timer
          duration: "00:{{ states('input_number.toiletfan_input') }}:00"

automation:
  - alias: 'Toilet fan control'
    trigger:
      - platform: state
        entity_id: fan.toilet_fan
        from: 'off'
        to: 'on'
    condition:
      condition: template
      value_template: "{{  timer.fan_timer  < 1 }}"
    action:
      - service: fan.turn_off
        entity_id: fan.toilet_fan

No, I think you must not have used the </> button to format the code.

I think you are nearly there, the input_number looks okay (but the numbers a bit wild, asuume minutes and go for 1 to 20 but it’s your toilet, step of 30 though ??? ). The automation is a bit off.
There are many ways to do this but the simplest is : -

automation:
  - alias: 'Toilet fan control'
    trigger:
      - platform: state
        entity_id: fan.toilet_fan
        to: 'on'
    action:
      - delay: "00:{{ states('input_number.toiletfan_input') | int }}:00" 
      - service: fan.turn_off
        entity_id: fan.toilet_fan

Do you really have a fan domain ? I just have switches :man_shrugging:
Looking at this I have no idea why I cast the number to int, but I always have

I usually have the fan come on with the light, and go off 10 mins after the light goes out

I was just helping a chap with a similar auto on discord. This should be in the docs more explicitly.

1 Like

Maybe the fan timer value is a string?

I would do this with a script, similar to what petro wrote here. The problem with the delay is when the fan get’s turned on again during the delay. This may never be the case for OP, but for other similar automations. Just my 2 cents xD

1 Like

No I agree, and my light/fan timers use scripts too but I’m pitching this at his level.
Let him get comfortable with that and then we’ll address bypassing timer issues

1 Like

Trug I’m not using a timer here, I’m using a delay
And yes HA stores input numbers as strings.
My point here was that I was just going to use it in a string anyway, so why bother with the int ??? (that’s a rhetorical question BTW)

I did it this way I hope it helps

#scripts.yaml

ventilador:        
   sequence:
    - service: timer.start
      data_template:
        entity_id: timer.ventilador
        duration: '00:{{ states.input_number.timer_minutes3.state | int }}' 

#configuration.yaml

timer:
    ventilador:

input_number:
  timer_minutes:
    name: Minutos
    initial: 0
    max: 180
    min: 0
    step: 1 

#automations.yaml

action:
  - data:
      entity_id: switch.ht
    service: switch.turn_off
- id: '1568856065189'
  alias: Timer ventilador
  trigger:
  - event_data:
      entity_id: timer.ventilador
    event_type: timer.finished
    platform: event
  condition: []
  action:
  - data:
      entity_id: switch.ventilador
    service: switch.turn_off
1 Like

Kobainer, you wouldn’t be a Nirvana fan would you ?

The nickname is more related to my last name, but who diden’t enjoy his music? :slight_smile:

So thanks for the help.

I tried with a delay earlier but didn’t work for me so dont know where i went wrong. It works with the delay now so thanks for that.

Now i will be looking at trying the scipt that pedro made here (thanks @Burningstone)

As to the Domain. Yes i have a fan domain… the domain has a lot of fancy fan funktions. But in reality im just talking to a switch so i might change this.

Regarding the stepsize @Mutt. Yes these were just choosen a bit random. But they will be relatively high until i get my humidity sensor connected. Thnaks for the help :slight_smile:

Don’t try to run before you can walk, get this working and we’ll enhance it.
The point is the format and the way it is constructed, this happens to that, then we call a delay, then we act on this to turn it off. Its not a formula, or a construct from another piece of software, it’s a logical sequence.

petro :wink:

1 Like

Kobainer, did you get it working.?
Have you discovered the issues with it ?
Do you want to adapt/extend it.?
If you post your current code (make sure it looks like mine ie the format) we can work from that point.

It worked really nicely with the delay. Don’t know where i made the mistake first time i tried the delay.

Tonight i’ll look into scenes to expand it. I’ll make a post later when i worked a bit on it myself first… either to ask for help or share how i expanded @Mutt :slight_smile:

Tomorrow my ZigBee hub and humidity sensors arrive. Then i’ll start implementing them aswell.
Tinkering with all this is so much fun, to bad i have to wait for the family to sleep :stuck_out_tongue:

I have a near identical code for my fan, as Mutt had posted, so I’m not going to pin it here. But as far as the reason why your code didn’t work. When I was playing with input sliders I had noticed that I had to cast values to integer with the |int operand otherwise they are coming as floats and the delay will not work. You can test this in the Developer Tools > Template.

This is then particularly relevant when you concatenate values as in 00:{{ states('input_number.toiletfan_input') | int }}:00.

Hope it helps.

So i changed it to use script after petros principle. Works awesome :slight_smile: here the code

input_number:
  toiletfan_input:
    name: toiletfan_input
    min: 0
    max: 60
    step: 10

automation:
  alias: 'Toilet fan control'
  trigger:
  - platform: state
    entity_id: fan.toilet_fan
    from: 'off'
    to: 'on'
  condition: []
  action:
  - service: script.turn_off
    entity_id: script.start_fantimer
  - service: fan.turn_on
    data:
      entity_id: fan.toilet_fan
  - service: script.start_fantimer

script:
  start_fantimer:
    sequence:
    - delay: "00:{{ states('input_number.toiletfan_input') | int }}:00"
    - data:
        entity_id: fan.toilet_fan
      service: fan.turn_off
2 Likes