Turning a switch off after a variable / configurable number of minutes selected in user interface

I’d have a ventilation system that is largely automated using Home Assistant. Sometimes I want to turn it on manually and have it turn off after a variable number of minutes - sometimes 5 minutes, sometimes 37 minutes, sometimes 15 minutes.

I’ve read quite a few threads, but I can’t work out how to do this cleanly. Right now I have buttons that trigger automations for turning off in 15 / 30 / 45 minutes, but I want to be able to select any number of minutes, ideally with a nice user interface.

Can anyone point me in the right direction? I’m not a beginner at HA but I’m not super advanced either. I could write a script with a parameter to do this, but I’m not entirely sure how to create the user interface or pass the parameter in to the script.

I’m basically trying to replicate what the TP-Link Kasa does so I can reduce the number of apps I need.

Automation that triggers when you turn your entity on, wait for x minutes according to input number, turn it off.

1 Like

Thanks @FPro that looks very useful.

Reading the documentation, I can see code such as what is copied below (second block). I’m familiar with automations as I have about 50 in my HA install, but I’m more used to creating the user interface elements on a dashboard and the automations in the console, linking them where required with actions. The examples from the documentation put the input_number and the automation in the same file. A few questions:

  • How does that work with both the input and automation in one file? Where do I put it?
  • Can I separate the two so the automation is visible in the usual place with the input_number on the dashboard?
  • If you have a standalone input_number and an automation how do you link the two? I have a few of this type of thing on my dashboards

From my dashboard (this was my clunky first attempt)

              - type: custom:button-card
                entity: switch.ventilation
                color_type: card
                color: rgb(204,50,50)
                icon: mdi:fast-forward-30
                state:
                  - value: 'off'
                    color: rgb(204,50,50)
                tap_action:
                  action: call-service
                  service: automation.trigger
                  service_data:
                    entity_id: automation.ventilation_on_30m

From the input_number documentation

# Example configuration.yaml entry using 'input_number' as a trigger in an automation
input_number:
  bedroom_brightness:
    name: Brightness
    initial: 254
    min: 0
    max: 254
    step: 1
automation:
  - alias: "Bedroom Light - Adjust Brightness"
    trigger:
      platform: state
      entity_id: input_number.bedroom_brightness
    action:
      - service: light.turn_on
        target:
          entity_id: light.bedroom
        data:
          brightness: "{{ trigger.to_state.state | int }}"

If you work in the UI ignore the example.
You create the input number in Settings → Devices & Services → Helpers (Tab).
You create your automation as usual in the UI.

You add a delay as an action in the automation. Edit the action as YAML and add something like this:

delay:
  hours: 0
  minutes: {{states('input_numer.myinputnumber') | int}}
  seconds: 0
  milliseconds: 0

Awesome, thanks.

I’m curious how I would do this outside the UI as well, with everything in one file. I write most of my automations directly in YAML but in the web console. I’ve been using HA for while now, but there’s no great guide that takes you through everything that in aware of.

If you want it in one file do it in configuration.yaml just as in the example, but it is not common practice to put automations directly there. You do it in separate files that you include into configuration.yaml.
It is actually well documented:

Oh, I see, that makes sense now thanks. A lot of things are well documented but I tend to find things as I need them rather than learning up front. I might see if anyone has written a book.

Thanks to Florian I’ve managed to get this going. It’s not difficult, just time consuming to learn all the bits and pieces in HA. The documentation is good, but not always super easy to understand unless you already understand HA.

I’ve copied my code below. It shows control of light.ventilation_system which is actually a smart plug, but I don’t want to mess with it to change it because it’s used in a lot of my automations.

Here’s my configuration.yaml entry that defines the input_number. If you want multiple input sliders you just add more below, without adding input_number again.

input_number:
  ventilation_off_timer:
    name: Ventilation Off Timer
    initial: 0
    min: 0
    max: 90
    step: 5
    icon: mdi:fan
    unit_of_measurement: mins

Here’s my dashboard

views:
  - title: Home
    cards:
      - type: vertical-stack
        cards:
        - type: horizontal-stack
          cards:
            - type: entities
              show_header_toggle: false
              entities:
                - entity: input_number.ventilation_off_timer
                  name: Ventilation
            - type: custom:button-card
              entity: light.ventilation_system
              size: 25px
              show_label: false
              show_state: true
              show_name: false
              show_icon: true
              state:
                - value: 'off'
                  color: rgb(204,50,50)
                  icon: mdi:lightbulb-off
                  label: 'Off'
                - value: 'on'
                  color: rgb(45,201,55)
                  icon: mdi:lightbulb-on
                  label: 'On'
              styles:
                card:
                  - height: 80px
                  - width: 80px
                  - font-size: 10pt

Here’s the automation that listens for changes to the slider. It doesn’t do anything if the value passed in is zero. This automation is triggered every time the slider position is moved. If the slider is moved again the counter is restarted. It could be modified so that if it’s moved to zero than the fan is stopped, but I haven’t done that, as you can just click the on / off icon to stop it.

alias: Ventilation Timer Off Variable
description: Run ventilation for a set number of minutes off based on the value set with the slider
trigger:
  - platform: state
    entity_id:
      - input_number.ventilation_off_timer
condition:
  - condition: template
    value_template: "{{ states('input_number.ventilation_off_timer') | int > 0}}"
action:
  - type: turn_on
    device_id: abc123abc123
    entity_id: switch.ventilation
    domain: switch
  - delay:
      hours: 0
      minutes: "{{ states('input_number.ventilation_off_timer') | int }}"
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: abc123abc123
    entity_id: switch.ventilation
    domain: switch
mode: restart

Here’s what it looks like. The little indicator turns on and off based on the state of the entity, which can be started by anything, even an external app, if you have it configured properly.

image

1 Like

Thanks for this, this was really helpful, im a little surprised someone hasnt made a custom componenet/card for this sort of functionality yet… gonna look into it!