Person entity_picture based on state

Ok, I’m quite lost after reading like 4355 topics, browsing through loads of yaml’s on Github and still cannot get it to work…

Issue: I want to display a different image based on home or not_home state.

The view I want it in (names obviously removed):
defaut.yaml

cards:
  - type:                           glance
    show_header_toggle:             false
    show_name:                      false
    show_state:                     true
    entities:
      - entity:                     person.name
      - entity:                     person.name
      - entity:                     person.name
      - entity:                     person.name

Config:
configuration.yaml:

homeassistant:
  customize: !include family.yaml

In family.yaml i tried many options:
1:

person.name:
  device_trackers:
    - device_tracker.name
  entity_picture:  '/local/img/name_home.png'
  state_filter:
    not_home:       grayscale(90%)

2:

person.name:
  device_trackers:
    - device_tracker.name
  entity_picture:  '/local/img/name_home.png'
  state_image:
    home:          '/local/img/name_home.png'
    not_home:      '/local/img/name_not_home.png'

3:

person.name:
  device_trackers:
    - device_tracker.name
  entity_picture:  >
    {% if is_state(config.entity, 'home' %}  '/local/img/name_home.png' {% else %} '/local/img/name_not_home.png' {% endif %};

Can anyone point me in the right direction?

1 Like

You’re putting it all in the wrong spot. ui-lovelace.yaml. I suggest you read up on modifying the interface by checking out the lovelace docs.

Sorry, forgot to post my ui-lovelace.yaml:

background:     var(--background-image)

custom_header:
  header_text:  '{{ time }}' # See templates section for more on this.
  compact_mode: true

resources:
  - url:        /community_plugin/custom-header/custom-header.js
    type:       module
  - url:        /community_plugin/lovelace-card-mod/card-mod.js
    type:       module
  - url:        /local/simple-thermostat.js?v=1
    type:       module

views:
  - !include lovelace/views/default.yaml
  - !include lovelace/views/overview.yaml

No, you still don’t get it. This:

does not go in customize, it goes in lovelace on the card you are configuring. And that purely depends on the card you are using. Some cards do not accept state_image. You need to review the documentation for that.

@petro thx for the info, I thought that could be set globally (sort of).

When I read the docs on glance card, it gives a picture attribute, which works for a ‘static’ image:

cards:

  - type:                           glance
    show_header_toggle:             false
    show_name:                      false
    show_state:                     true
    entities:
      - entity:                     person.name
        image:                      '/local/img/name_home.png'

When I try to use it ‘dynamicly’ it returns empty:

- entity:                     person.name
  image:                      >
    {% if is_state(config.entity, 'home' %}  '/local/img/name_home.png' {% else %} '/local/img/name_not_home.png' {% endif %}

Will fidle some more and report any progress.

You can’t use templates outside template fields. What you’re trying to do is piece meal configurations all over the place. This is not correct. What is listed in the documentation is what can be done. If the field in the documents does not mention templates, you cannot use templates. For glance card, you cannot dynamically change the image.

If you want to change the image dynamically on the glance card, you’d need card mod (a custom lovelace card, or a templating card) to make the change.

every time I need a dynamic config in Lovelace which isnt natively supported, I try to see if I can come up with a backend template sensor, which can then be used in the concrete Lovelace frontend.

Example, you can do something like this for entity_pictures on Person. On anything really.

      marijn_picture:
        value_template: >
          {% set person = states.device_tracker.life360_marijn %}
          {% set zones = states.zone | map(attribute='name')|list %}
          {% set name = person.entity_id.split('360_')[1] %}
          {% if person.state in zones %} /local/family/{{name}}_zoning.png
          {% elif person.state in ['moving','driving'] %} /local/family/{{name}}_not_home.png
          {% else %} /local/family/{{name}}_{{person.state|lower|replace(' ','_')}}.png
          {% endif %}

And use these in your glance card. Of course it all depends on your device_trackers and composite set of these trackers. This is a value_template for a separate sensor. You could also use it as entity_picture_template elsewhere

I customized my person like this (using custom-ui) :

# https://www.home-assistant.io/integrations/person/
homeassistant:
  customize:
    person.marijn:
      templates:
        entity_picture: >
          return entities['sensor.marijn_picture'].state ;

see few post down, how to do it in a native HA template sensor.

Doesn’t this also require custom ui? Pretty sure any template in the customize section does.

this does indeed. When you see javascript, its custom-ui…

As I use that, it was easy for me to do it this way

but you could simply create a template sensor based on the person entity, and then show that in the glance card.

marijn_person_template:
  value_template: >
    {{states('person.marijn')}}
  entity_picture_template: >
    {{states('sensor.marijn_picture'(}}

I’m home :wink:

Right, but @RvdHNL doesn’t know that. So posting it without saying that isn’t the best thing to do…

my pardon. you’re right, Ive corrected the post with the relevant link to custom-ui.

btw, to be precise, have a look here for what is available in native HA and customize:

in fact, biggest difference now adays is the icon_color which can be templated through custom-ui.
furthermore, custom-ui uses the browser front-end (and its processor power) while native jinja is back-end, server side processing. Depending on your hardware, and the RPi’s as advised device, it can be very welcome to use custom-ui…

Sorry for the late response. Yesterday the new Hassio update (which was done by the update button in hassio) killed my rpi4 install. So yesterday created a new image and restored last snapshot.

So what I’m getting from this thread is 3 things:

  1. Read the docs
  2. For customizing use addons
  3. Educate others

The documentation is quite overwhelming…(Note! NOT a banter!). It is so extensive, that it is hard to get “to the point”. After my initial setup is done, and I finally know what makes HA tick, and how to get it to tick, I’ll try to contribute with a “HA for dummies” kinda tutorial/documentation.

@Mariusthvdb
Where do you template the sensors? Is that in a seperate yaml which you include? Do you have your config on github somewhere so I can see how you set it up?

No, no GitHub, sorry.

Just define the sensors, in which ever place you do, it all depends on your personal setup.

I have a package based setup, and indeed, as a consequence , have these in a dedicated file.

Ok, this file is included in config.yaml or ui-lovelace.yaml?

In other words; where to include a templating file so it can be “accessed” from a view file?

ha, even that can be answered depending on your setup.

Depending on the Card, Lovelace can accept state dependent presentation. Meaning you then set the icon/color, what have you, in that specific card for the used entity.

But, since Lovelace doesn’t accept Jinja templating (except for the Markdown card), I tend to write/use my own (backend) template sensors, and customized entities, and use these in the Lovelace, which is pure frontend thing. All customizing is then does in the backend, no more need to burden that on the Lovelace cards.

So, the sensor I wrote above for the person picture, would be a backend template sensor. defined under

homeassistant:
  sensor:
    - platform: template
        sensors:
          person_picture:
            etc
            etc

and, depending on if and how you split up your configuration, have it in the configuration.yaml (not advised, but possible) or in a dedicated file in a dedicated folder :wink:

this would be a fine moment to read up on that https://www.home-assistant.io/docs/configuration/splitting_configuration/

1 Like

sensor does not go under the home assistant platform…

Thank you for the response!

O dear my bad. Sorry for that c&p going wrong…

Can someone who’s been able to get this to work post a summary of exactly where the “if” statement needs to be written? I want to have a different picture when I’m home as uposed to when away. I’ve tried to “customize” the person entity with the if statement in the JSON data field with no luck.