tilz0R
(Tilen)
April 14, 2025, 5:12pm
1
I have an automation that is triggered on a state change by many entities:
triggers:
- trigger: state
entity_id:
- event.button_1_action
- event.button_2_action
- event.button_3_action
- event.button_4_action
- event.button_5_action
- event.button_6_action
- event.button_7_action
- event.button_8_action
actions:
- variables:
switch_entity_name: switch.my_output_{{ trigger.entity_id | ... }}
I am looking at a variable trigger.entity_id
to get the entity that triggered the automation. And I would like to switch the same output number.
How could I extract the number from the entity name with regex and put it to variable?
IgorZg
(Igor Simić)
April 14, 2025, 5:49pm
2
You can give ID to each trigger, and this id can be just number. In that way, you will avoid messing with string operations in jinja.
You could use eight separate triggers with trigger ids, but I get why you’d prefer to just have all the entities listed in one trigger. To get the number from the entity_id of the trigger you can use
{{ trigger.entity_id.split("_")[1] }}
tilz0R
(Tilen)
April 14, 2025, 9:41pm
4
That was the easiest, thanks.
You got the point, having many triggers to perform respective actions with outputs with same name, copy/paste is a nightmare for me.
Edit: I found the way with regex_findall
and a matching group:
{{ 'event.button_8_action' | regex_findall(find='.*_(\d+)_.*') | first }}
which returns 8
or any other number present.
tom_l
April 14, 2025, 11:21pm
5
That’s exactly what this does but without the overhead of a regex search.