Data Template not working in an Automation

I can’t figure out why this automation is not working:

alias: 'Telegram command lights'
initial_state: 'on'
trigger:
  - platform: event
    event_type: telegram_command
    event_data:
      command: '/lights'
condition:
  - condition: template
    value_template: "{{ trigger.event.data.args|length == 0 }}"
action:
  - service: telegram_bot.send_message
    data_template:
      message: >
        {% set domain = 'light' %}
        {% set entities = states['light'] | selectattr('state', 'eq', 'on') | map(attribute='name') | list | join(', ') %}
        
        The following lights are on: {{ entities }}

It gets triggered, but I actually don’t receive anything. If I try with another operation (e.g. select some specific entity instead) it works just fine.

In the Developer tools section the template works just fine and provides the expected information.

Any ideas?

Thanks!

Have you tried debugging it?

I think there are some superfluous things in the template.

try this:

action:
  - service: telegram_bot.send_message
    data:
      message: >
        The following lights are on: {{ states.light | selectattr('state', 'eq', 'on') | map(attribute='name') | list | join(', ') }}
1 Like

I tried debugging it but I could see no warning or error showing what was going on.

This fixed it!
Thanks!

The only thing I had to change was replacing data_template by data.