Advertisement
Guest User

Untitled

a guest
Aug 26th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Domoticz Server Information
  4. USERNAME=<domoticz username>
  5. PASSWORD=<domoticz password>
  6. HOSTNAME=<domoticz server fqdn or ip address>
  7. HOSTPORT=<domoticz server listening port>
  8. HOSTPROTOCOL=<domoticz server protocal (http/https)>
  9. SWITCHIDX=<id of the switch in domoticz>
  10.  
  11. # Buienradar Information
  12. LAT=<your latitude>
  13. LON=<your longitude>
  14. URL="https://gadgets.buienradar.nl/data/raintext?lat=${LAT}&lon=${LON}"
  15.  
  16. # Script variables
  17. TEMPFILENAME=/tmp/rain.tmp
  18. CHECKMINUTES=15
  19. CURRENTTIME=`date +%R`
  20. CURRENTHOUR=`echo "${CURRENTTIME}" | cut -d':' -f1`
  21. CURRENTMINUTE=`echo "${CURRENTTIME}" | cut -d':' -f2`
  22. CHECKLINES=$((CHECKMINUTES/5))
  23. TOTALLINES=0
  24. TOTALRAIN=0
  25.  
  26. # Get Buienradar Prediction
  27. wget -q -m 10 -O "${TEMPFILENAME}" "${URL}"
  28. PREDICTION=`sed $'s/[^[:print:]\t]//g' "${TEMPFILENAME}"`
  29.  
  30. # Parse Buienradar Prediction and caclulate predicted amount of rain
  31. while IFS= read -r LINE; do
  32. TIME=`echo "${LINE}" | cut -d'|' -f2`
  33. HOUR=`echo "${TIME}" | cut -d':' -f1`
  34. MINUTE=`echo "${TIME}" | cut -d':' -f2`
  35. if [ "${HOUR}" -lt "${CURRENTHOUR}" ]; then continue; fi
  36. if [ "${HOUR}" -eq "${CURRENTHOUR}" ] && [ "${MINUTE}" -lt "${CURRENTMINUTE}" ]; then continue; fi
  37. RAIN=`echo "${LINE}" | cut -d'|' -f1 |sed 's/^0*//'`
  38. TOTALLINES=$((TOTALLINES+1))
  39. TOTALRAIN=$((TOTALRAIN+RAIN))
  40. AVERAGERAIN=$((TOTALRAIN / TOTALLINES))
  41. if [ "${TOTALLINES}" -eq "${CHECKLINES}" ]; then break; fi
  42. done < <(printf '%s\n' "${PREDICTION}")
  43.  
  44. # Get the current state of the switch in Domoticz
  45. 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
  46. echo Failed to retrieve current state!
  47. exit 1
  48. fi
  49.  
  50. # Set Domoticz switch to ON if rain is predicted
  51. if [ "${AVERAGERAIN}" -ge "77" ] && [ "${CURRENTSTATE}" == "Off" ]; then
  52. if ! curl "${HOSTPROTOCOL}://${USERNAME}:${PASSWORD}@${HOSTNAME}:${HOSTPORT}/json.htm?type=command&param=switchlight&idx=${SWITCHIDX}&switchcmd=On" >/dev/null 2>&1; then
  53. echo Failed to switch ON
  54. exit 1
  55. fi
  56. fi
  57.  
  58. # Set Domoticz switch to OFF if no rain is predicted
  59. if [ "${AVERAGERAIN}" -lt "77" ] && [ "${CURRENTSTATE}" == "On" ]; then
  60. if ! curl "${HOSTPROTOCOL}://${USERNAME}:${PASSWORD}@${HOSTNAME}:${HOSTPORT}/json.htm?type=command&param=switchlight&idx=${SWITCHIDX}&switchcmd=Off" >/dev/null 2>&1; then
  61. echo Failed to switch OFF
  62. exit 1
  63. fi
  64. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement