kiwinol
(Adam)
January 25, 2017, 1:53am
1
I am trying to add the following “sensor” to my sensors.yaml file. What am I doing wrong?
- platform: template
sensors:
BedRoomLightWorkColor:
friendly_name: "BedRoom light color based on weather"
value_template: >-
{%- if sensor.dark_sky_apparent_temperature <= '14' -%}
Blue
{%- elif sensor.dark_sky_apparent_temperature > '14' and sensor.dark_sky_apparent_temperature <= '18' -%}
Green
{%- elif sensor.dark_sky_apparent_temperature > '18' and sensor.dark_sky_apparent_temperature <= '20' -%}
Yellow
{%- elif sensor.dark_sky_apparent_temperature > '20' -%}
Red
{%- endif -%}
aneisch
(Andrew)
January 25, 2017, 3:49am
2
I have some comparisons like this:
{% if states.sensor.dark_sky_temperature.state | float > 50.5 %}
HOT
{% endif %}
1 Like
kiwinol
(Adam)
January 25, 2017, 4:07am
3
Thanks a lot!
I wasnt putting “states.” at the beginning of the sensor name or converting the value to a float " | float"
could you share a bit more of your code so I can see you template formatting?
aneisch
(Andrew)
January 25, 2017, 6:14am
4
You will find comparisons in Alexa.yaml
1 Like
kiwinol
(Adam)
January 25, 2017, 10:49am
5
Thanks speekquietly very useful examples and interesting you monitor your chickens with it
I nearly have it working like this:
- platform: template
sensors:
worklight:
friendly_name: 'Work Light'
value_template: >-
{%- if sensor.dark_sky_apparent_temperature <= '14' -%}
Blue
{%- elif sensor.dark_sky_apparent_temperature > '14' and sensor.dark_sky_apparent_temperature <= '18' -%}
Green
{%- elif sensor.dark_sky_apparent_temperature > '18' and sensor.dark_sky_apparent_temperature <= '20' -%}
Yellow
{%- elif sensor.dark_sky_apparent_temperature > '20' -%}
Red
{%- endif -%}
but it gets a value of “unknown” and I see lots of errors in my error log like:
17-01-25 22:32:16 homeassistant.components.sensor.template: UndefinedError: 'sensor' is undefined
ih8gates
(Scott Reston)
January 25, 2017, 2:08pm
6
Are the errors at startup? To avoid the “undefined sensor” errors at startup, wrap the template in “if sensor.dark_sky_apparent_temperature” to check that the sensor exists
On HA boot, i get a few template error:
16-12-24 02:36:54 homeassistant.components.sensor.template: UndefinedError: 'None' has no attribute 'state'
16-12-24 02:36:54 homeassistant.components.sensor.template: UndefinedError: 'None' has no attribute 'attributes'
16-12-24 02:36:54 homeassistant.components.sensor.template: UndefinedError: 'None' has no attribute 'attributes'
16-12-24 02:36:54 homeassistant.components.sensor.template: UndefinedError: 'None' has no attribute 'state'
Any way to prope…
Go to YOUR_IP:8123/dev-template to validate the template.
Try:
states.sensor.dark_sky_apparent_temperature.state | float <= 14
rpitera
(Robert Pitera)
January 25, 2017, 2:43pm
8
This should test correctly
{{ states.sensor.dark_sky_apparent_temperature.state | float <= 14 }}
kiwinol
(Adam)
January 25, 2017, 8:47pm
9
Thanks Daniel, I was using the dev-template tester, then I somehow went backwards and broke my template and assumed it was ok as I had tested it earlier! I can see I am going to use the dev-template page a lot.
kiwinol
(Adam)
January 25, 2017, 8:48pm
10
Thanks you are correct.
Just in case anyone else finds this thread I will post my working outcome:
- platform: template
sensors:
worklight:
friendly_name: 'Work Light'
value_template: >-
{%- if states.sensor.dark_sky_temperature.state <= '14' -%}
Blue
{%- elif states.sensor.dark_sky_temperature.state > '14' and states.sensor.dark_sky_temperature.state <= '18' -%}
Green
{%- elif states.sensor.dark_sky_temperature.state > '18' and states.sensor.dark_sky_temperature.state <= '20' -%}
Yellow
{%- elif states.sensor.dark_sky_temperature.state > '20' -%}
Red
{%- endif -%}
1 Like