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

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 3

I 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 easy to achieve, and I will show how to do it.

The following instructions assume that you are running Home Assistant and that you have installed an MQTT broker like Mosquitto on your Raspberry Pi.

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.

The python script to count the images

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

This script is automatically triggered by a change in your Pictures directory when you add or remove images.

In requires a package called “Watchdog” that you install with

python3 -m pip install watchdog

Open your file editor. Copy & paste the content below it and save it as count_images_mqtt.py.

Enter the IP address of your Mosquitto broker and adjust the “searchdir” to your images folder.


#requires installation of watchdog with "python3 -m pip install watchdog"

import time 
from watchdog.observers import Observer 
from watchdog.events import FileSystemEventHandler
import paho.mqtt.client as mqtt
import os

client = mqtt.Client()
client.connect("your-ip-address-of-mqtt-broker", 1883, 60)

searchdir = r"/home/pi/Pictures"  #image count search directory 

def count_images():
	image_count = 0
	for root, dirs, files in os.walk(searchdir):
		for name in files:
			ext = os.path.splitext(name)[1].lower()
			if ext in ('.jpg') and not '.AppleDouble' in root and not name.startswith('.') and not '.sync' in root:
				image_count += 1
	image_count = "{:,}".format(image_count)
	client.publish("frame/images", image_count)

#Initial count of images

class OnMyWatch: 
	# Set the directory on watch 
	watchDirectory = searchdir

	def __init__(self): 
		self.observer = Observer() 

	def run(self): 
		event_handler = Handler() 
		self.observer.schedule(event_handler, self.watchDirectory, recursive = True) 
			while True: 
			print("Observer Stopped") 


class Handler(FileSystemEventHandler): 

	def on_any_event(event): 
		if event.is_directory: #look only for directory changes
			return None

if __name__ == '__main__': 
	watch = OnMyWatch() 

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

Launching the script at boot

To activate this script at boot, we’ll use systemd.


sudo nano /etc/systemd/system/count_images.service

Paste the following text into the editor window

Description=Count images in Pictures

ExecStart=/usr/bin/python3 /home/pi/count_images_mqtt.py


Save and close.

Now change the file permissions with

sudo chmod 644 /etc/systemd/system/count_images.service

and update the services with

sudo systemctl daemon-reload
sudo systemctl enable count_images.service


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
  - platform: mqtt
    name: "image_count_frame"
    state_topic: "frame/images"
    unit_of_measurement: 'Photos'

Restart Home Assistant and check the device states.

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

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

Restart Home Assistant. The number of photos should now be shown. Test it by adding a few images and deleting them again.

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

Congratulations. You’re done!


This is another example of how you can connect your digital picture frame and your Home Automation system with simple MQTT messages.

Let me know what you think!

Was this post helpful?

Scroll to Top