Since 0.115 I’m getting some template sensors as “unknown” but when I run the template in dev tools it works perfectly every single time. What has change and what’s wrong with my previously working code?
average_house_luminance:
unit_of_measurement: 'lux'
value_template: >-
{% if states.sensor.bathroom_lightlevel and states.sensor.entrance_luminance and states.sensor.kitchen_lightlevel and states.sensor.living_room_lightlevel and states.sensor.shower_room_lightlevel %}
{{ (
(float(states.sensor.bathroom_lightlevel.state)
+ float(states.sensor.entrance_luminance.state)
+ float(states.sensor.kitchen_lightlevel.state)
+ float(states.sensor.living_room_lightlevel.state)
+ float(states.sensor.shower_room_lightlevel.state)
) / 5) | round(0)
}}
{% else %}
0
{% endif %}
Maybe I’m wrong but the way you do your if statement looks odd.
You got states.sensor.entity that should return a complete entity which I assume is an array.
I don’t code in python, but I would hesitate on doing that in the languages I use since you are relying on type conversion to return a boolean response which the if understand.
Maybe that is not the issue, maybe it’s one of the entities that returns unavailable or N/A or something that makes the math impossible.
What happens if you template them each on a separate line in template tool? Does all get a numeric value?
Assuming states.sensor.bathroom_lightlevel.state has any value, I believe the syntax should be something like this:
states.sensor.bathroom_lightlevel.state | float
not this
float(states.sensor.bathroom_lightlevel.state)
but I maybe I am jumping to (wrong) concussions, but I guess that states.sensor.bathroom_lightlevel.state does not give you any state at all. If I am right, try:
{{ states("sensor.bathroom_lightlevel") }}
If that gives you a value, add | float
The round(0) can be simplified with just round by the way.
I’ve changed my template to use just float and round but I’m still having issues.
I noticed that when I reload templates from the front-end, their values are updated. So for some reason it seams like they aren’t automatically updated?
Nice, and once one entity updates, the outcome still isn’t updated?
I did something similar with two sensors and once one updates, the sum gets recalculated.
It can take a while before a sensor (especially a battery powered sensor) updates, but you could leave both the “code sets” in your template editor and then put a towel (or something) over sensor.living_room_lightlevel to see if the individual status updates and if the sum changes.
I think the code you are using now should work. The only other thing I can think of is this:
Now it’s been a day more and I’m now running the latest update that came yesterday, but I’m still facing issues
BUT as you can see one of the template sensor has actually been updating.
Here’s the Period of the Day sensor (not updating)
day:
friendly_name: Period of the Day
value_template: >-
{% if now().strftime('%H:%M') > '22:30' or now().strftime('%H:%M') < '05:00' %}
night
{% else %}
{% if now().strftime('%H:%M') >= '05:00' and now().strftime('%H:%M') < '08:30' %}
morning
{% elif now().strftime('%H:%M') > '12:00'
and (
(states.sun.sun.attributes.elevation | float < 11 and states.sensor.average_house_luminance.state | int < 50)
or
(states.sun.sun.attributes.elevation | float < 8 and states.sensor.dark_sky_cloud_coverage.state | int > 50)
or
states.sun.sun.attributes.elevation | float < 4
) %}
evening
{% else %}
day
{% endif %}
{% endif %}
You are comparing strings with times as if it’s numbers.
That is usually a breaking point in any coding language.
A string is anything basically so ‘hello’ is a string, and so is ‘05:00’.
But which is larger than the other?
To us as humans it’s easy to see that the example above makes no sense.
For us ‘04:00’ is less than ‘05:00’ but it’s still strings.
String matching is usually done == or !=. < Or > makes no sense with strings.
I believe you need to find a way where you use integers to compare the times instead.
A easy fix would be to use hmm as 530 for ‘05:30’ but keep in mind this is just a “simple hack” and will (or might) fail when its midnight unless you make sure the code understands it.
I have the exact same problem since 0.115 for template sensors. I have many template sensors and some of them won’t be updated anymore and are on unknown.
If I reload the templates from the front-end they all get the right actual values instead of unknown. Also when I place the template in the template designer they get the expected values and no errors.
So the templates are ok but they show up with unknown but when I reload the templates they appear.
Example of a template sensor which won’t work anymore, but there are more and they have always worked and work again after I reload the templates from the front-end(after a restart of HA) and after that they work as normal:
Has anyone found a solution on this? I am having the same issue with template sensors. After reboot they work for a while but then change to “unknown”. Heres one of my sensors: