Problem with template sensor and template dev tab

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?

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?

(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.

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 %}"

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 %}

Thanks for the correction! I made the change.

Last try here…

{% if is_state("sensor.dark_sky_cloud_coverage", > 50) %}on{% else %}off{% endif %}

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.

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,

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 %}'

Thans a lot! Your posting finally solved my problem :slight_smile: Was searching for a solution the whole day…