Record Vidéo from ESP32_Cam (config with EspHome) in hassio works great!

Hello,

i try to record a stream from a camera in hassio, i have follow a post : https://community.home-assistant.io/t/ip-camera-snapshot-automation/41319/14

this post is for snapshot pictures, and i try to adapt the shell_command from another post : https://community.home-assistant.io/t/not-working-to-do-snapshot-or-recording-using-esp32-cam/125360

because the token change often i read to replace the token by :

{{ state_attr('camera.esp32_camera','access_token') }}

here is my shell file :

#!/bin/bash
date

folder=/config/www/cam_record
id=$(date +"%y-%m-%d_%H-%M-%S")cuisine_camera
http_url=https://192.168.1.50:8123/api/camera_proxy_stream/camera.cuisine?token={{ State_attr('camera.cuisine','access_token') }}

mkdir $folder
ffmpeg -i $http_url -t 1 -vcodec copy $folder/$id.mp4
ls -lh $folder/$id.mp4 $folder/cuisine_camera.mp4
cp $folder/$id.mp4 $folder/cuisine_camera.mp4
ls -lh $folder/$id.mp4 $folder/cuisine_camera.mp4
find $folder -type f -name ‘*.mp4’ -mtime +30 -exec rm {} ;

i get these errors in the shell log file:

+ $'date\r'
/config/shell_scripts/cuisine_camera_shell_script.sh: line 2: $'date\r': command not found
+ $'\r'
/config/shell_scripts/cuisine_camera_shell_script.sh: line 3: $'\r': command not found
+ folder=$'/config/www/cam_record\r'
++ date +%y-%m-%d_%H-%M-%S
+ id=$'19-12-07_18-24-07cuisine_camera\r'
/config/shell_scripts/cuisine_camera_shell_script.sh: line 6: syntax error near unexpected token `('
/config/shell_scripts/cuisine_camera_shell_script.sh: line 6: `http_url=https://192.168.1.50:8123/api/camera_proxy_stream/camera.cuisine?token={{ state_attr('camera.cuisine','access_token') }}

sorry i try to use notepad++ and find where is the errors…but i am not familiar to shell command and shell files…

If someone could help please?

Thanks

2 Likes

It is not possible to use jinja expression in a shell script. Removing {{ State_attr('camera.cuisine','access_token') }} should fix the problem.

thanks for your help, i know its the problem,

i want to pass a variable “cam_token” from script, like :

service: shell_command.cuisine_camera_shell_script
   data_template:
        cam_token: '{{ state_attr("camera.cuisine","access_token") }}'

my shell_command is :

shell_command:
  cuisine_camera_shell_script: bash -x /config/shell_scripts/cuisine_camera_shell_script.sh {{cam_token}} >> /config/shell_scripts/cuisine_camera_shell_script.log 2>&1

but i dont know how to do in the shell file to use the variable “cam_token”?

can you help please?

this is what i try :

#!/bin/bash
date

folder=/config/www/cam_record
id=$(date +"%y-%m-%d_%H-%M-%S")cuisine_camera
http_url=https://192.168.1.50:8123/api/camera_proxy_stream/camera.cuisine?token=$cam_token

mkdir $folder
ffmpeg -i $http_url -t 1 -vcodec copy $folder/$id.mp4
ls -lh $folder/$id.mp4 $folder/cuisine_camera.mp4
cp $folder/$id.mp4 $folder/cuisine_camera.mp4
ls -lh $folder/$id.mp4 $folder/cuisine_camera.mp4
find $folder -type f -name ‘*.mp4’ -mtime +30 -exec rm {} ;

i dont know how used the “cam_token” variable to put in the http adress…

thanks if you know…

Use a Positional Parameter

In your case just replace

http_url=https://192.168.1.50:8123/api/camera_proxy_stream/camera.cuisine?token=$cam_token

with

http_url=https://192.168.1.50:8123/api/camera_proxy_stream/camera.cuisine?token=$1

ok thanks!!!

but how is 1 is defined to “cam_token”, its automatic assignement to 1 from cam_token, nothing to declare in the shell file?

sorry i am a noob…

sorry but it doesnt work, i still have nothing after token= in the https adress, so i have an error 401 : unauthorized…because no token provide.

Please, may i have to declare something in the shell file? like 1=cam_token ?

Strange.
You are already redirecting the output - right?
Check the token you are passing to the script with something like:

echo "token is: $1"

Then execute your command by hand, with the token that you will get from the echo command above in order to verify the token. A wrong token is all I can think of.

And yes you can assign the token to a variable (but it should not make any difference) like so:

token=$1

sorry, what do you mean by “You are already redirecting the output”?

Edit your script to start with these lines:

#!/bin/bash
date
echo "token is: $1"

Execute the shell command from Home Assistant.
Then in order to verify that the token is valid check the log file: /config/shell_scripts/cuisine_camera_shell_script.log

the log is empty, nothing in

the shell_command :

shell_command:
  cuisine_camera_shell_script: bash -x /config/shell_scripts/cuisine_camera_shell_script.sh **{{cam_token}} >>** /config/shell_scripts/cuisine_camera_shell_script.log 2>&1

doesnt work together {{ }} and >>, i read that in a post https://community.home-assistant.io/t/pass-arguments-to-shell-command-on-event/37029/4?u=olivier974

so i just keep :

shell_command:
  cuisine_camera_shell_script: bash -x /config/shell_scripts/cuisine_camera_shell_script.sh {{cam_token}}

and since that i have nothing in the log…and i dont know how make a second line to have the log…

And i have this error still the beginning :

Error running command: `/bin/bash /config/shell_scripts/cuisine_camera_shell_script.sh {{cam_token}}`, return code: 1

i google it but dont find anything…

i have execute the script by double click it in samba share and the echo is appeared but no token provided

Then replace:

#!/bin/bash
date
echo "token is: $1"

with

#!/bin/bash
exec &>>/config/shell_scripts/cuisine_camera_shell_script.log

date
echo "token is: $1"

Execute the script by hand - do not forget to add a token after the file name.
Does it work, is there anything in /config/shell_scripts/cuisine_camera_shell_script.log ? If it isn’t working then you need to fix the script.

If it works try from Home Assistant.

thanks

if i execute the script by hand with power shell windows, it works, i can see the token…fast because the windows open and close very fast…

But no .log create…

if i triggered manualy the automation in hassio, the log is create with no token…

an idea?

I am just guessing - put the command in a double quotation marks:

shell_command:
  cuisine_camera_shell_script: "bash /config/shell_scripts/cuisine_camera_shell_script.sh {{cam_token}}"

thanks again for your help!

it doesnt change anything…

this is my .sh :

#!/bin/bash
exec &>>/config/shell_scripts/cuisine_camera_shell_script.log

date
echo "token is: $1"
cam_token=$1


folder=/config/www/cam_record
id=$(date +"%y-%m-%d_%H-%M-%S")cuisine_camera
http_url=https://192.168.1.50:8123/api/camera_proxy_stream/camera.cuisine?token=$1

mkdir $folder
ffmpeg -i $http_url -t 1 -vcodec copy $folder/$id.mp4
ls -lh $folder/$id.mp4 $folder/cuisine_camera.mp4
cp $folder/$id.mp4 $folder/cuisine_camera.mp4
ls -lh $folder/$id.mp4 $folder/cuisine_camera.mp4
find $folder -type f -name ‘*.mp4’ -mtime +30 -exec rm {} ;

i really dont understand why i cant passed a variable from HA, a lot of people do that…

i have replace the " {{cam_token}}" with text and it passed to the script when i triggered manualy…

like this :

shell_command:
  cuisine_camera_shell_script: bash -x /config/shell_scripts/cuisine_camera_shell_script.sh test_to_see

and i have “test_to_see” in the .log for the echo function…

So its a problem with the argument…between ha and the sript…

OK!!! i get it!!!

i have just remove the template “cam_token” in the automation and put directly :

shell_command:
  cuisine_cam_shell: '/config/shell_scripts/cuisine_cam.sh {{ state_attr("camera.cuisine","access_token") }}'

and it works!!! lol

thanks a lot for your help @neohidra

i have now little .mp4 but the format is not regognized…lol

i will search wich format is it now :frowning:

here is the log :frowning:

Sat Dec  7 23:45:55 +04 2019
token is: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4fdf663e463bb1e09d167b784b063a470776
mkdir: can't create directory '/config/www/cam_record': File exists
ffmpeg version 4.1.4 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 8.3.0 (Alpine 8.3.0)
  configuration: --prefix=/usr --enable-avresample --enable-avfilter --enable-gnutls --enable-gpl --enable-libass --enable-libmp3lame --enable-libvorbis --enable-libvpx --enable-libxvid --enable-libx264 --enable-libx265 --enable-libtheora --enable-libv4l2 --enable-postproc --enable-pic --enable-pthreads --enable-shared --enable-libxcb --disable-stripping --disable-static --disable-librtmp --enable-vaapi --enable-vdpau --enable-libopus --disable-asm --disable-debug
  libavutil      56. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100
Input #0, mpjpeg, from 'https://xxxxxxxxx:8123/api/camera_proxy_stream/camera.cuisine?token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxe463bb1e09d167b784b063a470776':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: mjpeg, yuvj422p(pc, bt470bg/unknown/unknown), 640x480, 25 tbr, 25 tbn, 25 tbc
Output #0, mp4, to '/config/www/cam_record/19-12-07_23-45-55cuisine_camera.mp4':
  Metadata:
    encoder         : Lavf58.20.100
    Stream #0:0: Video: mjpeg (mp4v / 0x7634706D), yuvj422p(pc, bt470bg/unknown/unknown), 640x480, q=2-31, 25 tbr, 12800 tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
frame=    6 fps=0.0 q=-1.0 size=       0kB time=00:00:00.20 bitrate=   1.8kbits/s speed=0.372x    
frame=   10 fps=9.2 q=-1.0 size=       0kB time=00:00:00.36 bitrate=   1.0kbits/s speed=0.331x    
frame=   14 fps=8.0 q=-1.0 size=       0kB time=00:00:00.52 bitrate=   0.7kbits/s speed=0.299x    
frame=   18 fps=7.6 q=-1.0 size=       0kB time=00:00:00.68 bitrate=   0.5kbits/s speed=0.285x    
frame=   22 fps=7.3 q=-1.0 size=       0kB time=00:00:00.84 bitrate=   0.4kbits/s speed=0.279x    
frame=   25 fps=7.0 q=-1.0 size=       0kB time=00:00:00.96 bitrate=   0.4kbits/s speed=0.268x    
frame=   25 fps=6.4 q=-1.0 Lsize=     256kB time=00:00:00.96 bitrate=2182.7kbits/s speed=0.246x    
video:255kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.340544%
ls: /config/www/cam_record/cuisine_camera.mp4: No such file or directory
-rw-r--r--    1 root     root      255.8K Dec  7 23:45 /config/www/cam_record/19-12-07_23-45-55cuisine_camera.mp4
-rw-r--r--    1 root     root      255.8K Dec  7 23:45 /config/www/cam_record/19-12-07_23-45-55cuisine_camera.mp4
-rw-r--r--    1 root     root      255.8K Dec  7 23:45 /config/www/cam_record/cuisine_camera.mp4
find: -exec requires an argument

reading the vidéo is perfect with VLC Media Player!!! yeahhhhhhhhhhhhhhh!!! :slight_smile:

Thanks anyway for your time, patience and help! your are a nice person!

2 Likes

oohhh friend I would love to work with this but I have problems with the directories and I don’t know where to start

I have the same scritp as you, and changed only the name of cam

1st question: the cuisine_camera_shell_script.sh file where it is hosted
I have it here /config/shell_scripts/cuisine_camera_shell_script.sh

my error: bash: /config/shell_scripts/camera_shell_script.log: No such file or directory

2nd shell_command: does it have to be hosted in configuration.yaml?

3rd is necessary

  homeassistant:
   whitelist_external_dirs:
     - "/ config / shell_scripts /"
     - "/ config / www / cam_record /"

thank you very much friend

Hello @Domy5,

its a pleasure that someone is interested in :wink:

-1st question: i have create a folder named “shell_scripts” under config : like that : /config/shell_scripts/cuisine_camera_shell_script.sh
and lol i understand because i create a .log file to see if it works, so this is the content of the “cuisine_camera_shell_script.sh” for example :slight_smile:

 #!/bin/bash
exec &>>/config/shell_scripts/cuisine_cam.log

date
echo "token is: $1"
cam_token=$1


folder=/config/www/cam_record
id=$(date +"%y-%m-%d_%H-%M-%S")cuisine_camera
http_url=https://192.168.xxx.xxx:8123/api/camera_proxy_stream/camera.cuisine?token=$1

mkdir $folder
ffmpeg -i $http_url -t 10 -vcodec copy $folder/$id.mp4
ls -lh $folder/$id.mp4 $folder/cuisine_camera.mp4
cp $folder/$id.mp4 $folder/cuisine_camera.mp4 ;

So you can see after triggered manualy your automation, the log will be create and inside its normally looks like mine in my previous message.

-2nd shell_command: does it have to be hosted in configuration.yaml? : you can but i create a file “shell_command.yaml” to put all my shell commands…like :

cuisine_cam_shell: '/config/shell_scripts/cuisine_cam.sh {{ state_attr("camera.cuisine","access_token") }}'

if you do, dont forget to put in configuration.yaml : “shell_command: !include shell_command.yaml” :wink:

You can do whithout and just put in configuration.yaml :

shell_command:
  cuisine_camera_shell_script: "bash /config/shell_scripts/cuisine_camera_shell_script.sh {{cam_token}}"

-3rd is necessary : no, i havent that in my config…

Last thing : its the automation : you have to triggered with something, like a motion sensor. I have one attached on the camera (you can see here my thingverse : [https://www.thingiverse.com/thing:3816458] (https://www.thingiverse.com/thing:3816458) ).

My automation is :

- id: 'xxxxxxxxxxxxxxxx'
  alias: Record cam detecteur cuisine
  trigger:
  - entity_id: binary_sensor.cuisine_pir_sensor
    for: 00:00:02
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: binary_sensor.somebody_home
    state: 'off'
  action:
  - data:
      entity_id: camera.cuisine
      filename: /config/www/cam_captures/cuisine_{{ now ().year }}_{{ now ().month
        }}_{{ now ().day }}_{{ now ().hour }}_{{ now ().minute }}.jpg
    service: camera.snapshot
  - delay: 00:00:01
  - data_template:
      data:
        force_image: true
        image: https://XXXXXX.duckdns.org/local/cam_captures/cuisine_{{ now ().year
          }}_{{ now ().month }}_{{ now ().day }}_{{ now ().hour }}_{{ now ().minute
          }}.jpg
      message: Détection de mouvement cuisine
      title: Alerte cuisine
    service: notify.mobile_app_galaxy_note10
  - data_template:
      data:
        images:
        - /config/www/cam_captures/cuisine_{{ now ().year }}_{{ now ().month }}_{{
          now ().day }}_{{ now ().hour }}_{{ now ().minute }}.jpg
      message: Capteur de mouvements Cuisine déclenché
      title: '!!! Hassio Detection Cuisine !!!'
    service: notify.email_notify
  - service: shell_command.cuisine_cam_shell

Now you have all what you need :wink:

Enjoy! give me some feedback if its ok for you please :wink:

1 Like

Thank you very much, how quickly you answer, it is appreciated
this does not works:

after configuration.yaml:“ shell_command:! include shell_command.yaml ”
and
shell_command.yaml

cam_shell: '/config/shell_scripts/camara.sh {{state_attr ("camera.puerta_esp32cam", "access_token")}}'

This worked for me:

shell_command:
  cam_shell: 'bash /config/shell_scripts/camara.sh {{state_attr ("camera.puerta_esp32cam", "access_token")}}' 

but it makes a file with a different name than “camera_shell_script.log”
name like this “CY4X7X ~ 8”

and inside the log

mkdir: can't create directory '/ config / www / cam_record

': File exists
ffmpeg version 4.1.4 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 8.3.0 (Alpine 8.3.0)
  configuration: --prefix = / usr --enable-avresample --enable-avfilter --enable-gnutls --enable-gpl --enable-libass --enable-libmp3lame --enable-libvorbis --enable-libvpx - enable-libxvid --enable-libx264 --enable-libx265 --enable-libtheora --enable-libv4l2 --enable-postproc --enable-pic --enable-pthreads --enable-shared --enable-libxcb - disable-stripping --disable-static --disable-librtmp --enable-vaapi --enable-vdpau --enable-libopus --disable-asm --disable-debug
  libavutil 56. 22.100 / 56. 22.100
  libavcodec 58. 35.100 / 58. 35.100
  libavformat 58. 20.100 / 58. 20.100
  libavdevice 58. 5,100 / 58. 5,100
  libavfilter 7. 40.101 / 7. 40.101
  libavresample 4. 0. 0 / 4. 0. 0
  libswscale 5. 3.100 / 5. 3.100
  libswresample 3. 3.100 / 3. 3.100
  libpostproc 55. 3,100 / 55. 3,100
[https @ 0x753beea0] HTTP error 400 Bad Request
https: //*****.duckdns.org/api/camera_proxy_stream/camera.puerta_esp32cam? token = 7 ************************** ************ 33c4013f1df67e184f1d
: Server returned 400 Bad Request
ls: / config / www / cam_record
/ 20-01-31_16-12-49cuisine_camera
.mp4: No such file or directory
ls: / config / www / cam_record
/cuisine_camera.mp4
: No such file or directory
cp: can't stat '/ config / www / cam_record
/ 20-01-31_16-12-49cuisine_camera
.mp4 ': No such file or directory
ls: / config / www / cam_record
/ 20-01-31_16-12-49cuisine_camera
.mp4: No such file or directory
ls: / config / www / cam_record
/cuisine_camera.mp4
: No such file or directory
find: -exec requires an argument
/config/shell_scripts/camara.sh: line 15: $ '\ r': command not found

automation is not a problem I have an automation with a switch-button that sounds a buzz like a doorbell and in turn sends a photo and this would be to send a small video (for now to keep it in a folder) all with the same esp32-cam

any ideas again thank you very much