Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Domoticz Server Information
- USERNAME=<domoticz username>
- PASSWORD=<domoticz password>
- HOSTNAME=<domoticz server fqdn or ip>
- HOSTPORT=<domoticz server listening port>
- HOSTPROTOCOL=<domoticz server protocol (http/https)>
- SWITCHIDX=<idx of the rain expected switch>
- PREDICTIONIDX=<idx of the rain forecast mm/h custom sensor>
- # Buienradar Information
- LAT=<your latitude>
- LON=<your longitude>
- URL="https://gadgets.buienradar.nl/data/raintext?lat=${LAT}&lon=${LON}"
- # Script variables
- TEMPFILENAME=/tmp/rain.tmp
- CHECKMINUTES=15
- CURRENTTIME=`date +%R`
- CURRENTHOUR=`echo "${CURRENTTIME}" | cut -d':' -f1`
- CURRENTMINUTE=`echo "${CURRENTTIME}" | cut -d':' -f2`
- CHECKLINES=$((CHECKMINUTES/5))
- TOTALLINES=0
- TOTALRAIN=0
- # Get Buienradar prediction
- if ! curl -L -o "${TEMPFILENAME}" "${URL}" >/dev/null 2>&1; then
- echo Failed to retrieve Buienradar prediction!
- exit 1
- fi
- PREDICTION=`sed $'s/[^[:print:]\t]//g' "${TEMPFILENAME}"`
- # Parse Buienradar prediction and caclulate amount of rain
- while IFS= read -r LINE; do
- TIME=`echo "${LINE}" | cut -d'|' -f2`
- HOUR=`echo "${TIME}" | cut -d':' -f1`
- MINUTE=`echo "${TIME}" | cut -d':' -f2`
- if [ "${HOUR}" -lt "${CURRENTHOUR}" ]; then continue; fi
- if [ "${HOUR}" -eq "${CURRENTHOUR}" ] && [ "${MINUTE}" -lt "${CURRENTMINUTE}" ]; then continue; fi
- RAIN=`echo "${LINE}" | cut -d'|' -f1 |sed 's/^0*//'`
- TOTALLINES=$((TOTALLINES+1))
- TOTALRAIN=$((TOTALRAIN+RAIN))
- if [ "${TOTALLINES}" -eq "${CHECKLINES}" ]; then break; fi
- done < <(printf '%s\n' "${PREDICTION}")
- AVERAGERAIN=$((TOTALRAIN / TOTALLINES))
- AVERAGERAINMM=`echo "" | awk "END { print 10^((${AVERAGERAIN}-109)/32) }" | awk '{printf "%.1f\n", $0}'`
- # Update rain prediction
- if ! curl "${HOSTPROTOCOL}://${USERNAME}:${PASSWORD}@${HOSTNAME}:${HOSTPORT}/json.htm?type=command¶m=udevice&idx=${PREDICTIONIDX}&nvalue=0&svalue=${AVERAGERAINMM}" >/dev/null 2>&1; then
- echo Failed to switch ON
- exit 1
- fi
- # Get the current state of the switch in Domoticz
- if ! CURRENTSTATE=`curl "${HOSTPROTOCOL}://${USERNAME}:${PASSWORD}@${HOSTNAME}:${HOSTPORT}/json.htm?type=devices&rid=${SWITCHIDX}" 2>/dev/null |grep Status |awk '{print $3}' |sed 's/\"//g' |sed 's/\,//g'`; then
- echo Failed to retrieve current state!
- exit 1
- fi
- # Set Domoticz switch to ON if rain is predicted
- if [ "${AVERAGERAIN}" -ge "99" ] && [ "${CURRENTSTATE}" == "Off" ]; then
- if ! curl "${HOSTPROTOCOL}://${USERNAME}:${PASSWORD}@${HOSTNAME}:${HOSTPORT}/json.htm?type=command¶m=switchlight&idx=${SWITCHIDX}&switchcmd=On" >/dev/null 2>&1; then
- echo Failed to switch ON
- exit 1
- fi
- fi
- # Set Domoticz switch to OFF if no rain is predicted
- if [ "${AVERAGERAIN}" -lt "99" ] && [ "${CURRENTSTATE}" == "On" ]; then
- if ! curl "${HOSTPROTOCOL}://${USERNAME}:${PASSWORD}@${HOSTNAME}:${HOSTPORT}/json.htm?type=command¶m=switchlight&idx=${SWITCHIDX}&switchcmd=Off" >/dev/null 2>&1; then
- echo Failed to switch OFF
- exit 1
- fi
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement