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 %}
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.
Unless you use packages
but he’s not using packages
Thanks folks, it is now working. I was pulling my hair out over this one. It is really odd that some sensors worked with the syntax and this one did not. However, it is now SOLVED thanks to all the help.
I am not using packages and am a newbie to HomeAssistant. But I am not new to automation and controls so I often try to do things that are more complicated than the examples.
I appreciate all the help and support.
txNgineer,
do you mind sharing how you fixed the issue?
Sure, I followed the advice that petro gave in the message and made sure that I used the “sensors” platform line only once in the yaml.
Hi everyone, this is my first post herre!
I just hit the same problem, seemingly out of nowhere, as some template sensors stopped working while others continued to do so. This kind of behavior is very hard to debug!
IMHO this is a bug: such a YAML configuration should not validate, but it does.
I agree with you that this is a bug. It’s extremely frustrating to have a config validate when it is in fact broken.
I’ve got this issue now.
template:
- sensor:
- name: alarm_sensor
device_class: timestamp
state: "{{ (states('input_datetime.alarm_time')) }}"
Works perfectly in Dev Tools and returns the correct state.
However, within the rest of HA it returns “unknown”.
device_class: timestamp requires a date. Your logs will tell you why it’s not working.
I.e. Your template is fine, where you’re using it is not.
Thanks for your assistance. Much appreciated!
Can you help me with a fix? I need to create a time sensor from an input_datetime
I’ve tried:
template:
- sensor:
name: test
device_class: timestamp
state: {{ state_attr("input_datetime.alarm_time", "timestamp") | timestamp_custom("%H.%M", False) }}
Again - works in template but configuration.yaml doesn’t like it due to “missed comma between flow collection entries”
you’re missing outside quotes for your template. Use the multiline yaml notation or ensure your outside quotes are wrapped around your template. Also make sure outside quotes and inside quotes are different, i.e. "
outside and '
inside.
Template show correct in developer tool template editor, but error in configuration ?
TemplateError('ValueError: Template error: float got invalid input 'unavailable' when rendering template '{{ ((states.sensor.varmepumpe_total_daily_energy.state | float) * (states.sensor.strompris_kwh.state | float)) | round(2) }}' but no default was specified') while processing template 'Template("{{ ((states.sensor.varmepumpe_total_daily_energy.state | float) * (states.sensor.strompris_kwh.state | float)) | round(2) }}")' for attribute '_attr_native_value' in entity 'sensor.varmepumpe_kost_daglig'
TemplateError('ValueError: Template error: float got invalid input 'unavailable' when rendering template '{{ ((states.sensor.varmepumpe_forbruk_mnd.state | float) * (states.sensor.strompris_kwh.state | float)) | round(2) }}' but no default was specified') while processing template 'Template("{{ ((states.sensor.varmepumpe_forbruk_mnd.state | float) * (states.sensor.strompris_kwh.state | float)) | round(2) }}")' for attribute '_attr_native_value' in entity 'sensor.varmepumpe_kost_mnd'
TemplateError('ValueError: Template error: float got invalid input 'unavailable' when rendering template '{{ ((states.sensor.easse_forbruk_kwh_dag.state | float) * (states.sensor.strompris_kwh.state | float)) | round(2) }}' but no default was specified') while processing template 'Template("{{ ((states.sensor.easse_forbruk_kwh_dag.state | float) * (states.sensor.strompris_kwh.state | float)) | round(2) }}")' for attribute '_attr_native_value' in entity 'sensor.ladebil_kost_daily'
TemplateError('ValueError: Template error: float got invalid input 'unavailable' when rendering template '{{ ((states.sensor.easse_forbruk_kwh_mnd.state | float) * (states.sensor.strompris_kwh.state | float)) | round(2) }}' but no default was specified') while processing template 'Template("{{ ((states.sensor.easse_forbruk_kwh_mnd.state | float) * (states.sensor.strompris_kwh.state | float)) | round(2) }}")' for attribute '_attr_native_value' in entity 'sensor.ladebil_kost_mnd'
TemplateError('TypeError: is_state() takes 3 positional arguments but 4 were given') while processing template 'Template("{% if is_state('sensor.washing_machine_status','Running','Finishing') %} Running {% else %} Finishing {% endif %}")' for attribute '_attr_native_value' in entity 'sensor.vaskemaskin_status'
Many filters (including float) require assigned defaults as of 2012.10
From Templating Docs:
WARNING
Avoid using
states.sensor.temperature.state
, instead usestates('sensor.temperature')
. It is strongly advised to use thestates()
,is_state()
,state_attr()
andis_state_attr()
as much as possible, to avoid errors and error message when the entity isn’t ready yet (e.g., during Home Assistant startup).