Run script once on door sensor

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
jon205
Posts: 39
Joined: Monday 05 November 2018 16:17
Target OS: Linux
Domoticz version: 2022.2
Location: @home
Contact:

Run script once on door sensor

Post 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.
SweetPants

Re: Run script once on door sensor

Post 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
jon205
Posts: 39
Joined: Monday 05 November 2018 16:17
Target OS: Linux
Domoticz version: 2022.2
Location: @home
Contact:

Re: Run script once on door sensor

Post 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.
jon205
Posts: 39
Joined: Monday 05 November 2018 16:17
Target OS: Linux
Domoticz version: 2022.2
Location: @home
Contact:

Re: Run script once on door sensor

Post 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.
Last edited by jon205 on Wednesday 23 January 2019 19:57, edited 1 time in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Run script once on door sensor

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
jon205
Posts: 39
Joined: Monday 05 November 2018 16:17
Target OS: Linux
Domoticz version: 2022.2
Location: @home
Contact:

Re: Run script once on door sensor

Post by jon205 »

I'll try this when spare time > 0 :-)
Thanks for sharing.

Jon.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest