Would be really cool if HA would support a ‘global variable’ (entity) for templates in all configuration variables. Right now, if we want to template based on that single entity, we need to declare it in all configuration variables. Why not introduce a global variable per sensor that can be use throughout the other templates in the sensors configuration
now:
driveway_buiten_sensor_light_level_raw:
value_template: >
{{state_attr('sensor.driveway_buiten_sensor_light_level','lightlevel')}}
friendly_name_template: >
Driveway buiten:
{% set light_level = state_attr('sensor.driveway_buiten_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 %}
device_class: illuminance
with global variable:
driveway_buiten_sensor_light_level_raw:
global_variables:
variable1: sensor.driveway_buiten_sensor_light_level
value_template: >
{{state_attr(variable1,'lightlevel')}}
friendly_name_template: >
{{state_attr(variable1,'friendly_name').split('sensor')[0]}}:
{% set light_level = state_attr(variable1,'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 %}
device_class: illuminance
heck, we’d even be able to create a yaml anchor here and use it for 20 other motion sensors light level
driveway_buiten_sensor_light_level_raw:
global_variables:
variable1: sensor.driveway_buiten_sensor_light_level
value_template: >
{{state_attr(variable1,'lightlevel')}}
friendly_name_template: >
<<: &friendly_name_anchor
{{state_attr(variable1,'friendly_name').split('sensor')[0]}}:
{% set light_level = state_attr(variable1,'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 %}
device_class: illuminance
porch_buiten_sensor_light_level_raw:
global_variables:
variable1: sensor.porch_buiten_sensor_light_level
value_template: >
{{state_attr(variable1,'lightlevel')}}
friendly_name_template: >
<<: *friendly_name_anchor
device_class: illuminance
or maybe even:
driveway_buiten_sensor_light_level_raw:
global_variables:
variable1: sensor.driveway_buiten_sensor_light_level
<<: &sensor_config_anchor
value_template: >
{{state_attr(variable1,'lightlevel')}}
friendly_name_template: >
{{state_attr(variable1,'friendly_name').split('sensor')[0]}}:
{% set light_level = state_attr(variable1,'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 %}
device_class: illuminance
porch_buiten_sensor_light_level_raw:
global_variable:
variable1: sensor.porch_buiten_sensor_light_level
<<: sensor_config_anchor
if only…
see: What's the difference between these yaml anchors whence this WTH originated
btw, I know Ive posted this template above before, in an effort to also introduce friendly_name_templates for template binary_sensor, and Balloob shot that because he feels we shouldn’t template the frontend like that. I still dont agree on that, but for the sake of this WTH: imagine the identical template as an attributes template.