I have 5 buttons. I need one automation where the name of the button (device)(name_by_user) is to be displayed in an Action script of notification.
I know how to find the name by:
{{device_attr(“<device_id>”, “name_by_user”)}}
But ow can i find this <device_id> , Or put it into a variable?
THank!
According to the documentation, device_attr
accepts a device_id or an entity_id.
So if you have the button’s entity_id you can use it in device_attr
to get the value of name_by_user
.
If your automation uses a State Trigger, referencing all 5 buttons by their entity_id, you can use trigger.entity_id
to get the entity_id of whichever one of the 5 buttons was pressed.
If your automation doesn’t work like that, please post it so we can see how you have designed it to work.
Yes exactly. No state triggers. I just want to get the device_id of a button selected in an automation. Then via the found (how?) device_id it needs to be given as a variable trough to the notification script. Is there a code to get a device_id from a device selected in a trigger?
You need to either explain or show what you mean by “a button selected in an automation”.
As Taras already asked, please post your automation configuration so we can see what you are doing.
If you are using Device triggers it will also be helpful if you cause the trigger to fire and the share the debug Trace from that test. Device triggers do not have predefined sets of available trigger data, they inherit the variables of the underlying event or state trigger that they are using “behind the scenes”.
Like Didgeridrew said, you will have to show the automation (in YAML) that you are using to do this: “a device selected in a trigger”.
At bare minimum, what kind of trigger does the automation use? If not a State Trigger then is it a Device Trigger?
Depending on the type of Device Trigger, the resulting trigger
variable will have the properties of a State Trigger or Event Trigger.
If we know the type of trigger, then we know what properties the trigger
variable will have and what data (entity_id or device_id) can be passed to the notification action (or script).
oke that makes sence, ( sorry)
triggers:
- device_id: a110c1799fa7c538d907968c681a6e44
domain: zha
type: remote_button_short_press
subtype: button
trigger: device
conditions: []
actions:
- action: script.1_mail_zorg_script
Does this help?
Thank you, that helps.
Your automation is using a single Device Trigger, containing a hard-coded device_id, and this type of Device Trigger is like an Event Trigger (so the resulting trigger
variable will have properties like an Event Trigger).
Is your plan to add more Device Triggers to this automation? In other words, a Device Trigger for each of the five buttons for a total of five Device Triggers?
EDIT
If that’s the plan then your automation will look something like this:
alias: example
triggers:
- device_id: a110c1799fa7c538d907968c681a6e44
domain: zha
type: remote_button_short_press
subtype: button
trigger: device
- device_id: <... another_button_device_id_goes_here ...>
domain: zha
type: remote_button_short_press
subtype: button
trigger: device
<... three more Device Triggers ...>
conditions: []
actions:
- action: script.1_mail_zorg_script
data:
dev_id: "{{ trigger.event.data.device_id }}"
The template in script.1_mail_zorg_script
should reference the dev_id
variable that is passed to it.
{{ device_attr(dev_id, 'name_by_user') }}
unfortunally i get : Error: Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘event’
actions:
- action: script.1_mail_zorg_script
metadata: {}
data:
dev_id: "{{ trigger.event.data.device_id }}"
tekstbericht: "{{dev_id}}"
onderwerpmail: "Alarm: - Wat?"
mode: single
Why is it so hard to understand this device object model?
Did you run the automation by pressing the RUN button in the GUI and not by actually pressing the physical button?
i pressed the button and also via run automation. Both give an error…
The error posted above will occur when you press the run button, as there will be no trigger then.
Does pressing the physical button result in the same error?
you are right. Two different errors:
when run automation:
Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘event’
when push button:
Error: Error rendering data template: TemplateError: Must provide a device or entity ID
this is the automation:
alias: Alarm SOS
description: Als persoon op de alarm knop drukt. Worden de mantelzorger gebeld.
triggers:
- device_id: a110c1799fa7c538d907968c681a6e44
domain: zha
type: remote_button_short_press
subtype: button
trigger: device
conditions: []
actions:
- action: script.1_mail_zorg_script
metadata: {}
data:
dev_id: "{{ trigger.event.data.device_id }}"
onderwerpmail: >-
Alarm: - Ik heb op de Alarm knop {{ device_attr(dev_id, 'name_by_user')
}} gedrukt.
tekstbericht: >-
<b style="color:red"><font size="5"> Ik heb hulp nodig !</font></b><br>
<i>(want ik heb op een alarm knop gedrukt)</i><br><br>
It will not work in the template editor, as you need the data provided by the trigger. The same data which will be missing when you press Run in the GUI.
The error you get when pressing the actual physical button is caused because you are referring to the variable dev_id
which you didn’t define yet. You are sending it as a variable to the script, but you don’t define it in the automation.
Try it like this
alias: Alarm SOS
description: Als persoon op de alarm knop drukt. Worden de mantelzorger gebeld.
triggers:
- device_id: a110c1799fa7c538d907968c681a6e44
domain: zha
type: remote_button_short_press
subtype: button
trigger: device
actions:
- variables:
dev_id: "{{ trigger.event.data.device_id }}"
- action: script.1_mail_zorg_script
data:
dev_id: "{{ dev_id }}"
onderwerpmail: >-
Alarm: - Ik heb op de Alarm knop {{ device_attr(dev_id, 'name_by_user')
}} gedrukt.
tekstbericht: >-
<b style="color:red"><font size="5"> Ik heb hulp nodig !</font></b><br>
<i>(want ik heb op een alarm knop gedrukt)</i><br><br>
I doubt you actually need the dev_id
in your script. So you could also simply use
Alarm: - Ik heb op de Alarm knop {{ device_attr(trigger.event.data.device_id, 'name_by_user') }} gedrukt.
and don’t send the dev_id
variable to your script
Actually, the model is straightforward but what has been challenging to understand are some of the error messages you have reported because of the unexpected (and incorrect) ways you have tested the automation.
-
Never use the Run command to test an automation that refers to the
trigger
object. This object only exists when the automation is actually triggered. The Run command isn’t a trigger, it simply executes the automation’s actions. For more information, refer to Troubleshooting your automation. -
The Template Editor is for testing Jinja2 templates and nothing else. It has no understanding of YAML statements.
That was the correct test to perform and produced a useful error message.
What you did with dev_id
is not exactly what I had suggested and that’s the cause of the error.
You cannot refer to the dev_id
variable within Alarm
because dev_id
will only exist as a variable when it is passed to the script.
One way to fix it is how TheFes suggested, by first defining dev_id
as a script variable and then referencing it wherever else it’s needed.
Another way is to simply use trigger.event.data.device_id
wherever it is needed as shown below.
alias: Alarm SOS
description: Als persoon op de alarm knop drukt. Worden de mantelzorger gebeld.
triggers:
- device_id: a110c1799fa7c538d907968c681a6e44
domain: zha
type: remote_button_short_press
subtype: button
trigger: device
conditions: []
actions:
- action: script.1_mail_zorg_script
metadata: {}
data:
dev_id: "{{ trigger.event.data.device_id }}"
onderwerpmail: >-
Alarm: - Ik heb op de Alarm knop {{ device_attr(trigger.event.data.device_id, 'name_by_user') }} gedrukt.
tekstbericht: >-
<b style="color:red"><font size="5"> Ik heb hulp nodig !</font></b><br>
<i>(want ik heb op een alarm knop gedrukt)</i><br><br>
NOTE
You haven’t shown the script’s code so it’s unclear if this part is still needed by the script:
dev_id: "{{ trigger.event.data.device_id }}"
because Alarm
already uses the device_id to get name_by_user
.
Thank you very much for the help and this lesson. I still find it difficult to get good documentation on variables and object collections. This code is working, But why is needed to define dev-id twice?
actions:
- variables:
--> **dev_id:** "{{ trigger.event.data.device_id }}"
- action: script.1_mail_zorg_script
data:
-->**dev_id:** "{{ dev_id }}"
onderwerpmail: >-
This i do not understand. Variable or script Variable is there a difference?
Thanks very much for working on this case and getting good results!.
For me it will take i while to understand this peculiar way of progrmmaing.
Again thanks both (Taras & The fes) very much1
If you do it the way I suggested, you don’t need to define a script variable at all. Try it and you’ll see it works.
This line here, from the action, does not define a variable that is usable in the automation (automation.alarm_os).
dev_id: "{{ trigger.event.data.device_id }}"
It defines a variable that will be usable in the script (script.1_mail_zorg_script). In other words, it passes the variable dev_id
to the script.
I was on mobile yesterday, but this is exactly what I tried to say with the last line of my post