I would like to show in a custom button whether my children are present. For this I have several images which I can then call with a value.
To be able to do this I want to make a sensor that creates different states so that I can call the images with it.
When child A is at home and child b and c are not, the sensor must give the state a-home, when child b is at home b-home and child c c-home, in addition also the following combinations: when child a and b are at home must be deliver the sensor ab-home etc etc
Is this the right way to do this or is there another way?
Example of the Custom Button:
- type: custom:button-card
entity: sensor to be created
show_entity_picture: true
show_name: false
name: Kids
show_state: true
state:
- value: a-home
entity_picture: /local/a-home.png
- value: b_home
entity_picture: /local/b-home.png
You need to expand the slice by changing [0] to [0:3]:
template:
- sensor:
- name: "Which kids are home"
state: >
{% set ns = namespace(values='') %}
{% for e in expand('person.alex', 'person.blair', 'person.cassidy') %}
{% if e.state == 'home' %}
{% set ns.values = ns.values + (e.name)[0:3] | lower %}
{% endif %}
{% endfor %}
{{iif(ns.values != '', ns.values,'not')}}-home
Keep in mind when you set up your card, the values will follow the order you have them in the expand(). In the example above the possible values will be:
I’m pretty sure you could do this directly in the card, but it’s been a while since I messed with the type of templating that custom-button-card uses, so I’ll leave that for someone else with more experience…
Not sure if this is something on the order of what you’re looking for, but this is what I do with our family presence:
In my ui-lovelace. yaml file:
button_card_templates: !include lovelace-templates-button-card.yaml
# other non-related stuff
- entity: sensor.family_sensor
name: Presence
template: presence-tile-compact
type: 'custom:button-card'
variables:
m1_entity: device_tracker.sm_n986u
m1_name: R
m2_entity: device_tracker.janettes_phone
m2_name: J
m3_entity: device_tracker.kates_phone
m3_name: K
The “family sensor” is in a /config/sensors.yaml file as follows:
- platform: template
sensors:
family_sensor:
value_template: >-
{% set items = states.person %}
{% set all = items|length %}
{% set home = items|map(attribute="state")|select("equalto","home")|list|length %}
{{ "none home" if home == 0 else "all home" if home == all else "some home" }}
icon_template: >-
{% if is_state('family_sensor', 'none') %}
mdi:home-circle-outline
{% else %}
mdi:home-circle
{% endif %}
and the “presence-tile-compact” template is in my lovelace-templates-button-card.yaml file as follows: (with the other definitions I need as well)
This also requires the custom: button-card available through the link above and also through HACS.
It looks like this:
For the location icons to work, you need to set up your zones with names that will “parse” properly. For example the Icc location above is based on a zone named “ICC” with an icon of “mdi:office-building”.