WTH can't I pull the zone's Entity ID via template?

I want to be able to use a template to change an icon based off the zone a person is in. All I am able to pull is the zone name, but I can’t always just append zone. to get the entity id for a zone.

Trying to set an icon for a tepmlate card, this is what I have:

{% set in_zone = states(entity)%}
{% if is_state(entity, 'not_home')%}
  mdi:car
{% else %}
  {{ state_attr('zone.' + in_zone, 'icon') }}
{% endif %}

This will only show an icon if the entity is either not_home or in and zone with one word/no spaces as the name like “Home”. I want to be able to set the icon even if the zone name is “Grandpa’s House” or something complex like that.

{% set in_zone = states(entity)%}
{% if in_zone == 'not_home' %}
  mdi:car
{% elif in_zone == 'home' %}
  {{ state_attr('zone.home', 'icon') }}
{% else %}
  {{ states.zone | selectattr('name', 'eq', in_zone)
  | map(attribute='attributes.icon') | join }}
{% endif %}
4 Likes

That does it! I do have questions on you came upon this knowledge? I’ve read all through the templating docs and feel like i haven’t seen anything like this. Any tutorials you can point me to?

There a few YouTube channels that have decent templating tutorials:
Smart Home Makers, Smart Home Junkie, Slacker Labs (Jeff uses template throughout his videos, but I couldn’t find one that is specifically about templating).

As for written sources beyond the two links you can find in the Dev Tools > Templates page, there’s Jinja for Ninjas, Public configurations from users like @petro and those listed on Awesome Home Assistant.

But mostly I learned it from reading posts on this forum and trying stuff out.

2 Likes

So cool Drew,

EXACTLY what I was looking for. Don’t understand it, but it works just as I want it to!! :crazy_face:
Thank you.