This is what I have but no matter what I do it returns as null, which is okay but it isn’t following the rest of the template it never makes it past the 2nd line. It returns the state_attr if the tv is on but if it’s off it returns null.
{% if state_attr(‘media_player.living_room_tv’, ‘source’) != ‘null’ %}
{{ state_attr(‘media_player.living_room_tv’, ‘source’) }}
{% else %}
tv is off
{% endif %}
I’m thinking I have it wrong I’ve tried the template editor and many varities but cannot seem to get it to behave itself and do what I want it to do. I’m sure it’s a simple thing I’m making much harder then it has to be.
{% if state_attr('media_player.living_room_tv', 'source') is not none %}
{{ state_attr('media_player.living_room_tv', 'source') }}
{% else %}
tv is off
{% endif %}
well, that’s more python than jinja. I don’t think jinja will cover that. We are getting into do’s and dont’s of python, which you can always ignore if you want to.
Yes just saw that too! LOL So I guess I should probably just go read period…it’s how I learned java and php so I guess that’s how I’ll have to learn this.
A bit tougher because I’ve never taken any classes in school on any of this… just self taught with help from others.
Yes I did try != none and it did work!! I mean is none and != none are comparisons that both work.
But I did find that is None seems to be a bit quicker. I mean I read that don’t really know if it’s true but I think either would work just as well!!!
It won’t produce an incorrect result. It’s a “good coding practice” to use is none and is not none over == none because the == operations on other objects can be lengthy when they are not none. is checks if it’s the same object or not. It’s faster in all comparison cases regardless of the object types.
Just so you understand, all objects that can be checked using == implements a function __equals__() That entire function is skipped when using is because is checks the actual object. That’s why you want to use is when checking none because there is only a single none object.
i.e. a datetime objects == function would look something like this sudo code:
def __equals__(self, other: Any):
if isinstance(other, Datetime):
return self.hours == other.hours and self.minutes == other.minutes ... etc ...
So, you’d execute that code with other being none and start hitting the __enter__'s potentially length if statement.
where is just checks the object’s memory signature which is instant.
I’m not sure about that but I can say that I watched the state change in the template editor and it was much quicker then when I had !=… so is None is faster.
It depends on the other object. It could be seconds or milliseconds. Typically the objects we deal with in jinja are optimized so it would be on the order of milliseconds.
So just playing in the template editor I did this…
{{ state_attr(‘media_player.living_room_tv’, ‘source’) != none }}
{{ state_attr(‘media_player.living_room_tv’, ‘source’) is none }}
{{ state_attr(‘media_player.living_room_tv’, ‘source’) or ‘tv is off’ }}
They reponded at the exact same time… so speed really isn’t noticeable but for the sake of getting something correct is none is the best way to go instead of using !=