Automation Template

Hi All,

I try to create an automation which dynamically adds entities to groups based on their entity_id.
I can achieve this is already using the following code:

- id: set_group_members
  alias: 'set group members'
  description: 'dynamically add entities to groups based on their name'
  initial_state: 'off'
  trigger:
      platform: homeassistant
      event: start
  action:
    - service: group.set
      data_template:
        object_id: 0_bath_cover
        name: 0 bath cover
        add_entities: >
          {%- set room='bath' -%}
          {%- set type='cover' -%}
          {%- for item in states if item.entity_id | regex_search(type + '\..*_' + room + '_.*', ignorecase=True) -%}
          {% if not loop.first %},{% endif %}{{item.entity_id}}
          {%- endfor %}

If I now try to use the template for “object_id” and “name” as follows:

- id: set_group_members
  alias: 'set group members'
  description: 'dynamically add entities to groups based on their name'
  initial_state: 'off'
  trigger:
      platform: homeassistant
      event: start
  action:
    - service: group.set
      data_template: >
        {%- set room='bath' %}
        {%- set type='cover' %}
        object_id: 0_{{room}}_{{room}}
        name: 0 {{room}} {{type}}
        add_entities:
          {%- for item in states if item.entity_id | regex_search(type + '\..*_' + room + '_.*', ignorecase=True) -%}
          {% if loop.first %} {% else %},{% endif %}{{item.entity_id}}
          {%- endfor %}

I receive an error

Invalid config for [automation]: expected a dictionary for dictionary value @ data['action'][0]['data_template']. Got None.

I have tried a lot of space and {%-, {% etc. combination and within “Templates” UI both codes resolve in the same output as far as I can see…

What am I doing wrong here?
(And how can I verify that my spaces etc. are correct within templates?)

Thanks for any support in advance

It won’t like you have it because the template entries are only available within the key section where they are created so each line needs to be templated separately.

so these templates aren’t global to the entire data_template: section

data_template: >
  {%- set room='bath' %}
  {%- set type='cover' %}

So, you have to do something like this:

data_template: 
  object_id: >
    {%- set room='bath' %}
    {%- set type='cover' %}         
    0_{{room}}_{{room}}
  name: >
    {%- set room='bath' %}
    {%- set type='cover' %}
    0 {{room}} {{type}}
  add_entities: >
    {%- for item in states if item.entity_id | regex_search(type + '\..*_' + room + '_.*', ignorecase=True) -%}
    {% if loop.first %} {% else %},{% endif %}{{item.entity_id}}
    {%- endfor %}

But not knowing what the goal is for templating “room” or “type” if you just use it as above it really doesn’t make sense to use a template in those sections.

I hope I made it clear enough.

The biggest thing to remember is that template return values are only valid inside the key section where they are created and aren’t global outside of there.

1 Like

I believe there’s a way you can achieve what you want but it requires using a script. You can pass variables to a script and then the script’s templates can reference the variables.

NOTE:
I have not tested it so it may require modifications.

Automation

The automation’s action is reduced to simply calling the script and passing two variables:

- id: 'set_group_members'
  alias: 'set group members'
  trigger:
    platform: homeassistant
    event: start
  action:
    service: script.create_group
    data:
      room: 'bath'
      type: 'cover'

Script

The script receives the two variables and uses them in its templates. The templates are the same as what you shared in your first post.

  create_group:
    fields:
      room:
        description: 'The room name'
        example: 'bath'
      type:
        description: 'The domain name'
        example: 'cover'
    sequence:
      service: group.set
      data_template:
        object_id: 0_{{room}}_{{room}}
        name: 0 {{room}} {{type}}
        add_entities: >
          {%- for item in states if item.entity_id | regex_search(type + '\..*_' + room + '_.*', ignorecase=True) -%}
            {% if loop.first %} {% else %},{% endif %}{{item.entity_id}}
          {%- endfor %}

Thanks - was not aware of this but good to know for future :+1:

Will give it a try - never worked with script previously but thanks for the hint