Help with template for off timer (for each switch / light / etc.)

Hi everybody,

I am trying to create a card that has a dropdown menu containing all switches (and one for all lights), as well as a input_number slider for (for example) 5 - 60 minutes. So that I can select an entity that I want to switch off in x minutes, then enter the amount of minutes via slider, and then automatically turn that entity off when it’s time.

Example

  • value dropdown entity_off_timer = switch.arbeitszimmer_ventilator
  • value input_number.time_off_timer = 30 (defaulting to minutes)

My automation would have to trigger whenever entity_off_timer changes its value; then just put a delay of 00:{{ input_number.time_off_timer }}:00 (1-59 as it is minutes), and action <kind_of_entity>.turn_off with entity_id: "{{ state('entity_off_timer')".

So the automation would have to grab the device to turn of, the amount of minutes when to turn it off, and perhaps the type of entity (even though I could use different cards for switch, lights, etc., so each automation would “know” that service to use rather than having to determine it depending on the type). Since I will use this mostly on switches and perhaps every now and then on lights, two cards won’t be that bad.

(or so I think; if there is a better way, please let me know)

However, I don’t know how to create a dropdown menu listing all switches/lights/scenes/whatever automatically. Can this be done, and if so, how?

I have found this thread, which is similar to what I am trying to achieve; however, I don’t want to do this with a static device but rather be able to switch any device off in x minutes; I can imagine this working pretty well in automations, too).

Thanks in advance for your help :slight_smile:

I don’t think so. This would require dynamic configuration, as when you add or remove entities the configuration for an input select would have to change.

Maybe with a python script or appdaemon?

I’m not sure.

Thank you @tom_l

I am still getting started with Home Assistant and have not yet looked into appdaemon (though it has been on my list since seeing the awesome things people do with it).

Do you think it would instead be possible to declare all devices via jinja2 (or preferably json), then generate the dropdown menu from that (with Home Assistant processes, not external python)? For example

{ "my_devices":
  {
    "Ventilator": "switch.arbeitszimmer_ventilator",
    "PC": "switch.arbeitszimmer_computer",
    "Deckenlicht": "light.arbeitszimmer_deckenlicht"
  } 

And then something like

{% for devices in my_devices %}
  # { (whatever would go here }
{% endfor %}

?

Actually I was wrong. You can dynamically set the input select options see the set_options service here:

So yes it is do-able.

Thank you @tom_l
I must have missed the notification for your reply. Tried it out now, but cannot seem to create a custom timer.

Anybody else in need for such a thing and might want to build it together?

Here are my /config/packages/testing/timer_test.yaml contents (split to leave some room for explaining)

the timer itself

timer:
  timer_test:
    name: TEST Test Timer
    duration: '00:00:05'
    icon: mdi:clock

test trigger

input_boolean:
  test_timer:
    name: TEST Timer
    initial: off
    icon: mdi:clock

automation: when timer runs out, (1) send notification via telegram (just to debug that it worked) and (2) set input_boolean to ‘off’ again

automation:
  - id: "testtest"
    trigger:
      - platform: state
        entity_id: input_boolean.test_timer
        to: 'on'
    action:
      - service: timer.start
        entity_id: timer.timer_test
  - id: "testtest2"
    trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.timer_test
    action:
      - service: notify.me_test
        data:
          message: "Timer okay."
      - service: input_boolean.turn_off
        entity_id: input_boolean.test_timer

The above all works. As soon as I trigger the timer via input_boolean, the timer will count down from 5 to 0 (in unused entities); when this is done, it will send a test notification and set the switch back to ‘off’.

I then tried to set up an input_select for all my switches so that I could dynamically pick an entity that I want the timer to work on; this is a work in progress… I imagine the finished result to be a card that allows you to

  • pick an entity
  • set an off timer (either just minutes and/or hours and minutes)
  • activate the timer via input_boolean (or similar; I just want some dedicated trigger so that if I accidentally chose an incorrect device from the input_select, it will not fire right away but wait until I pressed the button to do so.

I added this part, which is just to see how (or if) things work before doing templates etc.

  - id: 'entitest'
    trigger:
      - platform: event
    action:
      - service: input_select.set_options
        data:
          entity_id: input_select.switches
          options: ["arbeitszimmer_ventilator", "unknown_dingens"]

I assumed that this would create an input_select with each value declared in options as an option (see docs here).

The reason for naming it input_select.switches was that I wanted to first create a timer each for lights, switches, and perhaps groups, just because they all require different services (for example, switch.turn_off for switches, light.turn_off for lights, etc.).
This entity does not show up, though.

As I said before, I was going to set up the options manually before testing templates, with templates, I thought of using expand, then putting anything I’d want to control via this timer in a group. For example {{ expand('group.all_lights') | selectattr('friendly_name') }}; however, while {{ expand('group.all_lights') }} will display an overview of all entities in this group (as well as their attributes etc.), I was not able to get a list of all their entity_ids and perhaps friendly_names.

I was hoping that it’d be possible to create an input_select displaying the friendly_name value while using the entity_id value. So that I’d have a dropdown list like

  • AZ Ventilator
  • AZ Computer
  • SZ Glotze

that would not use these values in the automation, but instead the appropriate entity_id per friendly_name, so in this case (same order as above)

  • switch.arbeitszimmer_ventilator
  • switch.arbeitszimmer_computer
  • switch.schlafzimmer_tv

I will keep testing different things to see if I can first create a timer for manually inserted options (as entitest above), and, if this works, automatically insert those options via template. If anybody is interested in this, please comment here, perhaps we can figure it out together (I started Home Assistant not too long ago, so I am still getting into yaml and the logic it uses).

Did you make any progress here?
I’m want so m to do the same.

List an my lights in an input select drop down, set a timer and then turn this light off with a transition.