Copy and rename File using Hassio

HI
Hoping someone can advise if this can be done and if so how
I have folder /local/share/camera which stores my motion detected videos from my camera’s and notifies me of new file all works well.
I would like to have a method (that will continue to work with updates ect) that would allow me grab the latest file from the folder above , then copy and rename it to /config/www/camera/latest.mp4 then to show with camera.local_file in the UI (showing file is all good from with www but not outside).
I could change my setup to have the camera folder with www but that would get backed up with snapshots which I don’t want

Thanks

Use the shell command service. Then schedule it with an automation. https://www.home-assistant.io/components/shell_command/

Thanks have not used that before any hints what the shell command would like

something like

mv $(ls -t1 /local/share/camera/*.mp4|head -n1) /config/www/camera/latest.mp4

ls -t1 /local/share/camera/*.mp4 #lists all the files with extension mp4 in the /usr/localshare/camera directory in date order (latest at top)

head -n1 # takes just the first line of that listing

wrapping that in $(  ) writes that result as part of tha command line.

mv # moves the file to your desireld destination.

Please test carefully, mv is destructive. Perhaps use cp while testing, as a copy is non destructive.

Thanks will do some testing

Tested via ssh command line and works well
But when i add the shell command and test it using services nothing appears to happen. So go to look at logs and HA locks up ??
any ideas, what am i missing

shell_command:
  copy_latestmotion: cp $(ls -t1 /share/camera/*.mp4|head -n1) /config/www/camera/latest.mp4

OK a couple of ideas (and sorry I caused HA to lock up!)

  1. HA does not have access to one of the directories? You may need to whitelist one or both of them

  2. Quotes " " or ’ ’ around the whole command.

Thanks will test and all good I appreciate the help :grinning:
It is white listed as all works from commas line and I can record from the camera to either directory
But will try quotes

@Kelvin_Sudlow sorry for bumping an old thread, but did you ever find a viable solution for this? I’m trying to do something similar - but have been unsuccessful thus far.

Yes used this as a command line switch

copy_latest:
        command_on: 'cp $(ls -t1 /share/camera/*.mp4|head -n1) /config/www/camera/latest.mp4'
1 Like

Thanks. This worked for me. Using the SSH addon to test helped.
(Home Assistant Community Add-on: SSH & Web Terminal) . I had to fiddle with directory paths for it to work…

Edit: More details added.

In config file:

shell_command:
  copy_latest: 'cp $(ls -t1 /share/motioneye/outside_digoo_cam/timelapse_vids/*.mpg|head -n1) /config/www/motioneye_files/latest_timelapse.mpg'

Then you’ll see shell_command.copy_latest (etc.) appear in the services tool which you can use for testing.

This can be triggered by an automation or script service call:

#Timelapse:
- id: '5455454245'
  alias: Auto get timelapse
  initial_state: 'on'
  trigger:
    platform: time_pattern
    # Matches every hour at 10 minutes past whole
    minutes: 10
  action:
    - service: shell_command.copy_latest
4 Likes

Interesting! How to you execute this and where did you put your code?
Ideally I would like to trigger this when my camera detects motion, upload it via FTP to the /share folder and then x seconds later I would like to move it to the /www folder and let me view the clip in Lovelace.

1 Like

I’ve added some details to my previous post. Cheers. I think what you want to do should be achieveable.

2 Likes

sorry for bumping an old thread like this, but can i copy or move that file into another HomeAssistant with ssh? or maybe there is another way to do it?

Sure, shouldn’t be a problem, scp is the answer you want :slight_smile:

works on the same infrastructure as ssh (secure shell) but is scp (secure copy)

alrght thanks then i will learn about scp cause i need to make automation VSC backup from my client Homeassistant into my server home assistant. but can u give me a little explain abt SCP or a little example maybe to use scp for copy file from homeassistant to another?