I am trying to turn some automations on or off based on an input_boolean but I’d rather not list them all manually so I want to do this:
action:
- service_template: >
{% if is_state('input_boolean.irrigation_master_control_switch', 'on') %}
automation.turn_on
{% else %}
automation.turn_off
{% endif %}
data_template:
entity_id: >
{% for item in states['automation'] if item.name.startswith('rpi') -%}
- {{ item.entity_id }}
{% endfor %}
But that doesn’t work and I get the following error:
Error while executing automation automation.master_control_switch. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data['entity_id']
I’m guessing the template doesn’t present the result in the correct format somehow but is there a way to do this?
Not for the first time I am tagging @petro, @Mariusthvdb and @pnbruckner. Certainly not because I expect help from you but only because this has been the kind of thing you have helped with in the past. Thanks and apologies if this doesn’t interest you.
Not sure a trailing comma will be accepted. I’d suggest something slightly different:
{% for item in states['automation'] if item.name.startswith('rpi') -%}
{% if not loop.first %},{% endif %}{{ item.entity_id }}
{%- endfor %}
@klogg, the idea here is that a template can only return a single string. It cannot return a YAML list. (What you wrote just generates a single string with entity_id’s separated by dashes, which is not valid syntax for the entity_id parameter.) But the entity_id parameter of a service call can (generally) accept a string of comma separated entity_id’s.
So this is bugging the crap out of me… Why does it start {%- and end with %} ? Recently I have noticed a lot of templates like this… Why is there on - before the last %… like I said I’ve seen a few like this recently. I always used to see and use {%- and end with -%}
The rules for templates just seem so random. Is there anywhere that explains this stuff?
Also sometimes, data_template: >
Other times data_template:
Sometimes the whole template will be in “ other times not or ‘ - I just wish there was some logic and rules that were laid out and understandable…
Ha ha! I know what you mean! But to be fair there is and there are. It’s (the logic) is just not always obvious and they (the rules) are laid out, they are just not easy to follow (IMHO)
But a high level answer is that the dashes control white space. don’t ask me to explain the detail I generally use a lot of trial and error but I am slowly getting the hang of it!
The chevrons (>) are to do with ignoring line breaks i.e. “the next line is part of this line”.
{%- crap %} removes leading whitespace.
{% crap -%} removes trailing whitespace.
>- removes whitespace for the first line of the template.
In general, whitespace is removed in the templating function for automations and templates. This won’t really make a difference for you in your configuration files.
In the template editor it will really help, because the template editor does not remove whitespace. You can really see what the dashes do in there.
After all my testing last night, because I didn’t have a solution, I reverted everything back to how it was before I started, and that included renaming the automations back to how they were.
Which of course means you solution failed (only) because it didn’t find any matches.
Funny you should ask because I recently explained this to someone, after being equally puzzled by what seemed to be random sprinklings of - throughout a template.
It’s described here, albeit tersely, under the heading Whitespace control. Skip ahead to the sentence: “You can also strip whitespace in templates by hand.”
In a nutshell, the dash here {%- means strip leading whitespace and here -%} it means strip trailing whitespace.
This convention >- basically means the template spans multiple lines (as opposed to being all on one line) and will be stripped of leading whitespace.
As far as the quotation marks go you can use either single or double style marks outside or inside of a template. But any time you use quotation marks inside of a template they have to be the opposite style of the ones on the outside of the template.
And the rules are that you have to use quotation marks on a single line template. And you have to use a > on a multi line template.