Question about a template

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.

Thank you!

{% 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 %}
1 Like

Thank you!! I was just found that when I typed in jinja2 string comparison when one value is null
Found the very thing you are telling me.

Thanks again!!!

you could also shorten it to

{{ state_attr('media_player.living_room_tv', 'source') or 'tv is off' }}

it would be the same thing

1 Like

For future reference, use none to represent null not 'none' (which is a string value).

{% if state_attr('media_player.living_room_tv', 'source') != none %}
1 Like

you don’t want to use != none, always check none with is or is not.

1 Like

Thank you for the help. I picked up java pretty quick but this has been an experience. But I do appreciate the help!!

This →

{{ state_attr(‘media_player.living_room_tv’, ‘source’) or ‘tv is off’ }}

Is awesome! I guess the best thing for me to do is read about jinja2 more!

Again, Thank you!

GOOD read!!! Thank you!!!

is None is a bit (~50%) faster than == None

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.

The fourth post in that thread explains there’s only one none object so is none and == none are equivalent tests.

FWIW, I typically use != none in templates and have never had it produce an incorrect result.

BTW, the most upvoted post in that thread doesn’t make much of a compelling case beyond simply stating “as a general rule”. :thinking:

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.

1 Like

is will return True if two variables point to the same object (in memory), == if the objects referred to by the variables are equal.

So that tells me that ‘is’ should be quicker! AND the better way to go.

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.

1 Like

How much faster did it “seem”? On the order of seconds, milliseconds, microseconds?

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.

is because is checks the actual object

Which that tells me I’m thinking correctly here…

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.

1 Like

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 !=

so for the most complex object we have, qhich is datetimes, it’s 2 if statements vs a memory location check

    def __eq__(self, other):
        if isinstance(other, datetime):
            return self._cmp(other, allow_mixed=True) == 0
        elif not isinstance(other, date):
            return NotImplemented
        else:
            return False
1 Like