lpt2007
(lpt2007)
March 18, 2022, 1:45pm
1
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
Hellis81
(Hellis81)
March 18, 2022, 1:58pm
2
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.
lpt2007
(lpt2007)
March 18, 2022, 2:05pm
3
Hellis81:
light.hallway_light
You are right:
Is there a way to configure template to bypass this error? Maybe to check if light is on?
Hellis81
(Hellis81)
March 18, 2022, 2:07pm
4
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.
pedolsky
(Pedolsky)
March 18, 2022, 4:17pm
5
You have to set a default value, see here for more info:
I keep seeing the same question about the new default argument for the templating. This post will cover all possible ways to define default.
All the methods that were changed:
acos
as_timestamp
asin
atan
atan2
cos
float
log
round
sin
sqrt
strptime
tan
timestamp_custom
timestamp_local
timestamp_utc
Each of them have the exact same default functionality but a different number of arguments. There are multiple ways to use these functions, and this is where all the confusion lies.
Understandi…
Thanks for helping
Here’s one:
- sensor:
- name: "boiler_power"
unit_of_measurement: 'W'
unique_id: "sensor.boiler_power"
device_class: power
state: "{{ states('sensor.boiler_l1_power') | float(0.0) * 3 }}"
state_class: measurement
1 Like
lpt2007
(lpt2007)
March 18, 2022, 8:58pm
6
Can you show me where I must put , default = 0
in my template?
pedolsky
(Pedolsky)
March 18, 2022, 9:12pm
7
Without digging into your code, first give all |int
and |float
a “0”:
|float(0)
|int(0)
1 Like
lpt2007
(lpt2007)
March 18, 2022, 9:55pm
8
pedolsky:
|float(0)
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'
pedolsky
(Pedolsky)
March 18, 2022, 10:21pm
9
See the second link I’ve posted and try again after adding an availability template.
lpt2007
(lpt2007)
March 18, 2022, 11:09pm
10
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?
pedolsky
(Pedolsky)
March 18, 2022, 11:13pm
11
availability: "{{ states('sensor.boiler_l1_power') | is_number }}"
lpt2007
(lpt2007)
March 19, 2022, 7:21pm
12
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.
pedolsky
(Pedolsky)
March 19, 2022, 7:45pm
13
Ah, I got it mixed up with the sensors whose state is a number.
lpt2007:
availability_template
Yes, I think you need the ‘_template’ suffix. The example above is from a modern design templates sensor.