[Resolved] Timer for lights

good morning,
I would like to create a timer with input by input number and input select minutes and the light that must have the timer active, can you help me? because I do not understand how to input input select and input number

I did this, but it does not work:

automation:
- alias: Timer light
trigger:
platform: state
entity_id: light.luce_letto
to: ‘on’
for:
minutes: ‘{{ states.input_number.minuti_luci.state }}’
action:
- service: light.turn_off
entity_id: >-
{% if is_state(“input_select.luci_timer”, “Luce_Letto”) %} light.luce_letto
{% elif is_state(“input_select.luci_timer”, “Lampadario”) %} light.lampadario
{% elif is_state(“input_select.luci_timer”, “Striscia_Led_RGB”) %} light.striscia_led_rgb
{% endif %}

When you say “it does not work”, it’s very helpful if you elaborate on what that means. E.g., did you can an error, and if so, what? Etc.

But my guess is you can’t use a template with for: minutes:.

If I understand, you want to turn a selected light (selected by an input_select) off a certain amount of time (controlled by an input_number) after light.luce_letto is turned on, if light.luce_letto has been on continuously for that time period. Is that correct?

If so, then you could do it with a timer and a few automations:

timer:
  luce_letto_on:
    name: Time light.luce_letto needs to be on

automation:
  - alias: light.luce_letto turned on
    trigger:
      platform: state
      entity_id: light.luce_letto
      to: 'on'
    action:
      service: timer.start
      entity_id: timer.luce_letto_on
      data_template:
        duration: "{{ (states('input_number.minuti_luci')|int(2) * 60)|timestamp_custom('%H:%M:%S',false) }}"

  - alias: light.luce_letto turned off
    trigger:
      platform: state
      entity_id: light.luce_letto
      to: 'off'
    action:
      service: timer.cancel

  - alias: light.luce_letto on for desired time
    trigger:
      platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.luce_letto_on
    action:
      service: light.turn_off
      data_template:
        entity_id: "light.{{ states('input_select.luci_timer')|lower }}"

I can’t really test this, but if this doesn’t work as-is, it should be close and at least a start.

Of course, there may be an easier way to do this. If so, maybe someone else will jump in.

thank you, I try and let me know

it works, but only with light.luce_letto, with the others it does not work, how do I select the others?

I guess I don’t quite understand how you want this to work. Please be more specific about describing what you want to happen.

I would like to be able to select any light via input select and through the input number to select the minutes, the light that I have selected must be turned off in the specified time when I turn it on.
This works only light.luce_letto

Ok. The easy way to do that is to add all the light entities that might be selected by the input_select to the trigger, then add a condition that makes sure the light that turned on was the selected one. So:

timer:
  light_on:
    name: Amount of time light should stay on before being turned off

automation:
  - alias: Light turned on, start the timer
    trigger:
      platform: state
      entity_id:
        - light.luce_letto
        - light.lampadario
        - light.striscia_led_rgb
      to: 'on'
    condition:
      - condition: template
        value_template: >
          {{ trigger.entity_id == 'light.' + states('input_select.luci_timer')|lower }}
    action:
      service: timer.start
      entity_id: timer.light_on
      data_template:
        duration: "{{ (states('input_number.minuti_luci')|int(2) * 60)|timestamp_custom('%H:%M:%S',false) }}"

  - alias: Light turned off, cancel the timer
    trigger:
      platform: state
      entity_id:
        - light.luce_letto
        - light.lampadario
        - light.striscia_led_rgb
      to: 'off'
    condition:
      - condition: template
        value_template: >
          {{ trigger.entity_id == 'light.' + states('input_select.luci_timer')|lower }}
    action:
      service: timer.cancel
      entity_id: timer.light_on

  - alias: Light on long enough, turn it off
    trigger:
      platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.light_on
    action:
      service: light.turn_off
      data_template:
        entity_id: "light.{{ states('input_select.luci_timer')|lower }}"

thank you, I try it

Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/packages/timer_light.yaml”, line 46, column 15

@pnbruckner

Sorry, copy and paste error. Forgot the trigger: line. I’ll fix post above…

I solved thanks everything works, You can say closed the discussion

Glad you got it working!

Actually, since you created this topic, you should click on the “this solved my problem” (or something like that) box in one of the posts. :slight_smile: