Get decluttering-card and auto-entities templates work together

I have docker-monitor for multiple docker instances
It creates sensors such as sensor.rpi_docker_ha_state - where rpi is the Docker name and ha is the container name.

I’ve created a decluttering-template which knows how to present an extensive data accordingly

e.g.
entities:
  - type: custom:decluttering-card
    template: docker_template
    variables:
      - host: rpi
      - container: ha
     ...
show_header_toggle: false
title: RPi Containers
type: entities

I want to have this generated automatically - based on a regex;
i.e. Given a Docker Host (rpi) i’ll fetch all states.sensor.rpi_docker_*_state - extract the * via string concatination and will pass it to the decluttering-card.

My biggest challenge for now - I saw that auto-entities allows to use templating with options.
e.g.

show_header_toggle: false
title: RPi Containers
type: entities
entities:
  - type: custom:auto-entities
    card:
      type: custom:decluttering-card
      template: docker_template
      variables:
        - host: ubuntu
    filter:
      template: |
        {{ states.sensor | selectattr("entity_id", "search", "rpi") | selectattr("entity_id", "search", "_state") | map(attribute='entity_id') | list | replace('sensor.ubuntu_docker_', '') | replace('_state', '') }}
      options:
        variables:
          container: this

Here - the template returns the list of the container names.
I tried also getting the Entities themselves (without the two last replace) - still no work.
It seems that the container field fails to be filled from the template.

This gives me this:
image

I’ve checked this thread: Combining auto-entities and decluttering cards challenge - without any work

Any help would be appreciated.

– When using entities card instead of decluttering-card it seems to be working (but showing just the single entity):

    card:
      type: entities
...
1 Like

Have you verified in dev tools that the sensors have valid states?

Try this filter on the auto entries card:

type: custom:auto-entities
sort:
  method: state
  numeric: false
card:
  type: entities
  show_header_toggle: false
  title: Ubuntu Containers
  state_color: true
show_empty: false
unique: true
filter:
  include:    
    - entity_id: '*rpi_docker_*_state'
  exclude:
    - state: '*unknown*'
    - state: '=0'
    - state: 'off'
    - state: unavailable
    - domain: automation

As for displaying a different name, just change the friendly name where you do the sensor or use customize.yaml to make it what you want.

Yes, the states are valid.

Note that in your example you used entities card.
I’d like to use the decluttering-card and pass to it a string, based on a regex (used in auto-entities).

in other words:
findAll(states.sensor.rpi_docker_*_state) --> trim(entity_id) [to containerName] --> set to decluttering_card.variables.container variable

Are you trying to do something like this?

how did you do the binary sensor for pending updates?

1 Like

I have a script that does a docker pull for each image I have (pull only, I only download new images via the script). I update the actual container when I have time to deal with any potential issues.
The script checks how many images I have of a given container; I send that information via MQTT to HA.
I have binary sensors set to be true if the number of images with that specific name is >1.

I posted the script somewhere here, but I couldn’t find it easily. Someone asked for it there and I posted the scripts and yaml.

I have the script run via a cron job every hour. A new image is only download if there is a update available.
Example binary sensor (add more as needed):

- platform: template
  sensors:
    dock_jellyfin_update:
      friendly_name: "Jellyfin Update Available"
      value_template: "{{ states('sensor.docker_image_jellyfin')|float > 1 }}"

cron job calls this (modify as needed):

echo Starting Docker Pulls
sudo docker pull homeassistant/home-assistant:stable && sudo docker pull portainer/portainer-ce && sudo docker pull linuxserver/grocy && sudo docker pull linuxserver/jellyfin && sudo docker pull linuxserver/swag && sudo docker pull  synesthesiam/marytts:5.2 && sudo docker pull eclipse-mosquitto:1.6.13 && sudo docker pull zwavejs/zwavejs2mqtt && sudo docker pull briis/weatherflow2mqtt:dev
sudo python3 /home/glenn/DockerMQTT.py

The MQTT script:

# Revision Date 12/28/2020 2047

# Requires paho-mqtt for MQTT intergration; if not using MQTT, then ignore
# sudo python3 -m pip install paho-mqtt

import datetime, time
import paho.mqtt.client as mqtt # Allow publishing to MQTT Server
import subprocess

# MQTT Configuration:
MQTT_enable     = True          # Enable MQTT
Broker_IP = "10.74.1.224"       # MQTT Broker IP
Broker_Port = "1883"            # MQTT Broker Port

now = datetime.datetime.now()
ETime = str(now.strftime(" %H:%M:%S on %m-%d-%Y"))

''' Only comment out the Debug = True below to turn off Debug '''
Debug = False
Debug2 = False

#Debug = True           # Uncomment and Comment out this line for Debug
if Debug is True:       # Debug Level 2
  Debug2 = False
  #Debug2 = True        # Uncomment and Comment out this line for Debug Level 2

#########

def MQTT(MQ_Topic,MQ_Payload):
  # Send to the MQTT Broker
  try:
    if MQTT_enable is True:
      if Debug2 is True:
        print ("MQTT pre Client")
      mqttc = mqtt.Client("python_pub")
      if Debug2 is True:
        print ("MQTT pre Connect")
      mqttc.connect(Broker_IP)
      if Debug2 is True:
        print ("MQTT pre Publish")
      mqttc.publish(MQ_Topic, MQ_Payload, retain = True)
      if Debug is True:
        print ("MQTT Publish Complete for " + MQ_Topic)
      if MQTT_enable is False:
        if Debug is True:
          print ("MQTT is not enabled")
  except:
    # Prevent crashing if Broker is disconnected
    if Debug is True:
      print ("MQTT Failed to publish All")

#########

# Main program:
try:
  if __name__ == '__main__':
    #time.sleep(MainWait) # Pause between Docker Image DownloadsChecks and MQTT Updates
    MQTT("docker/Time",ETime)
    time.sleep (10)
    Dock_MQTT = int(subprocess.check_output('docker image ls -a | wc -l', shell=True, text=True)) - 1
    MQTT("docker/Image",Dock_MQTT)
    time.sleep (10)
    Dock_MQTT = int(subprocess.check_output('docker container ls | wc -l', shell=True, text=True)) - 1
    MQTT("docker/ContRun",Dock_MQTT)
    time.sleep (10)
    Dock_MQTT = int(subprocess.check_output('docker ps -a | wc -l', shell=True, text=True)) - 1
    MQTT("docker/ContLoad",Dock_MQTT)
    time.sleep (10)
    Dock_MQTT = int(subprocess.check_output('docker images -f dangling=true | wc -l', shell=True, text=True)) - 1
    MQTT("docker/ImageUnused",Dock_MQTT)
    time.sleep (10)
    Dock_MQTT = int(subprocess.check_output('docker image ls -a | grep "portainer" | wc -l', shell=True, text=True))
    MQTT("docker/Image/Portainer",Dock_MQTT)
    time.sleep (10)
    Dock_MQTT = int(subprocess.check_output('docker image ls -a | grep "home-assistant" | wc -l', shell=True, text=True))
    MQTT("docker/Image/HA",Dock_MQTT)
    time.sleep (10)
    Dock_MQTT = int(subprocess.check_output('docker image ls -a | grep "swag" | wc -l', shell=True, text=True))
    MQTT("docker/Image/Swag",Dock_MQTT)
    time.sleep (10)
    Dock_MQTT = int(subprocess.check_output('docker image ls -a | grep "marytts" | wc -l', shell=True, text=True))
    MQTT("docker/Image/MaryTTS",Dock_MQTT)
    time.sleep (10)
    Dock_MQTT = int(subprocess.check_output('docker image ls -a | grep "grocy" | wc -l', shell=True, text=True))
    MQTT("docker/Image/Grocy",Dock_MQTT)
    time.sleep (10)
    Dock_MQTT = int(subprocess.check_output('docker image ls -a | grep "jellyfin" | wc -l', shell=True, text=True))
    MQTT("docker/Image/Jellyfin",Dock_MQTT)
    time.sleep (10)
    Dock_MQTT = int(subprocess.check_output('docker image ls -a | grep "mosquitto" | wc -l', shell=True, text=True))
    MQTT("docker/Image/Mosquitto",Dock_MQTT)
    time.sleep (10)
    Dock_MQTT = int(subprocess.check_output('docker image ls -a | grep "zwavejs2mqtt" | wc -l', shell=True, text=True))
    MQTT("docker/Image/Zwave2MQTT",Dock_MQTT)
    time.sleep (10)
    Dock_MQTT = int(subprocess.check_output('docker image ls -a | grep "weatherflow2mqtt" | wc -l', shell=True, text=True))
    MQTT("docker/Image/WF2MQTT",Dock_MQTT)
    time.sleep (10)

except KeyboardInterrupt:
  print ('Interrupted by User at keyboard')
  quit()

Edit I left in all the Debug tools to make it easier for others to find any issues. Follow notes in code to turn debug on.

Sort of…

Something like the picture below.

The problem is that I have to define each container manually, which I want to avoid.