Timer - Execute and Action from Lovelace UI

I look forward to using it. Will you make this timer and Lovelace UI available in HACS so that it’s easy for community members to install?

I’m trying to get it in the main code base, if it’s not accepted then I might try to get it in HACS. https://github.com/home-assistant/home-assistant/pull/28593#

1 Like

Sorry to bother you again, but what would be a quick equivalent of this code?

# add all the switches with "relay" in their name
group_entities = hass.states.get('group.all_switches').attributes['entity_id']
timer_entities = []
for e in group_entities:
	if "relay" in e:
		timer_entities.append(e)

# append light entities
timer_entities.extend(hass.states.get('group.all_lights').attributes['entity_id'])

I’m assuming that you are trying to get all of your switches and lights into the list? Did HA finally pull those from automatically being created? You can recreate them by adding them to your config.

You can add it in the config file or a separate file and do an include. https://www.home-assistant.io/docs/configuration/splitting_configuration/

Just make a new group and add the entities to it.

Thanks for the suggestions. I didn’t know I could do that (goes to show how much I still have to learn!). It’s not as generalized as “all_switches” or “all_lights”, so it means I have to maintain the list, but that’s fine and may work out in the long term to avoid getting undesirable entities on the list. Thanks again!

Yeah. Pros and Cons. I think that’s why they decided to get rid of it. Devices could go in there that you really don’t want in there. In this case of creating a drop down, it’s a non-issue, but it could in an automation or something.

You’re welcome. Cheers :beers:

Does this timer work with Hass.io running Home Assistant 0.107.3. I think I have everything except I do not know where to put my lights or switches. I saw you posted that you can edit the python_script to assign the group(s) that you want to populate. By default, it’s set to group.lights. Basically I just want to have a drop down for my lights and smart plugs. Sorry if this is a noob question.

here is my Lovelace card

Yes, this setup works in hass.io 0.107.3. However, the group.lights group is no longer automatically created, so you’ll have to define a group of the lights you want to control. Do that and replace the group name in the python_script. Then either reload your groups and run the python_script.populate_timer_entity_id script from the developer tools > services menu or reboot (if you set up the automation correctly to automatically run the script at boot).

I was able to figure it out, I had my group setup incorrectly. It should be

group:
  lights:
    entities:
      - light.002007175ccf7fba1f90
      - switch.air_purifer

I am sorry to bother you again but would you mind explaining the python script in more detail. I created a file named populate_timer_entity_id.py and placed it in a folder called python_scripts. Then I eddied that file and included this:

# pull all entities in the group group.lights
group_entities = hass.states.get('group.lights').attributes['entity_id']
timer_entities = []
for e in group_entities:
    timer_entities.append(e)
timer_entities.append('climate.upstairs')
timer_entities.append('script.party_time_is_over_go_home')
service_data = {'entity_id': 'input_select.timer_entity_id',
                'options': timer_entities}
hass.services.call('input_select', 'set_options', service_data)

Am I doing this correctly? I think I got group.lights group setup correctly. but now I am not sure. I put in my configuration.yaml this entry:

# Example configuration.yaml entry
light:
  - platform: group
    name: Lights
    entities:
      - light.living_lights
      - light.bedroom_lights
      - light.kitchen_lights
      - light.hallway_lights

Thanks for any help. I really want to get this working

Thank you so much for this and for getting back to me. This is exactly what I have been looking for. Everything works but I don’t get any information on the right showing Time Status. Also I do not have a cancel button like in your original screenshot of your timer. But if I click on the timer after it has started I have options to pause, Cancel, or Finish. Probably something to do with my markdown-mod. Either way I am extremely happy now. Thank you

I am trying to use this to be able to set two separate timers to control two of my switches. I am stuck at the point to run python scripts, When I run the populate_timer_entity_id.py script in services it doesnt seem to do anything? I havent edited it yet, what part of that python script do I change to make it see my switches?

I was able to get it working after modifying the python file with samba, I added it with file configurator and it didnt work. I modified my group, and the switch to append and it worked.

I am having trouble getting it to turn off a group of switches, I am getting error in my log:
[HA] Timer X: Error executing script. Service not found for call_service at pos 1: Unable to find service group/turn_off
anyway for me to ad the service group/turn off?
Also, is there any way to activate the timer via Appdameon Dashboard buttons, I have tried to add a button with:
script.timer_x_init
with no luck.

thanks for all the work on this, great timer that I was able to modify to meet my needs.

Hi @redeye_joe, glad to hear that you were able to figure out how to edit the python script. Based on your error, it sounds like the service group.turn_off either doesn’t work anymore or it never did and I just never noticed it :thinking: In your timer_x_execute script replace the domain_name[0] in the service_template with 'homeassistant'. It’s a generic domain that should work with everything. I was unaware of it when I wrote this.

timer_x_execute:
  sequence:
  # execute recorded action
  - service_template: >-
      {%- if not timer -%}
        {%- set timer = 'timer.timer_0' -%}
      {%- endif -%}
      {%- set domain_name = timer.split('.') -%} 
      {%- set read_action = 'input_text.'+ domain_name[1] + '_task' -%}
      {%- set action = states(read_action).split(' ') -%}
      {% set domain_name = action[1].split('.') %}
      {{- 'homeassistant' -}} {{-'.'-}} {{- action[0] -}}
    data_template:
      entity_id: >-
        {%- if not timer -%}
          {%- set timer = 'timer.timer_0' -%}
        {%- endif -%}
        {%- set domain_name = timer.split('.') -%} 
        {%- set read_action = 'input_text.' + domain_name[1] + '_task' -%}
        {%- set ent_id = states(read_action).split(' ') -%}
        {{-ent_id[1]-}}
  # clear stored action
  - service: input_text.set_value
    data_template:
        entity_id: >-
          {%- if not timer -%}
            {%- set timer = 'timer.timer_0' -%}
          {%- endif -%}
          {%- set domain_name = timer.split('.') -%} 
          {{"input_text."}}{{ domain_name[1] }}{{"_task"}}
        value: 'unknown'

Let me know if this works for you. I don’t have access to a quick way to test this right now. If it works for you, I’ll update the script in my original post for others.

Wanted to also contribute to this.
You could update the values of the list using a more simple approach with the following script:

update_options:
  alias: Update options
  sequence:
  - service: input_select.set_options
    data:
      options: "{{ states.light | map(attribute='entity_id') | list }}"
    target:
      entity_id: input_select.timer_entity_list
  mode: single
  icon: mdi:cog-refresh

This is dynamically generated and will set the values for all the “lights” on your HA instance