Nesting template issue

HI,

please have a look at this template:

' {%- if states.sensor.gamertag %}
    {% if is_state("sensor.gamertag", "Online") %}
          {%- if is_state("sensor.gamertag.attributes[''XboxOne Full'']" , "Netflix") %} mdi:netflix 
            {% else %}mdi:xbox-controller
          {%- endif %}
      {% else %}mdi:xbox-controller-off
    {%- endif %}
  {%- endif %}'

Somehow it seems to endup with mdi:xbox-controller while Netflix is on… Can’t see whats wrong, eyes wide shut apparently. Im struggling with the bit between [ ] because when i put [“XboxOne Full”] the template complains about : Error rendering template: TemplateSyntaxError: expected token ',', got 'XboxOne'
Wouldn’t know how to do it otherwise and it does work when use like this:

value_template: '{%- if states.sensor.gamertag %}
                            {% if is_state("sensor.gamertag", "Offline") %}Offline
                            {% else %}
                            {{ states.sensor.gamertag.attributes["XboxOne Full"] }}
                            {%- endif %}
                          {% else %}
                          n/a
                         {%- endif %}'

00

What don’t i see?

Thanks,
Marius

The reason the " is failing is because your is_state(" section starts with a " and you are closing out the string. You could excape the " with \ or you could use ' to create different strings. With '' I’m not really sure what you are causing to happen because it looks like 2 empty strings with random chacaters in the middle that the template should be complaining about.

Also, is_state checks the state of an entity, and you are attempting to point it to an attribute. You want to use is_state_attr('sensor.gamertag', 'XboxOne Full', 'Netflix')

(test it out in the developer tools section in the GUI, it’ll save you a lot of time).

1 Like

thanks!!
i was already testing in the dev tools, but didnt know about the is_state_attr, and was testing only between the [" "] because of the space between XboxOne and Full.

' {%- if states.sensor.gamertag %}
    {% if is_state("sensor.gamertag", "Online") %}
          {%- if is_state_attr("sensor.gamertag", "XboxOne Full" , "Netflix") %} mdi:netflix 
            {% else %}mdi:xbox-controller
          {%- endif %}
      {% else %}mdi:xbox-controller-off
    {%- endif %}
  {%- endif %}'

this is perfect now!
cool

20

now final perfection: turn green when online, else default.

doesnt work just yet:

  templates:
#        rgb_color: "{% if is_state('sensor.gamertag', 'Online') %}'green'
#                    {% else %}'default'
#                    {%- endif %}"
         theme: >
               "{% if is_state('sensor.gamertag', 'Online') %}'green'
                 {%- else %}'default'
                 {%- endif %}"

tried with and without quotes around green, no change though. any thoughts here too please? maybe theme_template. Think it tried that already, no such luck.

Cheers,
Marius

FYI, you don’t need quotes around the template if you use the > to indicate a multiline template. You probably don’t need/want the ' around green and default.

Your templates look fine to me. If they work the the developer tool section and return the results you expect, then odds are you have a syntax problem with whatever your component is. I’m not familiar with any template->theme or template->rgb_color fields, what component are you using?

tried whithout the quotes but no difference. Both work in the dev tools. Not in th frontend though.

Is it possible to write these:

 theme: >
              {% if is_state('sensor.gamertag', 'Online') %}'green'
                 {%- else %}'default'
                 {%- endif %}

in the format of:

templates:
        theme: >
          if (state === 'Online') return 'green'; else return 'red';

that does work with the main sensor. Im not sure about the syntax in template form using the state===

another thing:

could i try this:

{% set gamertag = !secret gamertag %}, and going forward use gamertag in the templates? Would make it so much cleaner. And more secretive …:wink:

Thanks,
Marius

So, then it isn’t a templating problem,but a problem of getting the right data to the component. You didn’t say which component you are trying to configure. I don’t recognize it, so its making me think you are using the custom front end, which I’m not familiar with.

You can do
{{'green' if is_state('domain.my_device', 'state') else 'red'}}

I don’t know, I’ve never tried. I’m guessing it’ll work because the YAML parser should interpret the !secret prior to the jinja engine parsing the template.

accidentally i posted an answer in the wrong thread, here’s the link:

not so promising after all. seems to not cause any errors, but always end with the latest ‘else’… because the {{gamertag}} wasn’t read apparently. Back to original for now.

sorry missed that question. Im using the Xboxlive sensor: Removed integration - Home Assistant and indeed use the custom-ui by @andrey set globally

strange thing is many themes come through just fine. This is a case I just cant get it to work, while it does show fine in the template dev tool.

Maybe that’ll work, will try and report back, thanks! Would that be a templates: theme: > ? I ask because you use the {{ }}

somehow this, and only this is working fine at the moment:

theme: >
    if (state === 'Netflix') return 'green';
    else if (state === 'Offline') return 'red'; else return null;

as long as no reference to another sensor has to be made, the themes work.

06

thanks for your time and effort!
Marius

that must be it. if i check in the dev state tool, no template is displayed.