How to display a card, only when certain sensors are non-zero

I’m using the Mail and Packages integration and would like to display progress in a card only when there are packages on the move. There’s probably a better way than using auto-entites, but I have a little knowledge of it. This card below find activity, but how do I use that to selectively display the card?

type: custom:auto-entities
sort:
  method: name
card:
  type: entities
show_empty: false
unique: entity
filter:
  include:
    - entity_id: sensor.mail*delivering
    - entity_id: sensor.mail*packages
  exclude:
    - state: '= 0'
    - state: unavailable

image

How do check for a number 1 or greater,
since I can’t have 2 state_nots: not ‘0’ and not ‘unavailable’?

Also, without wildcards I have 16 entities to check. It’s gonna be wordy.

My solution (mostly glommed and not well tested):

sensor

- platform: template
  sensors:
    package_count:
      friendly_name: 'Package(s) in Motion'
      value_template: >-
        {% set ns = namespace(count=0) %}
          {% for state in states.sensor if 'sensor.mail' in state.entity_id and
                                          state.state|int(0) > 0 %}
            {% set ns.count = ns.count + 1 %}
          {% endfor %}
        {{ ns.count }}

lovelace

type: conditional
conditions:
  - entity: sensor.package_count
    state_not: '0'
card:
  type: custom:stack-in-card
  etc...

@bmorgan I know this is an old post, but wondering if you would mind sharing your card code you are using for this mail and packages? That layout is exactly what I was hoping to achieve and it’s much cleaner than the card that is default for the integration. thanks!

@crankycowboy

I should have put more in the template. I frequently fire up a template and then end up negating most of it when I tweak the card.


    type: conditional
    conditions:
      - entity: sensor.packages_enroute
        state_not: '0'
    card:
      type: custom:stack-in-card
      mode: vertical
      keep:
        border_radius: true
        background: true
        box_shadow: false
        margin: true
      cards:
        - type: conditional
          conditions:
            - entity: sensor.packages_enroute
              state_not: '0'
          card:
            type: grid
            square: false
            columns: 6
            cards:
              - type: custom:button-card
                entity: sensor.packages.enroute
                name: Packages<br>En route
                show_state: false
                show_name: true
                show_label: false
                show_icon: false
                styles:
                  name:
                    - font-size: 13px
                    - font-family: Arial Narrow
                    - margin-top: 13px
                    - transform: rotate(0deg)
                    - color: LightGray
              - type: conditional
                conditions:
                  - entity: sensor.mail_usps_packages
                    state_not: '0'
                card:
                  type: custom:button-card
                  entity: sensor.mail_usps_packages
                  name: USPS
                  icon: mdi:truck
                  template: packages
                  styles:
                    icon:
                      - color: |-
                          [[[
                            if (entity.state == 0)
                              return "Gray";
                            else if (entity.state > 0)
                              return "Blue";
                            else
                              return "Red";
                          ]]]
              - type: conditional
                conditions:
                  - entity: sensor.mail_amazon_packages
                    state_not: '0'
                card:
                  type: custom:button-card
                  entity: sensor.mail_amazon_packages
                  name: Amazon
                  icon: mdi:truck
                  template: packages
                  styles:
                    icon:
                      - color: |-
                          [[[
                            if (entity.state == 0)
                              return "Gray";
                            else if (entity.state > 0)
                              return "Orange";
                            else
                              return "Red";
                          ]]]
              - type: conditional
                conditions:
                  - entity: sensor.mail_ups_delivering
                    state_not: '0'
                card:
                  type: custom:button-card
                  entity: sensor.mail_ups_delivering
                  icon: mdi:truck
                  name: UPS
                  template: packages
                  styles:
                    icon:
                      - color: |-
                          [[[
                            if (entity.state == 0)
                              return "Gray";
                            else if (entity.state > 0)
                              return "Blue";
                            else
                              return "Red";
                          ]]]
              - type: conditional
                conditions:
                  - entity: sensor.mail_fedex_delivering
                    state_not: '0'
                card:
                  type: custom:button-card
                  entity: sensor.mail_fedex_delivering
                  name: FedEx
                  template: packages
                  styles:
                    icon:
                      - color: |-
                          [[[
                            if (entity.state == 0)
                              return "Gray";
                            else if (entity.state > 0)
                              return "Purple";
                            else
                              return "Red";
                          ]]]
              - type: conditional
                conditions:
                  - entity: sensor.mail_dhl_delivering
                    state_not: '0'
                card:
                  type: custom:button-card
                  entity: sensor.mail_dhl_delivering
                  template: packages
                  name: DHL
                  icon: mdi:truck-check
                  styles:
                    icon:
                      - color: |-
                          [[[
                            if (entity.state == 0)
                              return "Gray";
                            else if (entity.state > 0)
                              return "Purple";
                            else
                              return "Red";
                          ]]]
        - type: conditional
          conditions:
            - entity: sensor.packages_delivered
              state_not: '0'
          card:
            type: grid
            square: false
            columns: 6
            cards:
              - type: custom:button-card
                entity: sensor.packages_delivered
                name: Delivered
                show_state: false
                show_label: false
                show_icon: false
                styles:
                  name:
                    - font-size: 13px
                    - font-family: Arial Narrow
                    - margin-top: 17px
                    - transform: rotate(0deg)
                    - color: LightGray
              - type: conditional
                conditions:
                  - entity: sensor.mail_usps_delivered
                    state_not: '0'
                card:
                  type: custom:button-card
                  entity: sensor.mail_usps_delivered
                  name: USPS
                  icon: mdi:truck-delivery-outline
                  template: packages
                  styles:
                    icon:
                      - color: |-
                          [[[
                            if (entity.state == 0)
                              return "Gray";
                            else if (entity.state > 0)
                              return "#0040ff";
                            else
                              return "Red";
                          ]]]
              - type: conditional
                conditions:
                  - entity: sensor.mail_amazon_packages_delivered
                    state_not: '0'
                card:
                  type: custom:button-card
                  entity: sensor.mail_amazon_packages_delivered
                  name: Amazon
                  template: packages
                  show_label: true
                  styles:
                    icon:
                      - color: |-
                          [[[
                            if (entity.state == 0)
                              return "Gray";
                            else if (entity.state > 0)
                              return "Orange";
                            else
                              return "Red";
                          ]]]
              - type: conditional
                conditions:
                  - entity: sensor.mail_ups_delivered
                    state_not: '0'
                card:
                  type: custom:button-card
                  entity: sensor.mail_ups_delivered
                  name: UPS
                  icon: mdi:truck-outline
                  template: packages
                  show_state: false
                  show_label: true
                  styles:
                    icon:
                      - color: |-
                          [[[
                            if (entity.state == 0)
                              return "Gray";
                            else if (entity.state > 0)
                              return "Brown";
                            else
                              return "Red";
                          ]]]
              - type: conditional
                conditions:
                  - entity: sensor.mail_fedex_delivered
                    state_not: '0'
                card:
                  type: custom:button-card
                  entity: sensor.mail_fedex_delivered
                  name: FedEx
                  icon: mdi:truck-fast-outline
                  template: packages
                  show_state: false
                  show_label: true
                  styles:
                    icon:
                      - color: |-
                          [[[
                            if (entity.state == 0)
                              return "Gray";
                            else if (entity.state > 0)
                              return "Purple";
                            else
                              return "Red";
                          ]]]
              - type: conditional
                conditions:
                  - entity: sensor.mail_dhl_delivered
                    state_not: '0'
                card:
                  type: custom:button-card
                  entity: sensor.mail_dhl_delivered
                  template: packages
                  name: DHL
                  icon: mdi:truck-check-outline
                  show_state: false
                  show_label: true
                  styles:
                    icon:
                      - color: |-
                          [[[
                            if (entity.state == 0)
                              return "Gray";
                            else if (entity.state > 0)
                              return "Yellow";
                            else
                              return "Red";
                          ]]]
        - type: conditional
          conditions:
            - entity: sensor.package_exceptions
              state_not: '0'
          card:
            type: grid
            square: false
            columns: 6
            cards:
              - type: custom:button-card
                entity: sensor.package_exceptions
                name: Exception
                show_state: false
                show_label: false
                show_icon: false
                styles:
                  name:
                    - font-size: 13px
                    - font-family: Arial Narrow
                    - margin-top: 10px
                    - transform: rotate(0deg)
                    - color: White
              - type: conditional
                conditions:
                  - entity: sensor.mail_amazon_exception
                    state_not: '0'
                card:
                  type: custom:button-card
                  entity: sensor.mail_amazon_exception
                  name: Amazon
                  icon: mdi:gift-off-outline
                  template: packages
                  show_state: false
                  show_label: true
                  styles:
                    icon:
                      - color: |-
                          [[[
                            if (entity.state == 0)
                              return "Gray";
                            else if (entity.state > 0)
                              return "Orange";
                            else
                              return "Red";
                          ]]]
              - type: conditional
                conditions:
                  - entity: sensor.mail_usps_exception
                    state_not: '0'
                card:
                  type: custom:button-card
                  entity: sensor.mail_usps_exception
                  name: USPS
                  icon: mdi:truck-delivery-outline
                  template: packages
                  show_state: false
                  show_label: true
                  styles:
                    icon:
                      - color: |-
                          [[[
                            if (entity.state == 0)
                              return "Gray";
                            else if (entity.state > 0)
                              return "#0040ff";
                            else
                              return "Red";
                          ]]]
              - type: conditional
                conditions:
                  - entity: sensor.mail_ups_exception
                    state_not: '0'
                card:
                  type: custom:button-card
                  entity: sensor.mail_ups_exception
                  name: UPS
                  icon: mdi:truck-outline
                  template: packages
                  show_state: false
                  show_label: true
                  styles:
                    icon:
                      - color: |-
                          [[[
                            if (entity.state == 0)
                              return "Gray";
                            else if (entity.state > 0)
                              return "Brown";
                            else
                              return "Red";
                          ]]]

template

  packages:
    show_state: false
    show_label: true
    label: |-
      [[[
        if (entity.state > 0 &&
        entity.state < 100)
          return entity.state;
        else
          return "&nbsp";
      ]]]
    styles:
      card:
        - '--keep-background': 'false'
        - border-radius: 10px;
        - border: |-
            [[[
              if (entity.state > 0 &&
              entity.state < 100)
                return '1px solid gray';
              else
                return " ";
            ]]]
      label:
        - font-size: 13px
        - color: |-
            [[[
              if (entity.state > 0 &&
              entity.state < 100)
                return "White";
              else if (entity.state == 0)
                return "Gray";
              else
                return "Red";
            ]]]
      name:
        - font-size: 13px
        - color: |-
            [[[
              if (entity.state > 0 &&
              entity.state < 100)
                return "White";
              else if (entity.state == 0)
                return "Gray";
              else
                return "Red";
            ]]]
1 Like