Template sensor returns incorrect value


#1

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

{% if is_state('sensor.valiceemo', 'Online') %}
{% if is_state_attr('sensor.gamertag', 'XboxOne Full', is_defined) %}
{{ state_attr('sensor.gamertag', 'XboxOne Full') }}
{% else %}
Home
{% endif %}
{% elif is_state('sensor.valiceemo', 'Offline') %}
Away
{% endif %}

Im certain its my poor template skills, so any help would be appreciated! I cant seem to find much info about nested If statements?!


#2

This is what I use, probably could be optimized. I think I wrote this 3 years ago.

      {% if states.sensor.gamertag %}
        {% set xstate = states('sensor.gamertag') | lower %}
        {% if xstate in [ 'unknown', 'offline' ] %}
          {{ xstate | title }}
        {% else %}
          {% if states.sensor.gamertag.attributes['XboxOne Full'] %}
            {{ states.sensor.gamertag.attributes['XboxOne Full'] | title }}
          {% elif states.sensor.gamertag.attributes['XboxOne Background'] %}
            {{ states.sensor.gamertag.attributes['XboxOne Background'] | title }}
          {% else %}
            {{ xstate | title }}
          {% endif %}
        {% endif %}
      {% else %}
        Api Limit
      {% endif %}

#3

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


#4

live was typo when removing my gamertag.

Just replace your gamer tag with ‘gamertag’. I updated the post and removed the word ‘live’.


#5

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


#6

no difference inside sensors. In template editor it adds whitespace.


#7

Ok, thanks for the help!
I’m not sure I follow the logic of the template, but it works! :man_shrugging:


#8
      #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 %}

#9

You sir, are a diamond.
Really appreciate that, it’s the best means for me to learn! :+1:


#10

Just to add this, I added an extra elif to check if the Xbox app is being used.

{% if states.sensor.valiceemo %}
{% set xstate = states('sensor.valiceemo') | lower %}
{% if xstate in [ 'unknown', 'offline' ] %} {{ xstate | title }}
{% else %}
{% if states.sensor.valiceemo.attributes['XboxOne Full'] %}
{{ states.sensor.valiceemo.attributes['XboxOne Full'] | title }}
{% elif states.sensor.valiceemo.attributes['XboxOne Background'] %}
{{ states.sensor.valiceemo.attributes['XboxOne Background'] | title }}
# check for Xbox mobile app use
{% elif states.sensor.valiceemo.attributes['Android Full'] %}
{{ states.sensor.valiceemo.attributes['Android Full'] | title }}
{% else %}
{{ xstate | title }}
{% endif %}
{% endif %}
{% else %}
Api Limit
{% endif %}

#11

If nothing else it helps immensely with readability.