Template YAML problem. Im not smart enough

I have a number, quite a lot, of value_template sensors, showing the average brightness of the light in a lightgroup. I use the template value in lovelace cards indicating the level of light in a room. Give a good overview of all light in all rooms in the house on a very small space

It all works perfectly, however I would like to clean up my logs :slight_smile: In a community of technical people, I think this is a valueable desire:-)

The template working is like this

    kokkenlightgroup_brightness:
      value_template: "{{ states.light.kokkenlightgroup.attributes.brightness | float }}"

It works perfectly, however shows in the logs as “unavailable” everytime a lightgroup is turned off. I would like the template to only “render” the value when there is an actual value in the brightness attribute, otherwise put in 0 as the brightness value in my template

I have tried a number of different templates setups with no luck. The below is still returning the brightness value when lights are on, however is not returning 0 when the lights are off (state is unavailiable). The value_template still ends as unavailable

    sovevaerelselightgroup_brightness:
      value_template: >
        {%- if is_state ('states.light.sovevaerelselightgroup.attributes.brightness', 'unavailable') -%}
          0
        {%- else -%}
          {{(states.light.sovevaerelselightgroup.attributes.brightness | float)}}
        {%- endif -%}

I can not figure out what Im doing wrong. Anyone with good YAML skills. Probably simple.

    kokkenlightgroup_brightness:
      value_template: >
        {% set b = state_attr('light.kokkenlightgroup', 'brightness') %}
        {{ b if b != none else 0 }}

If the brightness attribute doesn’t exist, the state_attr() function returns none (representing null).

There’s no need to use the float filter in this template because the brightness attribute is already numeric (its type is integer).

1 Like

Thanks, you are a champ :slight_smile:

1 Like