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.
finity
April 16, 2019, 4:53pm
2
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
finity
April 17, 2019, 5:00pm
4
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_
finity
April 17, 2019, 6:48pm
6
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] }}
finity
April 17, 2019, 9:27pm
8
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.
123
(Taras)
April 17, 2019, 11:31pm
9
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}}"
finity
April 18, 2019, 3:02am
10
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.
123
(Taras)
April 18, 2019, 4:35am
12
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}}