What this error means (light template)?

What this error in light template means?

Here is light template:

  - platform: template
    lights: 
      hallway_light:
        friendly_name: "hallway_light"
        value_template: "{{ (state_attr('light.hallway_light_white_warm', 'brightness')|int + state_attr('light.hallway_light_white_cool', 'brightness')|int) > 0 }}"
        level_template: "{{ ((state_attr('light.hallway_light_white_warm', 'brightness')|float + state_attr('light.hallway_light_white_cool', 'brightness')|float)/2)|int }}"
        temperature_template: "{{ 1000000/(((state_attr('light.hallway_light_white_warm', 'brightness')|float*3000 + state_attr('light.hallway_light_white_cool', 'brightness')|float*6500)/((state_attr('light.hallway_light_white_warm', 'brightness')|int + state_attr('light.hallway_light_white_cool', 'brightness')|int)))) }}"
        set_level:
          - service: light.turn_on
            data:
              entity_id: light.hallway_light_white_warm
              brightness: "{{ ((brightness*2) * (6500-1000000/state_attr('light.hallway_light', 'color_temp')|int) / (6500-3000)|int) }}"
          - service: light.turn_on
            data:
              entity_id: light.hallway_light_white_cool
              brightness: "{{ ((brightness*2) * (1000000/state_attr('light.hallway_light', 'color_temp')|int - 3000) / (6500-3000)|int) }}"
        set_temperature:
          - service: light.turn_on
            data:
              entity_id: light.hallway_light_white_warm
              brightness: "{{ ((state_attr('light.hallway_light', 'brightness')|int*2) * (6500 - 1000000/color_temp) / (6500 - 3000)|int) }}"
          - service: light.turn_on
            data:
              entity_id: light.hallway_light_white_cool
              brightness: "{{ ((state_attr('light.hallway_light', 'brightness')|int*2) * (1000000/color_temp - 3000) / (6500 - 3000)|int) }}"
        turn_on:
          - service: light.turn_on
            data:
              entity_id: light.hallway_light_white_warm
          - service: light.turn_on
            data:
              entity_id: light.hallway_light_white_cool
        turn_off:
          - service: light.turn_off
            data:
              entity_id: light.hallway_light_white_warm
          - service: light.turn_off
            data:
              entity_id: light.hallway_light_white_cool

Here is error:

2022-03-18 14:35:57 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'int' got invalid input 'None' when rendering template '{{ 1000000/(((state_attr('light.hallway_light_white_warm', 'brightness')|float*3000 + state_attr('light.hallway_light_white_cool', 'brightness')|float*6500)/((state_attr('light.hallway_light_white_warm', 'brightness')|int + state_attr('light.hallway_light_white_cool', 'brightness')|int)))) }}' but no default was specified. Currently 'int' will return '0', however this template will fail to render in Home Assistant core 2022.1

The brightness attribute is not there when the light is off therefor state_attr('light.hallway_light', 'brightness') will give you none.
I assume.

I honestly think the attributes should always be there even when a light is off for example since it will generate much less warnings and make templates and automations easier.

You are right:

Is there a way to configure template to bypass this error? Maybe to check if light is on?

I don’t think you can, at least I haven’t been able to.
It seems the template as a whole is evaluated even if something makes it “stop”.
But perhaps I have done something wrong.

You have to set a default value, see here for more info:

1 Like

Can you show me where I must put , default = 0 in my template?

Without digging into your code, first give all |int and |float a “0”:

|float(0)

|int(0)
1 Like

Thanks, error is gone.

Can I somehow get rid of this error?

2022-03-18 22:51:36 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('ZeroDivisionError: division by zero') while processing template 'Template("{{ 1000000/(((state_attr('light.hallway_light_white_warm', 'brightness')|float(0)*3000 + state_attr('light.hallway_light_white_cool', 'brightness')|float(0)*6500)/((state_attr('light.hallway_light_white_warm', 'brightness')|int(0) + state_attr('light.hallway_light_white_cool', 'brightness')|int(0))))) }}")' for attribute '_temperature' in entity 'light.hallway_light'

See the second link I’ve posted and try again after adding an availability template.

Do you mena to check if lights are on or off?

I tried like that but I get unavailable (can turn on the light).

availability_template: "{{ states('light.hallway_light_white_cool') == 'on' or states('light.hallway_light_white_warm') == 'on' }}"

How do I do that? Can you put me to right direction?


availability: "{{ states('sensor.boiler_l1_power') | is_number }}"

I can’t find availability in light template only availability_template.

Do you mean like that:

availability_template: "{{ states('sensor.hallway_light_white_cool_power') or states('sensor.hallway_light_white_warm_power') | is_number }}"

Not working?I get unavailable when cool and warm light is off.

Ah, I got it mixed up with the sensors whose state is a number.

Yes, I think you need the ‘_template’ suffix. The example above is from a modern design templates sensor.