ih8gates
(Scott Reston)
October 24, 2016, 1:40pm
1
I’m trying to create a template sensor called “gloomy” that test various weather details and summarizes them into a simply on/off sensor that I can use with other automations. I’m running into an issue where I’m getting nothing back from the template dev tool while trying to test my logic/syntax.
I’m trying something like:
{% if states.sensor.dark_sky_cloud_coverage.state > 50 %}on{% else %}off{% endif %}
And I get nothing. Blank. No error or anything. Do I need to use another sensor - is the cloud coverage an attribute of something else?
PtP
(Mike)
October 24, 2016, 1:47pm
2
Hey @ih8gates
Give this a go.
{% if states.sensor.dark_sky_cloud_coverage %}{{ states('sensor.dark_sky_cloud_coverage.state') > 50 }}on{% else %}off{% endif %}
In the entity dev-tool does it actually show the “%” symbol?
ih8gates
(Scott Reston)
October 24, 2016, 1:50pm
3
(In my code, I’m surrounding it with an “if” to make sure the sensor is initialized to make sure I don’t get errors. Simplified for testing in the dev tool)
This doesn’t do anything either. If I put the word “test” before it in the template dev tool, I don’t even see “test” when I have the other template code with it. It’s like it short-circuits the dev tool.
PtP
(Mike)
October 24, 2016, 2:02pm
4
First try adding the % back behind the 50 like this. I overlooked it being part of the syntax.
"{% if states.sensor.dark_sky_cloud_coverage %}{{ states('sensor.dark_sky_cloud_coverage.state') > 50 %}}on{% else %}off{% endif %}"
If that doesn’t do it maybe one of these will…
"{%if states('sensor.dark_sky_cloud_coverage') > 50 %}on{%elif states('sensor.dark_sky_cloud_coverage') < 50 %}off{% endif %}"
"{%if states('sensor.dark_sky_cloud_coverage') > 50 %}on{% else %}off{% endif %}"
ih8gates
(Scott Reston)
October 24, 2016, 2:47pm
5
No dice.
BTW, the syntax is wrong on that second one. It was missing the correct “elif”
{% if states('sensor.dark_sky_cloud_coverage') > 50 %}on{% elif states('sensor.dark_sky_cloud_coverage') < 50 %}off{% endif %
}
PtP
(Mike)
October 24, 2016, 3:04pm
6
Thanks for the correction! I made the change.
Last try here…
{% if is_state("sensor.dark_sky_cloud_coverage", > 50) %}on{% else %}off{% endif %}
ih8gates
(Scott Reston)
October 24, 2016, 3:07pm
7
You can’t do comparisons like that with is_state. That just gives a template error.
If my code gave me an error, I’d know where to start. It’s just coming back completely empty with no errors.
PtP
(Mike)
October 24, 2016, 3:23pm
8
Sorry I couldn’t help man!
I have a sensor with the same desired outcome and this is working for me.
{%if states('sensor.energy_meter') | float < 0.55 %}Low{%elif states('sensor.energy_meter') | float < 1.2 %}Normal{%elif states('sensor.energy_meter') | float > 1.2 %}Hi{% endif %}
Highest Regards,
ih8gates
(Scott Reston)
October 24, 2016, 4:12pm
9
You inadvertently gave me the answer. If you compare the state without parsing it as a float, the template tool tanks. This worked:
'{% if states.sensor.dark_sky_cloud_coverage -%}
{% if (states('sensor.dark_sky_cloud_coverage') | float) > 60 %}on{% else %}off{% endif %}
{%- endif %}'
dnuc
(Stefan)
December 28, 2016, 7:48pm
10
Thans a lot! Your posting finally solved my problem Was searching for a solution the whole day…