Sensor based on different states

Who can help me?

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.

The idea is as follows:

Child A: device_tracker_kind_a
Child B: device_tracker_kind_b
Child C: device_tracker_kind_c

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

Regards, Marius

What about using the first letter (or first 2 letters) of their names…

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] | lower %}
          {% endif %}
          {% endfor %}
          {{iif(ns.values != '', ns.values,'not')}}-home

Drew,

this is perfect, what do I need to change for the first 3 letter, my 2 daughters are lin and lil so the first or first 2 letters wont do the job :wink:

1 Like

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:

ale-home
alebla-home
aleblacas-home
bla-home
blacas-home
cas-home
not-home

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…

You are a hero !!! It works like a charm !!! thank you very much !

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)

  standard:
    color_type: card
    size: 80%
    hold_action:
      action: more-info
    styles:
      card:
        - padding: 0.2em
        - '--mdc-ripple-color': yellow
        - '--mdc-ripple-press-opacity': 0.5
      icon:
        - opacity: 0.75
      name:
        - font-size: 0.5em
        - white-space: normal
      state:
        - font-size: 0.5em
        - white-space: normal
      label:
        - font-size: 0.5em
        - white-space: normal
  wide:
    template: standard
    styles:
      grid:
        - position: relative
        - grid-template-areas: '"i n"'
        - grid-template-columns: 1fr 1fr
        - grid-template-rows: 1fr
  container:
    color_type: label-card
    color: dimgray
    styles:
      card:
        - padding: 0
      name:
        - border-radius: 0.4em 0.4em 0 0
        - padding: 0.0em
        - width: 100%
        - font-weight: bold
        - font-size: 0.6em
      grid:
        - grid-template-areas: '"i" "n" "buttons"'
        - grid-template-columns: 1fr
        - grid-template-rows: 1fr min-content min-content
      custom_fields:
        buttons:
          - background-color: 'rgba(0,0,0,0.3)'
          - margin: 0
          - padding: 0.15em
  standard-button:
    template: standard
    variables:
      value_on: 'on'
      value_off: 'off'
      background_color_on: gold
      background_color_off: slategrey
      text_color_on: blue
      text_color_off: white
    styles:
      label:
        - color: '[[[ return variables.text_color_off ]]]'
    state:
      - id: value_on
        value: '[[[ return variables.value_on ]]]'
        styles:
          card:
            - background-color: '[[[ return variables.background_color_on ]]]'
          name:
            - color: '[[[ return variables.text_color_on ]]]'
          icon:
            - color: '[[[ return variables.text_color_on ]]]'
            - opacity: 1
      - id: value_off
        value: '[[[ return variables.value_off ]]]'
        styles:
          card:
            - background-color: '[[[ return variables.background_color_off ]]]'
          name:
            - color: '[[[ return variables.text_color_off ]]]'
          icon:
            - color: '[[[ return variables.text_color_off ]]]'
            - opacity: 0.5
# presence-tile
  presence-tile-compact:
    color_type: card
    triggers_update: all
    aspect_ratio: 3/1.5
    tap_action:
      action: navigate
      navigation_path: /lovelace/map
    styles:
      card:
        - background-color: darkGreen
        - border-radius: 0%
        - padding: 1%
        - color: ivory
        - font-size: 11px
        - text-transform: capitalize
      grid:
        - grid-template-areas: '"n n n n n" "i i m1 m1 m1" "i i m2 m2 m2" "fam fam m3 m3 m3"'
        - grid-template-columns: 1fr 1fr 1fr 1fr 1fr
        - grid-template-rows: 1fr 1fr 1fr 1fr
      name:
        - font-weight: bold
        - font-size: 13px
        - color: white
        - align-self: middle
        - justify-self: start
        - padding-bottom: 0px
      img_cell:
        - justify-content: start
        - align-items: start
        - margin: 0%
      icon:
        - color: |
            [[[
              if (entity.state == 'some home') return 'white';
              if (entity.state == 'none home') return 'lightGrey';
              else return 'yellow';
            ]]]
        - width: 70%
        - margin-top: 0%
      custom_fields:
        fam:
          - font-size: 10px
          - align-self: middle
          - justify-self: start
        m1:
          - align-self: middle
          - justify-self: start
        m2:
          - align-self: middle
          - justify-self: start
        m3:
          - align-self: middle
          - justify-self: start
    custom_fields:
      fam: '[[[ return `${entity.state}` ]]]'
      m1: |
        [[[
          var icon = "mdi:map-marker";
          var color = "lightGrey";
          var status = states[variables.m1_entity].state;
          if (status != 'not_home') {
            icon = states['zone.'+status].attributes['icon'];
            color = "white";
          }
          return `<span>${variables.m1_name}:</span>
          <ha-icon icon="${icon}"
          style="width: 14px; height: 14px; color: ${color};"></ha-icon>
          <span>${status}</span>`
        ]]]
      m2: |
        [[[
          var icon = "mdi:map-marker";
          var color = "lightGrey";
          var status = states[variables.m2_entity].state;
          if (status != 'not_home') {
            icon = states['zone.'+status].attributes['icon'];
            color = "white";
          }
          return `<span>${variables.m2_name}:</span>
          <ha-icon icon="${icon}"
          style="width: 14px; height: 14px; color: ${color};"></ha-icon>
          <span>${status}</span>`
        ]]]
      m3: |
        [[[
          var icon = "mdi:map-marker";
          var color = "lightGrey";
          var status = states[variables.m3_entity].state;
          if (status != 'not_home') {
            icon = states['zone.'+status].attributes['icon'];
            color = "white";
          }
          return `<span>${variables.m3_name}:</span>
          <ha-icon icon="${icon}"
          style="width: 14px; height: 14px; color: ${color};"></ha-icon>
          <span>${status}</span>`
        ]]]

This also requires the custom: button-card available through the link above and also through HACS.
It looks like this:
presence tile
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”.

1 Like