Display the number of photos on your digital picture frame in Home Assistant


Display the number of photos on your digital picture frame in Home Assistant 1

Display the number of photos on your digital picture frame in Home Assistant 2I am always amazed by what Home Assistant, the home automation software, is capable of.

One piece of information I always wanted to see is how many images I have loaded onto the frame over time. With Home Assistant and MQTT this is very simple to do, and I will show how to do it.

The following instructions assume that you are running Home Assistant and that you have set up an MQTT broker like CloudMQTT.

If you are not familiar with this technology, please read my article “Control your digital picture frame with Home Assistant’s wifi presence detection and MQTT” first.

More than you can count

Python has this pretty cool os.walk command which can be used for many applications.

os.walk() generates the file names in a directory tree by “walking” the tree either top-down or bottom-up. It is recursive and also looks at all the subdirectories. You can then use the result and count the number of files in a given directory.

When I first tried the os.walk command, it didn’t add up. I looked at the number of objects of the shared Pictures folder (about 1,300), but I knew that this also included the folders. I estimated the number of subfolders to be around 150, but the counted number using os.walk was about over 2,500.

I quickly had a suspicion about the culprit.

As much as I love Apple computers, they have the nasty habit that they leave something behind when they access shared network folders. And for some reason, os.walk() counts these files that you cannot see in the network folder as they are hidden.

The files in question have an “.AppleDouble” name part which makes them easy to identify. After doing some further research, I also needed to exclude “.” hidden files and “.sync” files.

So in the end, my function had a condition that only counted “real” jpg files.

if ext in ('.jpg') and not '.AppleDouble' in root and not name.startswith('.') and not '.sync' in root:

I ran a few tests and low and behold, this time the number was accurate.

So, the first step is to add the following Python script to the home directory of your Raspberry Pi digital picture frame.

Either copy and paste it or download it from here. Save it as count_images.py.

Get your setting for username and password from CloudMQTT as described in the aforementioned article. Adjust the directory “ourphotos” to your images folder.

  1. #!/usr/bin/env python3
  2.  
  3. import paho.mqtt.client as mqtt
  4. import os
  5.  
  6. client = mqtt.Client()
  7. client.username_pw_set( "username" , "password" )
  8.  
  9. client.connect("m23.cloudmqtt.com", 17905, 60)
  10.  
  11. client.publish("frame/images/available", "online")
  12.  
  13. searchdir = r"/home/pi/Pictures/ourphotos"  # your search starts in this directory (your root) 
  14.  
  15. image_count = 0
  16. for root, dirs, files in os.walk(searchdir):
  17.     for name in files:
  18.         ext = os.path.splitext(name)[1].lower()
  19.         if ext in ('.jpg') and not '.AppleDouble' in root and not name.startswith('.') and not '.sync' in root:  # check the extension
  20.             image_count += 1
  21.  
  22. image_count = "{:,}".format(image_count)
  23.  
  24. print(image_count)
  25.  
  26. client.publish("frame/images", image_count)

The number of images is being published via MQTT in the topic “frame/images”.

To run this script every hour, you have to add a line to crontab.

crontab -e

Add this line at the end:

0 */1 * * * python3 /home/pi/count_images.py

Save and close. It’s probably a good idea to restart the digital picture frame after this.

Now we have to tell Home Assistant to pick up this sensor and display it on the frontend.

Setting up the image count sensor in Home Assistant

Add this paragraph in configuration.yaml:

#MQTT Sensor
sensor:
  - platform: mqtt
    name: "image_count_frame"
    state_topic: "frame/images"
    unit_of_measurement: 'Photos'
    availability_topic: "frame/images/available"
    payload_available: "online"
    payload_not_available: "offline"

Restart Home Assistant and check the device states.

In the front end of Home Assistant, probably Lovelace, define the entity like in this example:

entities:
  - entity: sensor.image_count_frame
    icon: 'mdi:camera-burst'
    name: Number of images
show_header_toggle: false
title: Digital Picture Frame
type: entities

Restart Home Assistant. It may take up to one hour until the number of photos is shown (as defined in the crontab -e).

Display the number of photos on your digital picture frame in Home Assistant 3

Congratulations. You’re done!

Wolfgang Männel

Photography has always been a passion of mine. Since 2005, I have looked at countless ideas on how to enjoy beautiful images digitally, making the experience entertaining, amusing, and stimulating. In my other life, I am an entrepreneur, consultant, and bicyclist based near Frankfurt am Main, Germany.

Recent Content