I am puzzled by my latest sensor. It shows unknown in the GUI and the “states” page, but when I put the value_template into the template developer it shows True or False (which is what I want). I have other sensors built using the same syntax and they work. Any help or tips would be appreciated.
The log error is:
Could not render template Good rain today: UndefinedError: ‘sensor’ is undefined
Gives true in the developer, but I still get the Error:
Could not render template Good rain today: UndefinedError: ‘sensor’ is undefined
I am suspicious that it may be a “race” condition where the personal weather sensors (pws_) are not set up and started by the time this sensor tries to access them. I don’t know how to fix it if that is the case.
Still gives error in HA but not in Template developer.
All of my other template sensors that use pws_ entities are working without error. Only the precipitation one is a problem
Thanks much for the syntax correction and tip on multi-line. Unfortunately neither of your proposals worked. Still got the:
018-06-20 22:13:53 ERROR (MainThread) [homeassistant.components.sensor.template] Could not render template Good rain today: UndefinedError: ‘sensor’ is undefined
Jer78,
I thought that too, but it seems that the startup error is killing it permanently. That would explain why thing work in the developer template once it is all started. I cannot understand why it does not do a retry later when polled.
pnbruckner, that gives the same results. I think it is just not starting quickly enough to prevent getting locked out. The sensor gives a known value whenever things are running and settled, but the template never runs again.
That sounds like the same issue I had when doing a time difference sensor. Works in the test template perfectly but say unknown in the real world all day long. Never found a fix.
Only reason to go this route is because the states object will return ‘None’ if it doesn’t exist, then you don’t have to explicitly look for a state, like unknown. Other than that, everything @pnbruckner said should have worked. Can you paste exactly what you are typing in your config, including the sensor section?
That did not work either. Still gave an Error on startup, while the other ones just give a warning until they are populated in the next poll.
This is all in my sensors.yaml file. Here is what I am typing, including the sensor before it in the file (which is working).
# Weather Sensor flags
# sensor as whether temperature and humidity are in range for painting
- platform: template
sensors:
ok_to_paint:
friendly_name: "Ok to Paint"
# Temperature between 60 F and 85 F with Humidity between 40% and 60% [non-inclusive]
value_template: "{{ (states('sensor.pws_temp_f')|float > 60) and (states('sensor.pws_temp_f')|float < 85)
and (states('sensor.pws_relative_humidity')|float > 40) and (states('sensor.pws_relative_humidity')|float < 60) }}"
# senssor to determine if we had a good rain today
- platform: template
sensors:
good_rain:
friendly_name: "Good rain today"
# value_template: "{{ states('sensor.pws_precip_today_in')|float > 0.4 }}"
value_template: >
{% if states.sensor.pws_precip_today_in %}
{{ states('sensor.pws_precip_today_in') | float > 0.4 }}
{% else %}
False
{% endif %}
And here is the output from home-assistant.log
2018-06-21 19:25:52 WARNING (MainThread) [homeassistant.components.sensor.template] Could not render template Painting Brightness, the state is unknown.
2018-06-21 19:25:52 ERROR (MainThread) [homeassistant.components.sensor.template] Could not render template Good rain today: UndefinedError: ‘sensor’ is undefined
Your problem is you have the template sensors split into 2 separate platform:templates. You need to have all your sensors in 1 platform. That’s why the section is called sensors, not sensor.
Here’s what it should look like:
# Weather Sensor flags
# sensor as whether temperature and humidity are in range for painting
- platform: template
sensors:
ok_to_paint:
friendly_name: "Ok to Paint"
# Temperature between 60 F and 85 F with Humidity between 40% and 60% [non-inclusive]
value_template: >
{{ 60 < states('sensor.pws_temp_f') | float < 85 and
40 < states('sensor.pws_relative_humidity') | float < 60 }}
good_rain:
friendly_name: "Good rain today"
# value_template: "{{ states('sensor.pws_precip_today_in')|float > 0.4 }}"
value_template: >
{% if states.sensor.pws_precip_today_in %}
{{ states('sensor.pws_precip_today_in') | float > 0.4 }}
{% else %}
False
{% endif %}
FYI i also made edits to your templates to make them easier to read.