Conditional Card Template and Sensor Lacks State

Hi All,

I’m trying to create a conditional card that is present when a weather warning sensor (Environment Canada) is populated. I can’t make the state: or state_not: test to see if the sensor is populated. I recognize that I could make a template binary sensor, but that seems inefficient so I was trying to use a template in the conditional card conditions section.

This led me to another discovery - it seems like the value of the sensor when there is no warning is nothing. If I test for ‘is defined’ it is defined. If I test for ‘null’, it is not null. If I test for ‘none’, it is not none. “” and ‘’ give the same results. If I try ‘x{{ states(“sensor.warnings”) }}x’ in the template editor, I get ‘xx’, so it is not a single space or some other special character. The state is not ‘unknown’ either.

So, question 1 is can I use a template in the conditional card conditions, and question 2 is what state is this sensor in when it is not populated with data - even if I were to make a template binary sensor, I wouldn’t know how to test it.

Thanks in advance for your help!

Test for string length > 0

Yeah, I got string+‘A’ = ‘A’ to work too. Thanks for that suggestion - it’s a bit more graceful.

Any idea if I can do that directly as a template in the conditional card, or does it need to be via a binary sensor?

Thanks again!

The core Lovelace cards don’t support templates. However there are a couple of custom cards that enable it:

  1. JINJA templates (you want this one):
  1. JavaScript (included for completeness):

Ok, great! Thanks for the insights. I hadn’t run across those cards before.

Be aware that using jinja2 templates causes a we socket connection to be opened for the backend to parse and evaluate it. If you use a lot of them, it can become a burden on your server and your frontend

1 Like

Thanks Ian, that’s good to know. I was thinking of changing my wind compass to jinja templates but now I’ll leave it as JavaScript.

Thanks Guys, binary sensors it is!