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?
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:
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.
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.
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?