Help Correcting Template Loop Detected Error

I set up a sensor to sum up the values of other sensors. Once running I get a lot of “template loop detected” messages. I thought I was doing things right by looping all sensors and then having a second if statement to eliminate the current one. But in reading the response by @123 on this thread I can see why I get that message: I’m still selecting ALL sensors in the condition and then filtering below it, the actual select is throwing the message.

So I understand that, and am trying to correct it by performing the filter directly in the selection itself but can’t seem to find the right syntax.

Here is the original syntax that throws the error (simplified by using Dev Tools):

    {% for s in states.sensor | selectattr('attributes.device_class', 'equalto', 'power') | list %}
      {% if int(s.state, 0) > 0 %}
          {% if not 'current in use wattage' in s.name.lower() %}
            {% if not 'current in use kilowatts' in s.name.lower() %}
              ... (code goes here)
            {% endif %}
        {% endif %}
      {% endif %}
    {% endfor %}

And here is my start to trying to filter directly in the initial condition but it still returns the name of the “current in use wattage” sensor even though I think I’m filtering it out with the if statement (but obviously am not):

      {% for s in states.sensor | selectattr('attributes.device_class', 'equalto', 'power') | list if s.name.lower not in ['current in use wattage', 'current in use kilowatts']  %}
        {{ s.name }}        
      {% endfor %}

I know I just have the Jinja2 if statement improperly formatted, can someone give me an example of that which is properly formatted?

Thank you!

if you’re making a sensor that uses device_class: power you’ll run into the warning. Also, it’s a warning, you can just ignore it. Look into filtering out the warning in the logger integration.

What is special about device_class: power that prohibits it from alleviating the warning message by modifying the filter? Is it treated differently than other sensor types?

Copy-paste this into the Template Editor and see if it gets you what you want:

{{ states.sensor 
  | selectattr('attributes.device_class', 'defined')
  | selectattr('attributes.device_class', 'eq', 'power')
  | rejectattr('name', 'search', 'current in use wattage', ignorecase=True)
  | rejectattr('name', 'search', 'current in use kilowatts', ignorecase=True)
  | rejectattr('state', 'in', ['unavailable', 'unknown'])
  | map(attribute='state') | map('int', 0) | sum }}

NOTE

The last rejectattr isn’t really needed because the map('int', 0) will convert anything non-numeric to 0 and that won’t negatively affect the sum operation. However, if you ever intend to replace sum with average then it’s probably best to reject unavailable and unknown values so they don’t skew the calculated average.

1 Like

Thanks @123 I’ll give that a shot and see if the warnings go away.

Because your searching for device class power, so your including itself in its own template. Hence the circular reference.

1 Like