Help with spacing in for loop template

Hello all,

I have a template that I’m trying to create which will show all of the lights that are set to manual control with the adaptive lighting custom integration. They are listed in an array as an attribute of the switch entity.

I’m 99% of the way there, but when I view the entity in lovelace, there are 2 spaces on a new line that I can’t seem to figure out how to get rid of. I’m hoping someone better at templating than I can take a look at this and either help me out, or if there’s a better way write this template I’m all ears :smile:

Here’s my current template:

sensor:
  name: Adaptive Lighting Manually Controlled Lights
  unique_id: f7ef4c86-4967-4f85-9df6-7a2f04ca7b62
  state: >-
    {% set adaptive_light_entities = expand('group.adaptive_lights') %}
    {% set manual_lights_looplength = namespace(value=0) %}
    {% for x in adaptive_light_entities if state_attr(x.entity_id, 'manual_control') %}{{"\r\n"}}
    {% set manual_lights_looplength.value = loop.length %}
    {{ state_attr(state_attr(x.entity_id, 'manual_control')[0], 'friendly_name') }}
    {% endfor %}
    {% if manual_lights_looplength.value == 0 %}All lights are being controlled by Adaptive Lighting{% endif %}

And here’s what it looks like in lovelace using a mushroom template card:
image
And here’s the yaml for the card:

type: custom:mushroom-template-card
primary: '{{ state_attr(entity, ''friendly_name'') }}'
secondary: '{{ states(entity) }}'
icon: mdi:home
entity: sensor.adaptive_lighting_manually_controlled_lights
multiline_secondary: true
tap_action:
  action: more-info
fill_container: true

I don’t have a means of testing the following example so you’ll need to confirm if it eliminates the leading spaces (or not).

sensor:
  name: Adaptive Lighting Manually Controlled Lights
  unique_id: f7ef4c86-4967-4f85-9df6-7a2f04ca7b62
  state: >-
    {% set ns = namespace(lights=[]) %}
    {% for x in expand('group.adaptive_lights') if state_attr(x.entity_id, 'manual_control') %}
    {% set ns.lights = ns.lights + [state_attr(state_attr(x.entity_id, 'manual_control')[0], 'friendly_name')] %}
    {% endfor %}
    {{ ns.lights | join('\n') if ns.lights | count > 0 else 'All lights are being controlled by Adaptive Lighting' }}

Thanks, I’ll give that a shot when I get home!

Is it actually a leading space? I’m even surprised the old \r\n works, but maybe you need just one and the other is treated as whitespace?

Thank you! That did the trick. Mines on the top, yours is on the bottom.

Now to go back and make sense of what this is doing haha

1 Like

You’re welcome!

Please consider marking my post above 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 been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.


It’s building a list of friendly_names. If the list is not empty, it displays all items in the list separated by a newline character. If the list is empty, it reports the ‘All lights are … etc’ message.

Ok makes sense, just going about it a slightly different way than I did

The subtle difference is that the method I suggested first builds a list of names then displays the list’s contents delimited by explicit newlines whereas your method displays each name as it’s found and relies on Jinja’s implicit whitespace control.

1 Like