Template loop detected while processing event

Right now, its constantly rebuilding hardware for development environment I need to work on my mikrotik integration and my indoor garden project, since I’m testing various grow lights and ventilation systems and their different setup.
I measure power draw for every device separately, specially my garden project to calculate cost and efficiency vs umol.
Its probably an extreme case and overkill, but its something I like to look at in detail.

OK, here’s a simple example of a group created on startup or when groups are reloaded:

# Dynamically create a group
- alias: 'Create My Sensors Group'
  trigger:
  - platform: homeassistant
    event: start
  - platform: event
    event_type: 'call_service'
    event_data:
      domain: 'group'
      service: 'reload'
  action:
  - service: group.set
    data_template:
      name: 'My Sensors'
      entities: >
        {{ states.sensor  
          | selectattr('attributes.unit_of_measurement', '==', 'W')
          | selectattr('attributes.device_class', '==', 'power')
          | selectattr('attributes.power_type', '==', 'whatever')
          | list }}

The template contains ‘everything but the kitchen sink’ and is only meant to demonstrate several ways to select desired entities. It shows how to select them by unit_of_measurement, device_class and a custom attribute called power_type.

In your case, you might use some or none of those techniques. In fact, you might want to use a template very similar to the one you used in your Template Sensor, something like this:

        entities: >
          {% set ns = namespace(entities=[]) %}
          {% for s in states.sensor if s.object_id.endswith('_power') or s.object_id.endswith('_energy') %}
            {% set ns.entities = ns.entities + [ s.entity_id ] %}
          {% endfor %}
          {{ ns.entities }}
4 Likes

Thank you, this is exactly what I need. I have adjusted it as follows:

- alias: 'Update Group - Daily Energy'
  trigger:
  - platform: homeassistant
    event: start
  - platform: event
    event_type: 'call_service'
    event_data:
      domain: 'group'
      service: 'reload'
  action:
  - service: group.set
    data_template:
      name: 'Energy - Daily'
      object_id: energy_daily
      entities: >
          {% set ns = namespace(entities=[]) %}
          {% for s in states.sensor if s.object_id.endswith('_energy') and s.attributes.unit_of_measurement == "kWh" and not s.attributes.meter_period %}
            {% set ns.entities = ns.entities + [ s.entity_id ] %}
          {% endfor %}
          {{ ns.entities }}
          
- alias: 'Update Group - Power'
  trigger:
  - platform: homeassistant
    event: start
  - platform: event
    event_type: 'call_service'
    event_data:
      domain: 'group'
      service: 'reload'
  action:
  - service: group.set
    data_template:
      name: 'Energy - Power'
      object_id: energy_power
      entities: >
          {% set ns = namespace(entities=[]) %}
          {% for s in states.sensor if s.object_id.endswith('_power') and s.attributes.unit_of_measurement == "W" and not s.attributes.meter_period %}
            {% set ns.entities = ns.entities + [ s.entity_id ] %}
          {% endfor %}
          {{ ns.entities }}

Since its automatic, I will use 2 groups.

I have tried using component loaded trigger, but that does not work for me.

  - platform: event
    event_type: 'component_loaded'
    event_data:
      component: esphome
1 Like

Glad to hear it solves the original problem.

To close out this topic, please consider marking my post (either the initial one suggesting to use expand with a group or the one demonstrating how to generate a dynamic group) with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has an accepted solution (only one Solution per topic). This helps other users find answers to similar questions.

Hi,
thanks for this.
I think I understand everything but have a minor issue with some of the logic.
I want to create a group for energy entities for each room (i.e kitchen, bedrooms, living room, etc) to track the used energy and then a total energy for the home (which will add all the rooms together)

I’m currently running 2021.12.7 and I have this automation code.

- id: "1641380832863"
  alias: Update Energy Groups
  description: ""
  trigger:
    - platform: homeassistant
      event: start
    - platform: event
      event_type: call service
      event_data:
        domain: group
        service: reload
  condition: []
  action:
    - service: group.set
      data_template:
        name: Master Bedroom Energy
        entities: >
          {% set ns = namespace(states=[]) %}
          {% for s in states.sensor %}
            {% if s.entity_id.startswith('sensor.leftbedroom') and s.object_id.endswith('_energy') or s.entity_id.startswith('sensor.rightbedroom') and s.object_id.endswith('_energy') or s.entity_id.startswith('sensor.masterbedroom') and s.object_id.endswith('_energy') and s.state not in ['unavailable', 'unknown' ] %} 
            {% set ns.states = ns.states + [s.state | float] %}
            {% endif %}
          {% endfor %} 
          {{ ns.states | sum | round(2) }}
  mode: single

when i run the automation i get the following error

Stopped because an error was encountered at January 5, 2022, 10:35:23 PM (runtime: 0.17 seconds)

’float’ object is not iterable

I know that they recently changed the float so it should read something like float (default=0) but i think its more than that.

Once I understand this part I want to add multiple actions for each room group update and then work on the value template for each room.

Does anyone know what the logic should be for this?
Any help or ideas would be greatly appreciated

Thanks

{{ states.sensor 
   | selectattr('object_id', 'search', '^(benbedroom|susiebedroom|masterbedroom)')
   | selectattr('object_id', 'search', '(_energy)$')
   | rejectattr('state', 'in', ['unavailable', 'unknown'])
   | map(attribute='state') | map('float', none)
   | reject('==', none)
   | sum | round(2) }}

EDIT: I just realized you’re using this on a group set. This has to be an entity_id list, not the actual math you want to employ…

{{ states.sensor 
   | selectattr('object_id', 'search', '^(benbedroom|susiebedroom|masterbedroom)')
   | selectattr('object_id', 'search', '(_energy)$')
   | map(attribute='entity_id') | list }}

Then your template sensor that calculates the group will be

{{ expand('group.master_bedroom_energy')
   | map(attribute='state') 
   | map('float', none)
   | reject('==', none)
   | sum | round(2) }}
1 Like

Thanks again Petro,
this worked great, I was able to create all the rooms and then another group that collected them all.
If I may, could I ask 2 follow up questions?

1.) Would I add this to the automation group or the template sensor
rejectattr(‘state’, ‘in’, [‘unavailable’, ‘unknown’, ‘none’])

2.) range filters as described here
https://www.home-assistant.io/integrations/filter/#range
I’ve seen some wild numbers from time to time so I’m looking to remove them so it doesn’t blow out the graphs.
Do you have any advise on if it can go with the group or template sensor as above or still call it out as described in the link?

thanks again for your help