Is_state_attr(...) with none argument to test

Why do the following two comparisons NOT return the same result?

{% if state_attr('climate.home_2', 'preset_mode') == none %}A{% else %}B{% endif %}

{% if is_state_attr('climate.home_2', 'preset_mode', none) %}A{% else %}B{% endif %}

Templating result:

A

B

because the first one returns string (and you compare it to none) when the second one internally compares an attribute (which can be anything, not necessary a string) to none.
Hope that’s enough? :wink:

The first one is the one working correctly in my setup. The attribute is really none. The second one is not working…

As you can see here, the attribute is NOT the ‘none’ string:

{% if state_attr('climate.home_2', 'preset_mode') == 'none' %}A{% else %}B{% endif %}

{% if is_state_attr('climate.home_2', 'preset_mode', 'none') %}A{% else %}B{% endif %}

Results:

B

B

The function is_state_attr does extra checks to make sure the attribute exists. This is essentially what you are seeing. Because the attribute is set to none, the check to verify if the attribute exists get’s hit instead of actually checking to see if the value is equal to none. So, in order to properly check an attribute that gets set to none, you need to use:

{% if state_attr('climate.home_2', 'preset_mode') == none %}A{% else %}B{% endif %}
3 Likes

I just want to remind you your original question

Looks like you wanted to ask something else - maybe you still can do that (as you’re the only one who knows that for sure I suppose)?

By the way, here is what petro just explained to you.

Thank you petro, documentation does not actually mention is_state_attr cannot be used with none to check if the attribute exists, reason why my template was not returning the result I expected.
I’ll use state_attr to do it.

2 Likes

The docs don’t mention a lot of the finer points. I guess you can’t document everything. I’m continually amazed that ppl do know these things though and that they’re here to help.

well, state_attr clearly does mention it. agree, it’s not 100% obvious but now you know :wink:

probably because many of them had the same problems and asked the same questions to themselves/here in the past :wink:

I read the code on that one helping someone else a year or so ago. Was curious as to why it wouldn’t pass.

edit here

2 Likes

I see someone was already suggesting to update the documentation last year :smiley:

That does not count. This does :wink:
Let’s see…

Thank you :wink:

Using the formulaone_api, having two different weekend formats due to sprintraces, I can’t get an if statement to work.

two formats; p1, Q, p2, sprint, race - p1, p2, p3 , Q , race

I’m checking for Sprint attribute to exist

{% set nr = states.sensor.formula_one_sensor.attributes.next_race  %}
{% set ts = state_attr('nr','Sprint') %}
{% if state_attr('nr', 'Sprint') == none %}A{% else %}B{% endif %}
{{ ts }}

This results in A for the 3rd statement and None for the 4th (instead of Null)
The upcoming event, French GP, does not have a sprintrace, so the outcome is correct.

When using this in a markdown card it does not work

{% set ts = state_attr('nr','Sprint') %}
{% if (ts == none) %}
<table shit>
{% else %}
<the other table>
{% endif %}

This is the format we’re talking about;

next_race:
  season: '2022'
  round: '12'
  url: http://en.wikipedia.org/wiki/2022_French_Grand_Prix
  raceName: French Grand Prix
  Circuit:
    circuitId: ricard
    url: http://en.wikipedia.org/wiki/Paul_Ricard_Circuit
    circuitName: Circuit Paul Ricard
    Location:
      lat: '43.2506'
      long: '5.79167'
      locality: Le Castellet
      country: France
  date: '2022-07-24'
  time: 13:00:00Z
  FirstPractice:
    date: '2022-07-22'
    time: 12:00:00Z
  SecondPractice:
    date: '2022-07-22'
    time: 15:00:00Z
  ThirdPractice:
    date: '2022-07-23'
    time: 11:00:00Z
  Qualifying:
    date: '2022-07-23'
    time: 14:00:00Z

season: '2022'
    round: '11'
    url: http://en.wikipedia.org/wiki/2022_Austrian_Grand_Prix
    raceName: Austrian Grand Prix
    Circuit:
      circuitId: red_bull_ring
      url: http://en.wikipedia.org/wiki/Red_Bull_Ring
      circuitName: Red Bull Ring
      Location:
        lat: '47.2197'
        long: '14.7647'
        locality: Spielberg
        country: Austria
    date: '2022-07-10'
    time: 13:00:00Z
    FirstPractice:
      date: '2022-07-08'
      time: 11:30:00Z
    Qualifying:
      date: '2022-07-08'
      time: 15:00:00Z
    SecondPractice:
      date: '2022-07-09'
      time: 10:30:00Z
    Sprint:
      date: '2022-07-09'
      time: 14:30:00Z

Does the routine error out because of the missing attr or is my approach wrong?