Hi. I’m hoping for some help from someone far cleverer than me. My local folder has 10 photos in it photo1.jpg, photo2.jpg…photo10.jpg. When an automation is triggered I’d like it to copy a random one of the photos to a file called screensaver.jpg. For background I intend to use this as the screensaver for a tablet that I have running the Wallpanel app. My experience is generally using the GUI for automations so I really haven’t got a clue how to start
Here is a template for making up a random photo filename:
{% set r = range(1, 11) | list | random %}
{{ "photo" ~ r ~ ".jpg" }}
If you use this in your automation, every time it triggers you will get a different filename.
Now you can make a shell command to copy the file, e.g. (depending on the paths you need):
copy_photo: "cp /home/homeassistant/.homeassistant/www/{{filename}} /home/homeassistant/.homeassistant/www/screensaver.jpg"
Putting this together to trigger the rotation every 5 minutes using a time_pattern trigger:
- alias: "Rotate photo"
trigger:
platform: time_pattern
minutes: "/5"
action:
service: shell_command.copy_photo
data:
filename: >
{% set r = range(1, 11) | list | random %}
{{ "photo" ~ r ~ ".jpg" }}
This is all untested.
Hi,
Thanks for the reply. I’m always impressed by how easily people can produce code like this. Unfortunately I don’t think this is quite I’m after so I probably didn’t explain it well enough…I don’t want the file to have a random name, I want it to always be called screensaver.jpg but I want home assistant to pick 1 of 10 random photo files to have that name. So essentially pick 1 of photo1.jpg, photo2.jpg, photo3.jpg…photo10.jpg to be copied to screensaver.jpg overwriting whatever photo was that file before. The trigger is perfect for me to have a 5 minute cycle though thanks.
That’s exactly what this does: It takes a random photo*.jpg
file and copies it to screensaver.jpg
. That is what the shell command does. The rest is to determine which random photo to pick, on an interval.
This is how to read the action
of the automation: filename
is a parameter for the shell command. The value of filename
will be a random photo*.jpg
value. In the shell command, filename
is the source from which the copy will be made.
Apologies, I should have read the first part properly rather than skipping to the automation but I was trying to get the kids to bed! I’ll have a go this evening and see how I get on. Thanks so much for your patience.
All good. Let me know how it goes and I’ll try to help fix anything that doesn’t work (because I didn’t test what I wrote, but it is snippets from a similar idea in my own config, so the parts separately do work).
Proof that not all heroes wear capes…worked perfectly once I recognised that my file path is different to yours because I’m on home assistant OS and I’m assuming you’re either in docker or a VE. Thanks again for your help.
Haha, great! Yup, I’m running core in a venv.