Group members list to send in message

Hi, guys. Sorry for my bad English.
I want to make an automation, that will send by telegram a message, with a list of entities (light), that is turned on now. It must be a name of entity, not ID . Has someone an idea how it’s to make work? Here is an example of automation:

- alias: light_auto_off_nobody_home
  trigger:
    platform: state
    entity_id: person.me
    to: 'not_home'
  condition:
    condition:
       condition: state
       entity_id: group.all_light
       state: 'on'
  action:
    - service: telegram_bot.send_message
      data_template:
        target: xxxxxxxxx
        message: # Here I want to paste a list of entities name in group all_light, that are in 'on' state

If you have the group group.all_light already defined you can use this:

message: >
  {{ expand('group.all_light')|selectattr('state', 'eq', 'on')|map(attribute='name')|list|join(', ') }}

If you don’t have a group called group.all_light already defined then you can use this:

message: >
  {{ states[light]|selectattr('state','eq', 'on')|map(attribute='name')|list|join(', ') }}
message: >
  {{ expand('group.all_light')|selectattr('state', 'eq', 'on')|map(attribute='name')|list|join(', ') }}

Its work perfectly, thank you. I need to improve my template skill.

1 Like