[SOLVED] Template sensor giving unknown but developer template shows true

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

And the template sensor code is:

  - platform: template
    sensors:
      good_rain:
        friendly_name: "Good rain today"   
        value_template: "{{ states('sensor.pws_precip_today_in')|float > 0.4 }}"

my environment…

Item Current Value
user homeassistant
Home Assistant 0.70.1
Python 2.7.13
OS hassbian 0.9.0
Kernel 4.14.34-v7+
Machine Raspberry Pi 3

Try it like this…

{{ (states.sensor.pws_precip_today_in| float) > 0.4 }}

That gives an error in the template developer unless I specify the attribute. So

{{ (states.sensor.pws_precip_today_in.state| float) > 0.4 }}

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.

Just tried:

   value_template: "{% if is_state('sensor.pws_precip_today_in.state', 'Unknown') -%}
     {{ False }}
   {%- else -%}
     {{ states('sensor.pws_precip_today_in')|float > 0.4 }}
   {%- endif %}"

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

At least you should use all lower case 'unknown' in your is_state function call.

And you should use the multi-line character instead of double quotes.

value_template: >
  {% if is_state('sensor.pws_precip_today_in', 'unknown') %}
    {{ False }}
  {% else %}
    {{ states('sensor.pws_precip_today_in')|float > 0.4 }}
  {% endif %}

Or you could do:

value_template: >
  {{ not is_state('sensor.pws_precip_today_in', 'unknown') and
     states('sensor.pws_precip_today_in')|float > 0.4 }}

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

If you are seeing this error on start I think it’s because it hasn’t loaded the pws_precip_today_in value yet. It should work after everything loads.

Ok, try this:

value_template: >
  {{ states('sensor.pws_precip_today_in') != 'unknown' and
     states('sensor.pws_precip_today_in')|float > 0.4 }}

Are you sure? HA requires at least 3.5.3.
Perhaps this is related?

Oops, sorry. That is the version of python2. The version that HA is running under is python3 3.6.0.

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.

@txNgineer

Try adding this line in your sensor:

entity_id: sensor.pws_precip_today_in

One last try:

value_template: >
  {% if states('sensor.pws_precip_today_in') != 'unknown' %}
    {{ states('sensor.pws_precip_today_in')|float > 0.4 }}
  {% else %}
    False
  {% endif %}

@txNgineer

Best bet to see if a sensor is ‘there’ is:

{% if states.sensor.pws_precip_today_in %}
  {{ states('sesnor.pws_precip_today_in') | float > 0.4 }}
{% else %}
  False
{% endif %}

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

Here is what is showing in the States table of the GUI (after running a while)

Entity Current Value Attributes
sensor.good_rain unknown friendly_name: Good rain today
sensor.pws_precip_today_in 0.02 attribution: Data provided by the WUnderground weather servic e
date: Last Updated on June 21, 7:35 PM CDT
unit_of_measurement: in
friendly_name: Precipitation Today
icon: mdi:umbrella

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.

3 Likes