The 100 best digital picture frames on amazon
The 100 best digital picture frames on amazon
The 100 best digital picture frames on amazon
The 100 best digital picture frames on amazon

Dis­play the num­ber of pho­tos on your dig­i­tal pic­ture frame in Home Assis­tant

-

I am always amazed by what Home Assis­tant, the home automa­tion soft­ware, is capa­ble of.

One piece of infor­ma­tion I always want­ed to see is how many images I have loaded onto the frame over time. With Home Assis­tant and MQTT this is very sim­ple to do, and I will show how to do it.

The fol­low­ing instruc­tions assume that you are run­ning Home Assis­tant and that you have set up an MQTT bro­ker like Cloud­MQTT.

If you are not famil­iar with this tech­nol­o­gy, please read my arti­cle "Con­trol your dig­i­tal pic­ture frame with Home Assistant's wifi pres­ence detec­tion and MQTT" first.

More than you can count

Python has this pret­ty cool os.walk com­mand which can be used for many appli­ca­tions.

os.walk() gen­er­ates the file names in a direc­to­ry tree by "walk­ing" the tree either top-down or bot­­tom-up. It is recur­sive and also looks at all the sub­di­rec­to­ries. You can then use the result and count the num­ber of files in a giv­en direc­to­ry.

When I first tried the os.walk com­mand, it didn't add up. I looked at the num­ber of objects of the shared Pic­tures fold­er (about 1,300), but I knew that this also includ­ed the fold­ers. I esti­mat­ed the num­ber of sub­fold­ers to be around 150, but the count­ed num­ber using os.walk was about over 2,500.

I quick­ly had a sus­pi­cion about the cul­prit.

As much as I love Apple com­put­ers, they have the nasty habit that they leave some­thing behind when they access shared net­work fold­ers. And for some rea­son, os.walk() counts these files that you can­not see in the net­work fold­er as they are hid­den.

The files in ques­tion have an ".Apple­Dou­ble" name part which makes them easy to iden­ti­fy. After doing some fur­ther research, I also need­ed to exclude "." hid­den files and ".sync" files.

So in the end, my func­tion had a con­di­tion that only count­ed "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 num­ber was accu­rate.

So, the first step is to add the fol­low­ing Python script to the home direc­to­ry of your Rasp­berry Pi dig­i­tal pic­ture frame.

Either copy and paste it or down­load it from here. Save it as count_images.py.

Get your set­ting for user­name and pass­word from Cloud­MQTT as described in the afore­men­tioned arti­cle. Adjust the direc­to­ry "our­pho­tos" to your images fold­er.

  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 num­ber of images is being pub­lished via MQTT in the top­ic "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 prob­a­bly a good idea to restart the dig­i­tal pic­ture frame after this.

Now we have to tell Home Assis­tant to pick up this sen­sor and dis­play it on the fron­tend.

Set­ting up the image count sen­sor in Home Assis­tant

Add this para­graph 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 Assis­tant and check the device states.

In the front end of Home Assis­tant, prob­a­bly Lovelace, define the enti­ty like in this exam­ple:

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 Assis­tant. It may take up to one hour until the num­ber of pho­tos is shown (as defined in the crontab -e).

Con­grat­u­la­tions. You're done!