Compressing yaml in template customize_glob

HI,

Have 20 or so sensors like this:

control_room_motion_sensor_lux:
  friendly_name_template: >
    Control room: 
    {% set lux = states.sensor.control_room_motion_sensor.attributes.lx %}
    {%- if lux <=1 %}
      moonlight
    {%- elif lux <=2 %}
       night light
    {%- elif lux <= 10 %}
       dimmed light
    {%- elif lux <= 50 %}
       ‘Cosy’ living room
    {%- elif lux <= 150 %}
       ‘Normal’ non-task light
    {%- elif lux <= 350 %}
       working / reading
    {%- elif lux <= 700 %}
       inside daylight
    {%- elif lux <= 2000 %}
       maximum to avoid glare
    {%- elif lux <= 10000 %}
       clear daylight
    {%- elif lux <= 120000 %}
       direct sunlight
    {%- else %}
       Too bright
    {%- endif %}
  value_template: >
    {{states.sensor.control_room_motion_sensor.attributes.lx}}

how can i lift this rather large template out of each individual sensor and compress the yaml file considerably?

would be cool if something like:

friendly_name_template:
    {% set lux = states.sensor.control_room_motion_sensor.attributes.lx %}
      {{ luxtemplate(lux) }}

would be possible for each sensor where luxtemplate would be defined globally as

{% set luxtemplate(lux) = 
        {%- if lux <=1 %}
          moonlight
        {%- elif lux <=2 %}
           night light
        {%- elif lux <= 10 %}
           dimmed light
        {%- elif lux <= 50 %}
           ‘Cosy’ living room
        {%- elif lux <= 150 %}
           ‘Normal’ non-task light
        {%- elif lux <= 350 %}
           working / reading
        {%- elif lux <= 700 %}
           inside daylight
        {%- elif lux <= 2000 %}
           maximum to avoid glare
        {%- elif lux <= 10000 %}
           clear daylight
        {%- elif lux <= 120000 %}
           direct sunlight
        {%- else %}
           Too bright
        {%- endif %}  %}

could be set just once…

if at all possible maybe even go one step further, and changing the sensor name through a template also, since im rather meticulous on that subject, might be an option.

in this sensor control_room is used 4 time, 3 times as such once as Control room…

can this be done:

{% set name = Control room %}

and then create the sensor using

sensor. {{name | replace(’ ‘, ‘’_)|lower() }}motion_sensor_lux:
friendly_name_template: >
{{name}}:
{% set lux ={{states.sensor.{{name | replace(’ ', '
’)|lower() }}motion_sensor.attributes.lx}} %}
{{ luxtemplate(lux) }}
value_template: >
{{states.sensor.{{name | replace(’ ', '
’)|lower() }}_motion_sensor.attributes.lx}}

of course there’s issue with templates in templates ?

friendly_name_template: >
  Control room: !include lux_template
  value_template: "{{states.sensor.control_room_motion_sensor.attributes.lx}}"

That’s a nice solution! But how would
I declare the template?

lux_template.yaml

>
    {% set lux = states.sensor.control_room_motion_sensor.attributes.lx %}
    {%- if lux <=1 %}
      moonlight
    {%- elif lux <=2 %}
       night light
    {%- elif lux <= 10 %}
       dimmed light
    {%- elif lux <= 50 %}
       ‘Cosy’ living room
    {%- elif lux <= 150 %}
       ‘Normal’ non-task light
    {%- elif lux <= 350 %}
       working / reading
    {%- elif lux <= 700 %}
       inside daylight
    {%- elif lux <= 2000 %}
       maximum to avoid glare
    {%- elif lux <= 10000 %}
       clear daylight
    {%- elif lux <= 120000 %}
       direct sunlight
    {%- else %}
       Too bright
    {%- endif %}

I must not be having my day… but how can I use this same template for all sensors?
I somehow must be able to pass the name to the template , or the template to the named sensor …?

Sorry, I don’t know what you are asking. I thought from your first post you had a working configuration with lots of repetition and wanted to use a single template instead of repeating it every time. That’s what my solution does, just use the same include for each one.

thats exactly what i want indeed, but note this line:

{% set lux = states.sensor.control_room_motion_sensor.attributes.lx %}

which has the name of the individual sensor (control_room in this case) in the template. I need to pass the name of the others there too somehow, to be able to use the same single template to each of the sensors.

{% set lux = states.sensor.{{name_template}}_motion_sensor.attributes.lx %}

this works:

{% set name = ‘control_room’ %}

states.sensor.{{name}}_motion_sensor.attributes.lx

but using that as a template

{% set name = ‘control_room’ %}

{{states.sensor.{{name}}_motion_sensor.attributes.lx}} doesnt.

Some how we can’t insert a template in the template.

Yeah, you can’t generate the sensor itself by template, only template each sensor.

control_room_motion_sensor_lux:
  friendly_name_template: >
    Control room: !include lux_template
    value_template: "{{states.sensor.control_room_motion_sensor.attributes.lx}}"

next_room_motion_sensor_lux:
  friendly_name_template: >
    Control room: !include lux_template
    value_template: "{{states.sensor.next_room_motion_sensor.attributes.lx}}"

Etc