Mutliple events

Moderator: leecollings

Post Reply
AriePiet
Posts: 8
Joined: Saturday 30 March 2024 12:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Mutliple events

Post by AriePiet »

Hi Guys,

I am posting this here, as it's switch related.

I am having an issue where a specifi bash script is triggered to run more than once. I know why, but not how to surpress it.

I have several EspEasy devices, with all kinds of sensors and they all work flawlessly except for the one with a doorbell switch.
What happens is this, doorbell button is pushed, this triggers an event on the Esp and then send to the controller (Domoticz).
There it activates a bash script.
This script records a stream from the doorcamera, which is saved and sent to my phone.

All this works wonderfull, only thing is that when the doorbell is pressed seveerl times, the scriupt is triggered several times.
I made sure that if the script runs, it cannot run simultaneously. So it runs one after another for the amounts of the times the doorbell is pressed.

What i would like is to prevent this behaviour on the Esp or on Domoticz. For example do not run when pressed within a minute or so.

My Esp Device looks like this:

GPIO-0 (D3)
Switch Type: Switch
Switch Button Type: Normal Switch
De-bounce (ms): 250
Doubleclick event: Disabled
Doubleclick max. interval (ms): 2000
Longpress event: Disabled
Longpress min. interval (ms): 1000
Interval: 5000 [sec] (Optional for this Device)


In the Domoticz the switch is set-up like this:

Name: Deurbel
Switch Type: On/Off
On Delay: 0 (Seconds) 0 = Disabled
Off Delay: 20 (Seconds) 0 = Disabled
On Action: script:///home/pi/domoticz/scripts/VoordeurBelCam-vid.sh


Under devices:

Esp Switches 0001406C 1 Deurbel Light/Switch Switch Off

I think i am overlooking something easy and am pretty sure this should/can be solved in either the Esp or in Domoticz
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Mutliple events

Post by jvdz »

Add something like this to avoid running multiple occurrences of a shell script:

Code: Select all

cnt=`ps x | grep "VoordeurBelCam-vid.sh"|grep -cv grep`
echo $cnt
if [ $cnt -gt 1 ]
then
   echo "Process already running"
   exit
fi
--- rest of your script
check what the proper value of $cnt has to be in the script and change the test value 1 when needed.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
AriePiet
Posts: 8
Joined: Saturday 30 March 2024 12:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Mutliple events

Post by AriePiet »

Hi jvdz,

In the script i already prevent it from running more then once, that's not the problem.

The problem is that, somewhere (Domoticz, Espeasy) each press is remembered and handled after the first press and thus the script starts again.
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Mutliple events

Post by jvdz »

Ah ok ... so it is started by an event. Then you need to make it so the event script ( a separate new one) launches the recording script and then end immediately. That way yhe events are handled right away and any second run is stopped by yhe above logic.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
AriePiet
Posts: 8
Joined: Saturday 30 March 2024 12:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Mutliple events

Post by AriePiet »

First thanks for your quick replies!

Not sure why this would work, but worth a try.

I have created a new script, which is started by the Domo switch event:

cnt=`ps x | grep "Pre-VoordeurBel.sh"|grep -cv grep`
echo $cnt
if [ $cnt -gt 1 ]
then
echo "Process already running"
exit
fi
--- rest of your script
./VoordeurBelCam-vid.sh


This what you meant?

Gonna test it asap.

Care to elaborate? Why would this new script not run multiple times, just like the original?
To me, it looks like Domo would just wait until the script ends and then says go ahead, another one.
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Mutliple events

Post by jvdz »

Yes but you need a & at the end of the line or else the script will wait for VoordeurBelCam-vid.sh to finish! :)

So the line need to be
./VoordeurBelCam-vid.sh &

And the check for the VoordeurBelCam-vid.sh in stead of the pre.....
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
AriePiet
Posts: 8
Joined: Saturday 30 March 2024 12:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Mutliple events

Post by AriePiet »

So, like this:

cnt=`ps x | grep "VoordeurBelCam-vid.sh"|grep -cv grep`
echo $cnt
if [ $cnt -gt 1 ]
then
echo "Process already running"
exit
fi
--- rest of your script
./VoordeurBelCam-vid.sh &


Although this looks very promising and i'll definately will use it, wouldn't it be better to prevent this behaviour in Domo or already at the Esp?
User avatar
habahabahaba
Posts: 192
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: Mutliple events

Post by habahabahaba »

try to use <LastUpdate> property in Dzvents script

if <YourDeviceStatus>.lastUpdate.secondsAgo > 60 then

-- your actions

end
AriePiet
Posts: 8
Joined: Saturday 30 March 2024 12:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Mutliple events

Post by AriePiet »

Thanks Haba,

I will look into this also, not really played with Dzevents before.
AriePiet
Posts: 8
Joined: Saturday 30 March 2024 12:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Mutliple events

Post by AriePiet »

AriePiet wrote: Sunday 31 March 2024 14:18 So, like this:

cnt=`ps x | grep "VoordeurBelCam-vid.sh"|grep -cv grep`
echo $cnt
if [ $cnt -gt 1 ]
then
echo "Process already running"
exit
fi
--- rest of your script
./VoordeurBelCam-vid.sh &


Although this looks very promising and i'll definately will use it, wouldn't it be better to prevent this behaviour in Domo or already at the Esp?
I have it running the way you describe it and your logic works, but it does not help me with my problem, which is still there :(
User avatar
habahabahaba
Posts: 192
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: Mutliple events

Post by habahabahaba »

And where did you place the path to your script?

Try to put it on "Notifications"
2024-04-02_21-14-03.png
2024-04-02_21-14-03.png (7.74 KiB) Viewed 1758 times

To the "Action" field and there is Ignore interval slider

2024-04-02_21-14-52.png
2024-04-02_21-14-52.png (41.29 KiB) Viewed 1758 times

Which can be set it Setup/Settings/Notifications

2024-04-02_21-13-15.png
2024-04-02_21-13-15.png (52.55 KiB) Viewed 1758 times
AriePiet
Posts: 8
Joined: Saturday 30 March 2024 12:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Mutliple events

Post by AriePiet »

@haba

Tears of joy and happiness here, it looks like it's working!

Not sure about you example, but i had to uncheck the 'Ignore Interval' option:
Schermafbeelding 2024-04-03 201233.png
Schermafbeelding 2024-04-03 201233.png (62.46 KiB) Viewed 1735 times
User avatar
habahabahaba
Posts: 192
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: Mutliple events

Post by habahabahaba »

Great :)

Could you show the 'VoordeurBelCam-vid.sh' code?

May be i need it too :)
AriePiet
Posts: 8
Joined: Saturday 30 March 2024 12:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Mutliple events

Post by AriePiet »

Hi Haba,

Sorry for the late response.
Here you have the basic for the script:

#!/bin/bash
# VoordeurBelCam-vid.sh
# ==============
# |Author: JvdB|
# ==============
# Take video with the SmartWares IP WiFi camera..!
# With this script you save a live video from any camera using rtsp, mine is a SmartWares
#
# Script is triggered by a sensor (Doorbell), variables on when to run are not covered in this script (for that i use Domoticz).
# Capture program is 'FFMPEG', installation/set-up is not covered in this script.
# Communication to/with Telegram is done with 'Curl', installation/set-up is not covered in this script.
#
# To prevent this script from running more instances at the same time and thus probably slow down you pi (or server), we make sure it only runs once
# We do this by creating a PID file, if one exist when the script starts it won't run. Besides that if an abandoned PID file is found, but there's no corresponding process,
# the existing PID file is removed and a new one created and the script will run normally.
#
# ============================================
# Redirect stdout and stderr to debug log file
# ============================================
#exec &>> /home/pi/domoticz/scripts/debug.log
#
# =======================================================
# Start PID file creation and check for running instances
# =======================================================
PIDFILE=/home/pi/domoticz/scripts/VoordeurBelCam-vid.pid
#
# ----- script running? -----
[[ -s $PIDFILE ]] && exit
#
# no, create pidfile
echo $BASHPID > $PIDFILE
#
# =================
# En PID file check
# =================
# Start Variables
# =================
# currenttime - Check the current time to let the script run or not
# DATE - Datetime stamp, i always start with this variable, otherwise you might encounter problems in your script, if the path is changed
# IP - the ip address of your camera
# PORT - the port on which the camera operates (SmartWares is default 80, that's why i don't use it in this script)
# Rec_Time - duration of recording in seconds
# PATH - where the recording will be saved
# FILENAME - Name of the file/recording - Datetime is added! (format/extension for me is always mp4)
# Telegram_bot_id - Telegram bot id, bot from who you'll receive notifications
# Telegram_chat_id - Telegram chat id, this can be a personal or a group chat id. Howto set-up is not covered in this script!
# Ret_days - Amount of days to save previous recordings, play around with this
#
currenttime=$(date +%H:%M)
DATE=`date '+%Y-%m-%d_%H-%M'`
IP=123.123.123.123
# PORT=8080
Rec_Time=180
#Rec_Time=30
PATH='/DATA/media/videos/voordeur'
FILENAME="Voordeur-$DATE.mp4"
Telegram_bot_id=botname:"123...some string"
Telegram_chat_id="You TG id"
Message='Deurbel..!'
Ret_days=7
#
# ================
# End variables
# ================
# Start time check
# ================
#
if [[ "$currenttime" > "00:00" ]] || [[ "$currenttime" < "00:00" ]]
then
#
# ===============
# End time check
# ===============
# Start recording
# ===============
#
/usr/bin/ffmpeg -i rtsp://"loginname":"password"@$IP/live/av0? -vcodec copy -t $Rec_Time -y $PATH/$FILENAME;
#
# ======================
# End Recording
# ======================
# Start Telegram message
# ======================
#
/usr/bin/curl -s -X POST "https://api.telegram.org/$Telegram_bot_ ... t=$Message"
#
# Send Telegram video (optional, please consider time/length of recording), uncomment it if you want to sent it
# -------------------------------------------------------------------------------------------------------------
/usr/bin/curl -s -X POST "https://api.telegram.org/$Telegram_bot_id/sendVideo" -F chat_id=$Telegram_chat_id -F video="@$PATH/$FILENAME"
#
# ===================
# End Telgram message
# ===================
# Start Clean-up
# Remove recording(s) (To prevent you harddisk from filling up) and to remove the PID file.
# =========================================================================================
#
/usr/bin/find $PATH -type f -name '*.mp4' -mtime +$Ret_days -exec /bin/rm {} \;
/bin/rm /home/pi/domoticz/scripts/VoordeurBelCam-vid.pid
#
# =============
# End Clean-up
# =============
#
exit 0
else
#
# Remove the PID file for the second exit (when the script did not run, because of time window).
# ==============================================================================================
#
/bin/rm /home/pi/domoticz/scripts/VoordeurBelCam-vid.pid
exit 0
fi


In the above i use a generic RTS stream from a capable camera and i use Telegram to sent the info/video to my phone.
The files are stored for 7 days
And the time option is there, just in case you want to only record at given times or as in my case you also use this script for movement (other caerma in backyard) at certain times during the night.
User avatar
habahabahaba
Posts: 192
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: Mutliple events

Post by habahabahaba »

Yes, thank you.

But ffmpeg uses too much resourses of OS.
So, i will stay on taking pictures and sending them :)
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests