Just trying to wrap my head around templating, and I’m nearly there, I think.
I am looking at grabbing an attribute value from within a sensor, based on the state of said sensor
However, when I test this with the dev tools it returns Home even when the state of sensor.gamertag is Online
Thanks, when I try that I get many errors
Would you mind explaining a little please?
I’m not sure that the {% live states.sensor.gamertag %} is doing? Specifically the live part
Perfect! Works a charm!
Something I’ve always wondered…does indentation make any difference when using templates?
Having no indentation in the development tools makes no difference, as in it works with it all alligned to the left?.
#check to see if sensor is in HA
{% if states.sensor.gamertag %}
# get the state of the sensor, make it all lower case
{% set xstate = states('sensor.gamertag') | lower %}
# if the state is unknown or offline...
{% if xstate in [ 'unknown', 'offline' ] %}
# output the state as a title i.e. Unknown or Offline
{{ xstate | title }}
{% else %}
# if we have the XboxOne Full attribute
{% if states.sensor.gamertag.attributes['XboxOne Full'] %}
# output XboxOne Full as a title. I.e Netflix
{{ states.sensor.gamertag.attributes['XboxOne Full'] | title }}
# else if we have the XboxOne Background attribute
{% elif states.sensor.gamertag.attributes['XboxOne Background'] %}
# output XboxOne Background as a title. I.e Home
{{ states.sensor.gamertag.attributes['XboxOne Background'] | title }}
{% else %}
# otherwise, just output the state as a title.
{{ xstate | title }}
{% endif %}
{% endif %}
{% else %}
# if nothing is working, we probably hit the Api Limit
Api Limit
{% endif %}