Domoticz check status timers and stop it when time has expired

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
User avatar
ILoveIOT
Posts: 59
Joined: Sunday 25 March 2018 17:47
Target OS: Linux
Domoticz version: 2020.2
Location: NL
Contact:

Domoticz check status timers and stop it when time has expired

Post by ILoveIOT »

When a Domoticz server restart, or just process restart (by logrotate), the (dummy switches) timers are lost, this script will check the status en end the timer(s) that has expired.

It happens for me, the bedroom tv stays on because the timer did not end, also had to manual end the timer of the tablet on the wall, from time to time, so I made this script as a backup for Domoticz for critical timers that should end.

Maybe it would be nice to have this built-in Domoticz, as a check, when a server restarts, or process, it checks on startup what timers are still valid, maybe retime the switch, based on last update and seconds filled in the switch and status (on), so you get a restored version of Domoticz, when a server restarted, process etc.

I like to use these (dummy switches) timers allot, because you can reset them with a new period of time, or switch them off, where there, the blocky timers have no control at all, only the timer itself.

Have fun !! ;)

filename : domoticz.timers.status.sh

Code: Select all

#!/bin/bash
# domoticz.timers.status.sh
#
# Domoticz check status timers (dummy switch with buit-in timer) and stop it when "Switch-off delay" time has expires.
#
# When a Domoticz server restart, or just process restart (by logrotate), the (dummy switches) timers are lost, this script will check the status en end the timer(s) that has expires.
#
# 1. Make a dummy switch inside Domoticz and set the "Switch-off delay" in seconds, for example 3600 (1 hour)
# 2. Change the IP address "DOMOTICZIP="127.0.0.1"" now set to the same machine
# 3. Set the timer(s) IDX "IDXTOCHECK=`echo -e "146:150:151:152:153:166:177:200"`"
# 4. Run this script every 10-30-60 minutes
#
#
# By ILoveIOT 2019
#


# Domoticz IP address
DOMOTICZIP="127.0.0.1"

# Domoticz IDX(s) to check use : for separate, if u use only 1 idx, use 200: or 100:
#IDXTOCHECK=`echo -e "200:"`
IDXTOCHECK=`echo -e "146:150:151:152:153:166:173:177:200"`


#########################################################################################
#########################################################################################

CDATE=`/bin/date`

# Do the loop for "IDXTOCHECK"
c=1
temps=`echo "$IDXTOCHECK" | /usr/bin/cut -f $c -d ":"`
while [ -n "$temps" ]; do

	STATUSOFTIMER=`/usr/bin/curl -s 'http://'$DOMOTICZIP':8080/json.htm?type=devices&rid='$temps'' | /bin/grep Status | /usr/bin/awk '{ print $3 }' | /bin/sed 's/,/ /g' | /usr/bin/tr -d '"'`
	if [ $STATUSOFTIMER = "On" ] ; then		
		
		# Get the last switch update from Domoticz
		LASTUPDATETIME=`/usr/bin/curl -s 'http://'$DOMOTICZIP':8080/json.htm?type=devices&rid='$temps'' | /bin/grep LastUpdate | /usr/bin/awk '{ print $4 }' | /bin/sed 's/,/ /g' | /usr/bin/tr -d '"'`
		LASTUPDATEDATE=`/usr/bin/curl -s 'http://'$DOMOTICZIP':8080/json.htm?type=devices&rid='$temps'' | /bin/grep LastUpdate | /usr/bin/awk '{ print $3 }' | /usr/bin/tr -d '"' | /usr/bin/cut -c6-10 | /usr/bin/tr - /`
		# Get the Switch-off delay seconds from the switch, from Domoticz
		TIMERVALUEINSECONDS=`/usr/bin/curl -s 'http://'$DOMOTICZIP':8080/json.htm?type=devices&rid='$temps'' | /bin/grep "AddjValue" | /usr/bin/awk '{ print $3 }' | head -n 1 | /bin/sed 's/.0,/ /g'`
		# Calculate the end date
		ENDDATE=`/bin/date -d "$LASTUPDATEDATE $LASTUPDATETIME $TIMERVALUEINSECONDS sec" "+%m/%d %H:%M:%S"`
		# Get the current date for compare
		CURRENTDATE=`/bin/date "+%m/%d %H:%M:%S"`
		
		# Convert the end and current time for compare
		ENDDATEINSEC=`/bin/date -d "$ENDDATE" '+%s'`
		CURRENTDATEINSEC=`/bin/date -d "$CURRENTDATE" '+%s'`
			
		# If the timer has expired switch it off in Domoticz.
		if [ $CURRENTDATEINSEC \> $ENDDATEINSEC ]; then
			/usr/bin/curl -i -s "http://$DOMOTICZIP:8080/json.htm?type=command&param=switchlight&idx=$temps&switchcmd=Off" >> /dev/null 2>&1
			
			# Some mail output for debug when console not active
			# Get the IDX name just for debuging, so you see the name of the IDX on console
			IDXNAME=`/usr/bin/curl -s 'http://'$DOMOTICZIP':8080/json.htm?type=devices&rid='$temps'' | /bin/grep Name | /bin/sed -n '2p' | /usr/bin/awk '{print $3,$4,$5,$6,$7,$8}' | /usr/bin/tr -d '"' | /usr/bin/tr -d ','`
			echo ""
			echo "IDX                 : $temps"
			echo "IDXNAME             : $IDXNAME"
			echo "STATUSOFTIMER       : $STATUSOFTIMER"
			echo "TIMERVALUEINSECONDS : $TIMERVALUEINSECONDS"
			echo "LASTUPDATE          : $LASTUPDATEDATE $LASTUPDATE"
			echo "ENDDATE             : $ENDDATE"
			echo "CURRENTDATE         : $CURRENTDATE"
			echo "ENDDATEINSEC        : $ENDDATEINSEC"
			echo "CURRENTDATEINSEC    : $CURRENTDATEINSEC"		
			echo ""
		
		fi
		
		# Some console output for debug
		# Get the IDX name just for debuging, so you see the name of the IDX on console
		#IDXNAME=`/usr/bin/curl -s 'http://'$DOMOTICZIP':8080/json.htm?type=devices&rid='$temps'' | /bin/grep Name | /bin/sed -n '2p' | /usr/bin/awk '{print $3,$4,$5,$6,$7,$8}' | /usr/bin/tr -d '"' | /usr/bin/tr -d ','`
		#echo ""
		#echo "IDX                 : $temps"
		#echo "IDXNAME             : $IDXNAME"
		#echo "STATUSOFTIMER       : $STATUSOFTIMER"
		#echo "TIMERVALUEINSECONDS : $TIMERVALUEINSECONDS"
		#echo "LASTUPDATE          : $LASTUPDATEDATE $LASTUPDATE"
		#echo "ENDDATE             : $ENDDATE"
		#echo "CURRENTDATE         : $CURRENTDATE"
		#echo "ENDDATEINSEC        : $ENDDATEINSEC"
		#echo "CURRENTDATEINSEC    : $CURRENTDATEINSEC"		
		#echo ""

	fi

	# End the loop IDXTOCHECK
	let c=$c+1
	temps=`echo "$IDXTOCHECK" | cut -f $c -d ":"`
	done

	#echo ""
	#echo "Started  @ $CDATE" 
	#echo "Finished @ `date`" 
	#echo ""
	
	
exit


#EOF
1x OPI PC 5.8.16 Debian 10, MiniDNLA,SMD230,EpEver,MPD,800GB,Wiegand,7 temps,DHT,32/64 I/O,2022.1
2x OPI PC 4.19.25 Debian 9, MPD,7 temps,16/32 I/O,BMP,4.10717
1x IntelNUC 5.10 Debian 10, 2TB,DalyBMS,2023.1
3x ESPEasy NodeMCU V3, 16/16 I/O
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest