Telegram message templating

Why this works

  action:
    - service: telegram_bot.answer_callback_query
      data_template:
        callback_query_id: '{{ trigger.event.data.id }}'
        message: "Attivo uscita switch.myreef_relay_"

and this doesn’t?

  action:
    - service: telegram_bot.answer_callback_query
      data_template:
        callback_query_id: '{{ trigger.event.data.id }}'
        message: "Attivo uscita switch.myreef_relay_ {{ trigger.event.data.args[0] }}"

The problem seems to be template related, but as on the guidelines I’ve specified data_template and not data.

I’m not 100% sure but it looks like you have an extra space in your message template that will cause it to fail.

remove the space between the “relay_” & the “{{”. Like this:

message: "Attivo uscita switch.myreef_relay_{{ trigger.event.data.args[0] }}"

Nope… it’s still not working :expressionless:

Ok.

the next question is what is the format of the trigger entity?

To clarify I’m asking if the result of the “{{ trigger.event.data.args[0] }}” template valid?

I know what you mean… yes the result of the template is valid.
The first entry of the args array is a number and it will be appended to the string switch.myreef_relay_

can you post the entire automation instead of just the action so we can see everything in context?

- alias: 'Telegram Relays 8 ON'
  id: 'telegram_relays_on_8'
  initial_state: 'on'
  trigger:
    platform: event
    event_type: telegram_callback
    event_data:
      data: '/relay_on'
  action:
    - service: telegram_bot.answer_callback_query
      data_template:
        callback_query_id: '{{ trigger.event.data.id }}'
        message: 'Attivo uscita switch.myreef_relay_{{ trigger.event.data.args[0] }}'
    - service: switch.turn_on
      data_template:
        entity_id: switch.myreef_relay_{{ trigger.event.data.args[0] }}

You need to wrap the template in the entity_id in quotes.

- service: switch.turn_on
  data_template:
    entity_id: switch.myreef_relay_'{{ trigger.event.data.args[0] }}'

Or the whole thing. I’m not sure which:

- service: switch.turn_on
  data_template:
    entity_id: 'switch.myreef_relay_{{ trigger.event.data.args[0] }}'

but I think it’s the first one.

Here’s an example of a working automation. Notice the entire string assigned to entity_id is delimited by (double) quotes.

- alias: 'Door Opened/Closed'
  trigger:
    - platform: state
      entity_id: binary_sensor.rear_door
  action:
    - service: scene.turn_on
      data_template:
        entity_id: "scene.lights_{{trigger.to_state.state}}"

yeah, couldn’t remember which way it was supposed to be and I couldn’t find a ready example with google.

thanks for clarifying.

Already tried to use quotes, but not the double quotes, I will try it as soon as possible.

In this particular case it doesn’t make a difference if you use single or double quotes to delimit the entire string (my automation works using either one).

So if your automation does not work with either single or double-quotes, then it suggests that {{trigger.event.data.args[0]}} may not be returning the value you think it does.

1 Like

… I’m pretty sure that the return of {{trigger.event.data.args[0]}} is correct but I will check one more time!

that’s correct and because it needs the entity_id to be scene.lights_on or scene.lights_off, and not scene.lights_{{trigger.to_state.state}}, which would be what you’d get quoting the state template.

As I always suggest, and do myself to keep from making mistakes: simply use the multiline notation with >

   - service: scene.turn_on
      data_template:
        entity_id: >
          scene.lights_{{trigger.to_state.state}}