Timer slider for lights off

Firstly, let me first declare that I’m an absolute noob with this.

I’m trying to get a timer slider that can be turned off and on for lights to go off/on at certain times. I’ve read through the tutorials/helps/shared projects, but can’t for the life of me figure out what code/scripts/automations go where.

If someone who has the patience of a deity would be so kind as to guide me through the process, it would be greatly appreciated. Or point me in the right direction so I could learn about the different yaml files I need to code into

A timer slider?

Like, a duration? (lights stay on for 30 minutes)

Or a time picker so the lights turn on at time X, and off at time Y?

I have the patience of Cerus, so I can help!

1 Like

hi jocnnor!

Thanks so much for taking the time to help me!

I mean a timer for it to turn off in xxx minutes when triggered.

Sure. I’m going to be linking all of the documentation so you can hopefully learn the process. One of the hardest things (other than learning YAML and JINJA2) is knowing what to google to find the right documents.
For a slider, the easiest thing to do is to create an input_number.

input_XXX are easy ways for you to input variables INTO home assistant with direct support. 95% of the other things, there is no way to manually change the state.

Here are a list of the various input things you can easily create

input_number:
  # This one is currently in minutes with a step size of 30 seconds. 
  # Read the documentation to see what kind of resolution you might
  # want. The only thing that really matters here is how it feels on the
  # user interface. 
  # If you did seconds, you'd have to start doing mental math when you
  # want to leave lights on for multiple minutes. 
  # Don't set an initial value unless you want it to reset every reboot.
  light_timer_minutes:
    name: "Light Timer"
    min: 1
    max: 1440
    step: 0.5

After a config check and reboot, you can now straight up add that to lovelace. It will give you a slider that you can set. It will start at 1.0, and will remain at whatever you end up setting it to.

Create one of those for each group of things you might want to control with different off times. I think all lights with auto shut off should be fine following the same slider.

In addition to being able to control it on lovelace, we can now use it as a normal sensor for our needs. Here’s an example automation that will auto shut off the lights in the trigger in this amount of time WHENEVER they turn on (from any method).

 # Add this to your automation section
- alias: auto_light_timer
  trigger:
    # Each light you want to do this, add another trigger.
    - platform: state
      entity_id: light.bathroom_light
      to: 'on'
    - platform: state
      entity_id: light.master_bath_light
      to: 'on'
    - platform: state
      entity_id: switch.random_switch
      to: 'on'
  action:
    # Wait template will sit here until this evaluates to 'True', OR until the timeout happens
    # (if you specify a timeout).
    # is_state returns True or False if the entity id matches the state you ask it to compare.
    # So, wait here until the light is off
    - wait_template: "{{ is_state(trigger.entity_id, 'off') }}"
      # Convert our input number to seconds (because we allow 0.5 steps)
      timeout: 
        # Truncate to the nearest whole second. The result calculation needs to 
        # be an integer.
        seconds: "{{ (states(input_number.light_timer_minutes) | float * 60.0) | int }}"
    # Check the current state. Because we waited, the light could have been shut off
    # manually. There's no need to turn it off again (though it technically wouldn't hurt.
    # If a condition in an action sequence is false, the entire action stops right here.
    - condition: template
      # No need to continue unless the light is still on
      value_template: "{{ is_state(trigger.entity_id, 'on') }}"
    # Use homeassistant.turn_off here so we can handle any trigger that can be turned on/off. 
    # We could use a service_template and grab the domain (light, switch, etc) and call 
    # that specific turn off function, but this one takes care of all of that for us!
    - service: homeassistant.turn_off
      # We specify data_template here if any of the fields are going to be a template. 
      # In this case, entity_id will be a template because we are handling multiple
      # lights here in one automation.
      data_template:
        entity_id: "{{ trigger.entity_id }}"
       
      

Here are some links to the various things I used.
homeassistant.turn_on
wait_template
trigger.to_state
is_state()

And there you have it. Any light/switch/input_boolean/BLAH that you add to the triggers there will turn off automatically after X seconds defined by the input slider.

I don’t expect you to 100% understand those templates. But hopefully they make sense what they are doing with the comments.

4 Likes

thank you so much! With your help i’ve actually learnt to write some other functions for my HA :smiley: still working on this one though

Hey @Albert_Lin, great to hear you are making some progress. The initial learning curve can be tricky at times but its worth sticking with it!

You might be interested in an article I wrote which explains step-by-step how to create a timer in Home Assistant, the example code is for a timed light :slight_smile:

thank you! it’s actually quite amazing how the help I’ve gotten just on this one topic made me more aware of how to do OTHER stuff in HA!

btw, does anyone have an OCD like I do, where not being able to figure something out causes many sleepless nights???

Hello,

I tried to import this to my hassio. I can see the slider in lovelace
but when adding code to my automations.yaml i got this error:

Invalid config for [automation]: expected float for dictionary value @ data[‘action’][0][‘timeout’][‘seconds’]. Got None.

  • alias: auto_light_timer
    trigger:
    • platform: state
      entity_id: light.vtaklampa
      to: ‘on’
      action:
    • wait_template: “{{ is_state(trigger.entity_id, ‘off’) }}”
      timeout:
      seconds: “{{ (states(input_number.light_timer_minutes) | float * 60.0) | int }}”
    • condition: template
      value_template: “{{ is_state(trigger.entity_id, ‘on’) }}”
    • service: homeassistant.turn_off
      data_template:
      entity_id: “{{ trigger.entity_id }}”

The log is zero based, so data[‘action’][0] is saying the problem is with your first Action.

The posted code-block needs some quotes where the state object is used.
e.g.
"{{ is_state(trigger.entity_id, 'on') }}"
should be:
"{{ is_state('trigger.entity_id', 'on') }}"

and
"{{ (states(input_number.light_timer_minutes) }}"
should be:
"{{ (states('input_number.light_timer_minutes') }}"

Thank you, but it seems like the error just moved to another place.

Invalid config for [automation]: extra keys not allowed @ data[‘action’][0][‘seconds’]. Got None
offset None should be format ‘HH:MM’, ‘HH:MM:SS’ or ‘HH:MM:SS.F’ for dictionary value @ data[‘action’][0][‘timeout’]. Got None.

FYI, Check out this post to see how to properly post a code-block.

The example given above had some errors which I commented on. Your own should look something like this:

- alias: auto_light_timer
  trigger:
  - platform: state
    entity_id: light.vtaklampa
    to: 'on'
  action:
  - wait_template: '{{ is_state("trigger.entity_id", "off") }}'
    timeout: '{{ (states("input_number.light_timer_minutes") | float * 60.0) | int }}'
    continue_on_timeout: true
  - condition: template
    value_template: '{{ is_state("trigger.entity_id", "on") }}'
  - service: homeassistant.turn_off
    data:
      entity_id: '{{ trigger.entity_id }}'

Oh, sorry. I will use the method when posting code-block.

I’m about to give up, it still says there are some formatting errors.

Invalid config for [automation]: offset {{ (states("input_number.light_timer_minutes") | float * 60.0) | int }} should be format 'HH:MM', 'HH:MM:SS' or 'HH:MM:SS.F' for dictionary value @ data['action'][0]['timeout']. Got None.

The whole code:

- alias: auto_light_timer
  trigger:
      platform: state
      entity_id: light.vtaklampa
      to: 'on'
  action:
  - wait_template: '{{ is_state("trigger.entity_id", "off") }}'
    timeout: '{{ (states("input_number.light_timer_minutes") | float * 60.0) | int }}'
    continue_on_timeout: true
  - condition: template
    value_template: '{{ is_state("trigger.entity_id", "on") }}'
  - service: homeassistant.turn_off
    data:
      entity_id: '{{ trigger.entity_id }}'

Don’t give up, you’re almost there. It looks like the template for the timeout is the problem… give me a few and I’ll figure out what works.

The documentation for the Wait Timeout suggest that you should be able to specify the value in seconds as a template:

# Wait for IFTTT event or abort after specified timeout.
- wait_for_trigger:
    - platform: event
      event_type: ifttt_webhook_received
      event_data:
        action: connected_to_network
  timeout:
    minutes: "{{ timeout_minutes }}"
  continue_on_timeout: false

I use the UI editor and automations.yaml simultaneously to verify how the UI editor formats the final code. When I try to put the seconds: as a template it converts
this:

- wait_template: '{{ is_state("trigger.entity_id", "off") }}'
  timeout: 
    seconds: '{{ (states("input_number.light_timer_minutes") | float * 60.0) | int }}'

to:

- wait_template: '{{ is_state("trigger.entity_id", "off") }}'
  timeout: '[object Object]'

I’ll need to ask for help on that myself :upside_down_face:

We need to convert the seconds to 00:00:00 (HH:MM:SS) format, as per this example:

# Wait for sensor to change to 'on' up to 1 minute before continuing to execute.
- wait_template: "{{ is_state('binary_sensor.entrance', 'on') }}"
  timeout: '00:01:00'

This works for me:

- wait_template: '{{ is_state("trigger.entity_id", "off") }}'
  timeout: '{{ ((states("input_number.light_timer_minutes") | float * 60.0) | int) | timestamp_custom("%H:%M:%S", False) }}'
  continue_on_timeout: true

The entire automation:

- alias: auto_light_timer
  trigger:
  - platform: state
    entity_id: light.vtaklampa
    to: 'on'
  action:
  - wait_template: '{{ is_state("trigger.entity_id", "off") }}'
    timeout: '{{ (states("input_number.light_timer_minutes") | float * 60.0) | int) | timestamp_custom("%H:%M:%S",
      False) }}'
    continue_on_timeout: true
  - condition: template
    value_template: '{{ states(trigger.entity_id) == "on" }}'
  - service: homeassistant.turn_off
    data:
      entity_id: '{{ trigger.entity_id }}'
  mode: restart

Hello Everyone, maybe someone could help me with this one which I believe is related to this question:

I am trying to create a button in lovelace that starts a timer and the timer duration will be an input number like this:

type: button
entity: timer.first_floor_bedroom_lights
tap_action:
  action: call-service
  service: timer.start
  service_data:
    entity_id: timer.first_floor_bedroom_lights
    duration: 
      minutes: "{{ states('input_number.first_floor_bedroom_lights_slider') |int }}"

In my config I do ahve this variables:

############################################################################

timer:
  first_floor_bedroom_lights:
    duration: "00:05:00"
    name: First Floor Bedroom Lights Timer
    icon: mdi:timer
############################################################################

input_number:
  first_floor_bedroom_lights_slider:
    name: First Floor Bedroom Lights Input Number Slider
    initial: 0
    min: 0
    max: 10
    step: 1
    mode: slider

I am getting the error:

"Failed to call service timer/start. expected float 2data['duration']['minutes']"

Anyone knows what I might be missing?

The idea is to include this button near my lights. For instance, I have rgb lights, usually put them red when I am in bed watching tv, then I just like to leave them on as well as the tv while falling asleep. In that situation just like I put a timer in the tv to turn off, I would like to put this timer counting which would trigger an automation when finished to turn off the lights. The reason I would not like to put the timer inside the automation is because I do not like time based automations because of the qeued/reet type of automations that could create issues. This way is just instant automations.