Error when saving Automation that includes template

I’m getting the following error, when trying to save an automation that includes a template in the Actions block.

Message malformed: template value should be a string for dictionary value @ data[‘action’][0][‘data’]

Testing the template section with developer tools shows no errors and delivers the expected results. Deliver a list of all lights that are part of any light group.

The code for the Action is:

service: group.set
data:
  name: light_in_group
  entities: >
      {{ states.light 
        | selectattr('entity_id', 'in', states.light 
           | selectattr('entity_id', 'in', integration_entities('group')) 
           | map(attribute='entity_id')
           | expand
           | map(attribute="entity_id")
           | list)
      | map(attribute='entity_id')
      | list
      }}

What can I do to avoid this error message?

[Edit: Formatting in code updated to clarify]

I may ve wrong but:

Are you missing a closing bracket after selectattr('entity_id', 'in', states.light? Also, I think you have a surplus bracket after one of your list filters.

1 Like

Thanks, but the closing bracket after list belongs to the first selectattr

Are you sure this is the action preventing you to save the automation? Is this the first action in the automation?

And by the way, what is the difference from that to this?

      {{ states.light 
        | selectattr('name', 'eq', 'Leuchte') 
        | map(attribute='entity_id')
        | list
      }}

Unfortunately yes. I can save the automation without problem when I leave the template empty.

This action is the first action in the automation

The difference is, that I want to extract all entities that are members of groups. Your short version just extracts group entity. I’m able to save your template version with the automation.

In my original post I unfortunately inserted some test code for just one light group. I updated this later.

selectattr('name', 'eq', 'Leuchte') selects one specfic light group
selectattr('entity_id', 'in', integration_entities('group')) selects all light groups

The ‘save error’ remains.

This extracts all light entities from all groups.

{{ expand(states.group) 
  | selectattr('domain', 'eq', 'light')
  | map(attribute='entity_id') | list }}

Much shorter, but doesn’t work as suggested.

Without “selectattr” this delivers for the lights group only entities related : binary_sensor.xyz.update_available
not the light entity itself, with selectattr in the query the result list is empty.

So what is “as suggested” because it’s unclear what you want the template to do. You said this:

I want to extract all entities that are members of groups

Yet your own template doesn’t do that; it attempts to extract only light entities. That’s why I posted a template that extracts all light entities that are members of any group.

So what exactly do you want the template to do?

Sorry, my mistake for not being specific in the follow-up reply. I’m looking for:

Thanks for clarifying the requirements.

A “light group” isn’t a group entity, it’s a special light entity with an entity_id attribute containing a list of other light entities.

This template finds all “light group” entities by extracting all light entities containing an entity_id attribute. Then it expands them and lists their members.

{{ expand(states.light | selectattr('attributes.entity_id', 'defined') | map(attribute='entity_id'))
  | map(attribute='entity_id') | list }}
1 Like

Many thanks for the explanation of how to extract the light entities within a light group. This approach doesn’t generate errors in automations and also works elsewhere, e.g. with a template applied in lovelace cards (e.g. custom:auto-entities).

2 Likes