I have a group of lights and wanted to check all of them. In case, one of them is switched on, it should flash. This is just something to learn how this is done and to be extend in the future.
So as a first step, I created the upper script and it was nicely logging the name of all entities. Then I tried to extend the script and added a script call. There everything stopped working.
I tried it with “-service” and without. With data(_template), with parameters … nothing works.
#loop over outdoor lights - if switched on, let is flash
flash_generic:
alias: 00 - flash generic
mode: single
sequence:
- data: {}
service: >
{% for entity_id in states.group.outdoorlights.attributes.entity_id -%}
{%- if states(entity_id) == 'on' -%}
- service: script.flash_specific
data_template:
val1: {{entity_id}}
{%- endif -%}
{%- endfor %}
#currently only log the message to verify that the correct value has been transferred
flash_specific:
alias: 01 - flash specific
mode: single
sequence:
- service: notify.tplogger
data_template:
message: "{{val1}}"
The first script is attempting to do something that is not permitted. A template can only be used to compute a value to be assigned to an option. In the first script, the template for the service option is attempting to create additional options and that is not allowed. The most it can do is generate the name of a service call (because it is a template used to compute the value of the service option). Template cannot be used to create options.
@123 thank you for your answer - highly appreciated.
I’m not exactly sure, whether I correctly understood, what you are referring to while speaking about options, but if I’ve gotten you right, the only chance I have would be something like:
flash_generic:
...
- service: script.'serviceNameOfTheCurrentDevice'
...
serviceNameOfDevice1:
...
- do something
serviceNameOfDevice2:
...
- do something
You can do this where the template is used to compute a value that will be assigned to an option (where the options here are service and entity_id
- service: "{{ A template that computes a value to be assigned to the service option }}"
entity_id: "{{ A template that computes a value to be assigned to the entity_id option }}"
You cannot do this:
- service: "{{ A template that computes the name of other options and their values }}"
entity_id: "{{ A template that computes the name of other options and their values }}"
I’m not sure what is the ultimate goal but perhaps this can provide inspiration for alternative ways of determining which lights are currently on.
Again, thank you very much for this snippet and the idea.
Basically I was trying to iterate over all lights of a group, checking whether each one is switched on or not and depending on each result, I wanted to call a service to first save the current state of the device (esp. the color of the light), then letting the light flash in a specific color and then setting it back to the previous state.
If the lights are switched on, using scenes is working fine for me. But if the light is off, a scene created on the fly does either not capture the color the of light or does not restore this value once flashing is done.
So basically I wanted to do those steps manually, but still in a somehow generic way.