Page 1 of 1
Run script once on door sensor
Posted: Thursday 03 January 2019 17:09
by jon205
I have a script that sends a snapshot of my IP-camera to my phone through telegram. I've placed a door sensor in my letterbox and linked the script to the sensor in Domoticz. This works fine, but... The script should only run once when the letterbox is being opened. In stead of donig that, it starts the script again, the entire duration of the 'door' being open. So I get like 3 or 4 pictures on every 'opening'. What can I do to let it run the script only once?
Thanks, Jon.
Re: Run script once on door sensor
Posted: Thursday 03 January 2019 18:07
by SweetPants
First of all, publish your script (between code tags). It's a wild guess what is happening now and what to do to prevent this behaviour
Re: Run script once on door sensor
Posted: Thursday 03 January 2019 18:13
by jon205
I use this script for my doorbell as well. When the doorbell is being rung, it runs this script once. I think it's not the script, but the script is being triggered more than once.
I'll have to look up the script when I'm at home.
Jon.
Re: Run script once on door sensor
Posted: Thursday 03 January 2019 18:18
by jon205
Code: Select all
SnapFile="web.jpg"
# Get snapshot via Domoticz server
sudo wget $SnapFile "192.168.*.*/cgi-bin/getsnapshot.cgi"
sudo cp getsnapshot.cgi web.jpg
# Send Telegram message with image
curl -s -X POST "https://api.telegram.org/bot***:AAH-6AvLY2HnubxTujFVs**/sendMessage?chat_id=***/sendMessage?chat_id=***&text=Post!"
curl -s -X POST "https://api.telegram.org/bot****:AAH-6AvLY2HnubxTujFVsS**/sendPhoto" -F chat_id=*** -F photo="@$SnapFile"
# Remove Image
sudo rm $SnapFile
sudo rm getsnapshot.cgi
This is it.
Jon.
Re: Run script once on door sensor
Posted: Thursday 03 January 2019 18:25
by waaren
Assuming your script is triggered multiple times because domoticz receives multiple open signals you can prevent the snapshots from being send shortly after each other by pasting the script-snippet below, before the body of your script.
Code: Select all
#!/bin/bash
minimumTimeBetweenSnapshots=30 # seconds
thisFile="$( cd "$(dirname "$0")" ; pwd -P )"/${0##*/}
thisFileWithoutExtension=${thisFile%.*}
lastTimeStartedLog=$thisFileWithoutExtension"_lastTimeStarted.data"
#-- begin debugLines
echo thisFile : $thisFile
echo thisFileWithoutExtension: $thisFileWithoutExtension
echo lastTimeStartedLog : $lastTimeStartedLog
#-- end debugLines
now=$(date "+%s")
if [ -f $lastTimeStartedLog ] ; then
lastTime=$(cat $lastTimeStartedLog)
secondsAgo=$(($now - $lastTime))
# echo $secondsAgo
if [[ $secondsAgo -lt $minimumTimeBetweenSnapshots ]] ; then
# Too soon after previous execution exiting now
exit 1
fi
fi
echo $now > $lastTimeStartedLog
#
# Body of your script to follow below
#
#eoj
Re: Run script once on door sensor
Posted: Monday 07 January 2019 20:43
by jon205
I'll try this when spare time > 0

Thanks for sharing.
Jon.