Notify when card is shown

Cards often have a ‘Show if empty’ option. What I want is an automation which sends a notification if a card changes from not shown (card is emty) to shown.

I have for example a battery empty card which only shows the battery entities when the battery level is below 10%. On that occasion I want to get notified on my phone.

Is there a way to detect if a card is visible or not?

Just make a normal automation that triggers on the same entities that you will use to determine whether you’ll show your card.

Unfortunately that’s not a good solution in this case. I’m using an auto-entities card and that card will pick the entities automatically. So if I add a battery powered device to my Home Assistant, the battery entity is automatically added to the card. I don’t want to add all entities by hand.

type: custom:auto-entities
card:
  show_header_toggle: false
  title: Battery low
  type: entities
  state_color: true
filter:
  include:
    - attributes:
        device_class: battery
      state: <=10
  exclude:
    - name: /[Ll]ow/
    - name: /[Ss]tate/
    - name: Lux*
sort:
  methode: state
  numeric: true
show_empty: false

Hi,

What @parautenbach says.
I have 2 automations for each battery > Charged of Discharged notify.

alias: Melding Rolgordijn 1 Batterij 20%
description: ""
trigger:
  - type: battery_level
    platform: device
    device_id: 85515cbb8a40faaac9845a3ce1fbf4c3
    entity_id: sensor.rolgordijn1_battery
    domain: sensor
    below: 20
condition: []
action:
  - service: notify.mobile_app_leroy
    data:
      message: Rolgordijn 1 onder de 20%
      title: 🔋 Batterij
      data:
        ttl: 0
        priority: high
mode: single

Maybe you can template…but I am not a specialist in that stuff…

I use this:

Yeah thanks, I’ve seen and used that solution, but I have more cards which are hidden if empty. The battery card was just an example.

There should however be a solution for card visibility detection.

No, it’s backwards. This is not the way to build this. You don’t control backend things in that way from a frontend. Nobody that understands software engineering would do this.

It’s a bit involved, but here’s my automation for this kind of thing:

- alias: "Check For Low Batteries"
  initial_state: true
  variables:
    level: 5
    # https://community.home-assistant.io/t/variable-in-automation-not-working-as-expected/431591/2
    monitored_batteries: >-
      {{
        states.sensor
          | selectattr('attributes.monitor', 'defined')
          | selectattr('attributes.monitor', 'eq', True)
          | rejectattr('state', 'in', ['unavailable', 'unknown', 'none'])
          | selectattr('attributes.device_class', 'eq', 'battery')
          | map(attribute='entity_id')
          | list
      }}
  trigger:
    platform: time
    at: "09:00:00"
  condition: >-
    {{
      expand(monitored_batteries)
        | map(attribute='state')
        | map('int')
        | select('lt', level)
        | list
        | count
        > 0
    }}
  action:
    - service: notify.mobile_app_ceres
      data:
        title: "Batteries"
        # https://community.home-assistant.io/t/recommended-ways-to-manage-devices-and-entities-names/243815/12
        message: >
          The following devices have less than {{ level }}% charge:
          {%- for b in monitored_batteries %}
            {%- if states(b) | int < level and not is_state(b, 'unavailable') %}
            - {{ state_attr(b, 'friendly_name') | replace(' Battery', '') }}: {{ states(b) | int }}%
            {%- endif -%}
          {%- endfor %}
        data:
          group: "batteries"
          url: homeassistant://navigate/lovelace/devices

All I have to do now is to add a special attribute to a device with a battery I want monitored by this, for example:

sensor.pieter_bedside_light_button_battery:
  friendly_name: Pieter's Bedside Light Button Battery
  monitor: True

It’s even easier if you want to monitor all batteries. In my case, I only want to monitor certain batteries. In other words, this:

      {{
        states.sensor
          | selectattr('attributes.monitor', 'defined')
          | selectattr('attributes.monitor', 'eq', True)
          | rejectattr('state', 'in', ['unavailable', 'unknown', 'none'])
          | selectattr('attributes.device_class', 'eq', 'battery')
          | map(attribute='entity_id')
          | list
      }}

can then be simplified to this:

      {{
        states.sensor
          | rejectattr('state', 'in', ['unavailable', 'unknown', 'none'])
          | selectattr('attributes.device_class', 'eq', 'battery')
          | map(attribute='entity_id')
          | list
      }}