yes, this can not be done right now, and the wait really is for template variables, which have been asked for quite a few times…
Ive had the exact same issue with these:
frontdoor_sensor_light_level_raw:
value_template: >
{{state_attr('sensor.frontdoor_sensor_light_level','lightlevel')}}
# friendly_name_template: >
# Frontdoor:
# {% set light_level = state_attr('sensor.frontdoor_sensor_light_level','lightlevel')|int %}
# {% if light_level < 1 %} dark
# {% elif light_level < 3000 %} bright moonlight
# {% elif light_level < 10000 %} night light
# {% elif light_level < 17000 %} dimmed light
# {% elif light_level < 22000 %} 'cosy' living room
# {% elif light_level < 25500 %} 'normal' non-task light
# {% elif light_level < 28500 %} working / reading
# {% elif light_level < 33000 %} inside daylight
# {% elif light_level < 40000 %} maximum to avoid glare
# {% elif light_level < 51000 %} clear daylight
# {% else %} direct sunlight
# {% endif %}
attic_sensor_light_level_raw:
value_template: >
{{state_attr('sensor.attic_sensor_light_level','lightlevel')}}
# friendly_name_template: >
# Attic:
# {% set light_level = state_attr('sensor.attic_sensor_light_level','lightlevel')|int %}
# {% if light_level < 1 %} dark
# {% elif light_level < 3000 %} bright moonlight
# {% elif light_level < 10000 %} night light
# {% elif light_level < 17000 %} dimmed light
# {% elif light_level < 22000 %} 'cosy' living room
# {% elif light_level < 25500 %} 'normal' non-task light
# {% elif light_level < 28500 %} working / reading
# {% elif light_level < 33000 %} inside daylight
# {% elif light_level < 40000 %} maximum to avoid glare
# {% elif light_level < 51000 %} clear daylight
# {% else %} direct sunlight
# {% endif %}
that have the huge section copied on all 24 sensors… and only the naming specific for each sensor.
custom-ui to the rescue though
sensor.*_sensor_light_level_raw:
templates:
icon_color: >
if (state < 1) return 'black';
if (state < 3000) return 'maroon';
if (state < 10000) return 'firebrick';
if (state < 17000) return 'orange';
if (state < 22000) return 'green';
if (state < 25500) return 'gold';
if (state < 28500) return 'teal';
if (state < 33000) return 'dodgerblue';
if (state < 40000) return 'lightskyblue';
if (state < 40000) return 'lightblue';
return 'lightcyan';
friendly_name: >
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
var id = entity.entity_id.split('.')[1].split('_sensor_light_level_raw')[0].replace(/_/g,' ');
var id = capitalizeFirstLetter(id);
if (state < 1) return id + ': dark';
if (state < 3000 ) return id + ': bright moonlight';
if (state < 10000) return id + ': night light';
if (state < 17000) return id + ': dimmed light';
if (state < 22000) return id + ': \'cosy\' living room';
if (state < 25500 ) return id + ': \'normal\' non-task light';
if (state < 28500) return id + ': working / reading';
if (state < 33000) return id + ': inside daylight';
if (state < 40000) return id + ': maximum to avoid glare';
if (state < 51000) return id + ': clear daylight';
return id + ': direct sunlight';
unit_of_measurement: lm
device_class: illuminance
I know, custom-ui is not official, and designed for HA States, so ancient. But it still operates beautifully. And if to were to stop one day, I have all my core templates ready to un-comment
Sometimes it can help to rethink things, and use different tools to get almost the same outcome:
auto-entities and template-entity-row (which can use a template variable…) to the rescue:
- type: custom:auto-entities
card:
type: entities
title: Philips light level raw sensors
show_header_toggle: false
filter:
include:
- entity_id: '*_sensor_light_level'
options:
type: custom:template-entity-row
name: >
{% if state_attr(config.entity,'friendly_name') is not none %}
{{state_attr(config.entity,'friendly_name').split(' sensor light level')[0]}}
{% else %} Tbd
{% endif %}
state: >
{{state_attr(config.entity,'lightlevel')}} lm
secondary: >
{% set light_level = state_attr(config.entity,'lightlevel')|int %}
{% if light_level < 1 %} dark
{% elif light_level < 3000 %} bright moonlight
{% elif light_level < 10000 %} night light
{% elif light_level < 17000 %} dimmed light
{% elif light_level < 22000 %} 'cosy' living room
{% elif light_level < 25500 %} 'normal' non-task light
{% elif light_level < 28500 %} working / reading
{% elif light_level < 33000 %} inside daylight
{% elif light_level < 40000 %} maximum to avoid glare
{% elif light_level < 51000 %} clear daylight
{% else %} direct sunlight
{% endif %}
sort:
method: state
numeric: true