Advertisement
Guest User

Untitled

a guest
Aug 26th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 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>
  7. HOSTPORT=<domoticz server listening port>
  8. HOSTPROTOCOL=<domoticz server protocol (http/https)>
  9. SWITCHIDX=<idx of the rain expected switch>
  10. PREDICTIONIDX=<idx of the rain forecast mm/h custom sensor>
  11.  
  12. # Buienradar Information
  13. LAT=<your latitude>
  14. LON=<your longitude>
  15. URL="https://gadgets.buienradar.nl/data/raintext?lat=${LAT}&lon=${LON}"
  16.  
  17. # Script variables
  18. TEMPFILENAME=/tmp/rain.tmp
  19. CHECKMINUTES=15
  20. CURRENTTIME=`date +%R`
  21. CURRENTHOUR=`echo "${CURRENTTIME}" | cut -d':' -f1`
  22. CURRENTMINUTE=`echo "${CURRENTTIME}" | cut -d':' -f2`
  23. CHECKLINES=$((CHECKMINUTES/5))
  24. TOTALLINES=0
  25. TOTALRAIN=0
  26.  
  27. # Get Buienradar prediction
  28. if ! curl -L -o "${TEMPFILENAME}" "${URL}" >/dev/null 2>&1; then
  29. echo Failed to retrieve Buienradar prediction!
  30. exit 1
  31. fi
  32. PREDICTION=`sed $'s/[^[:print:]\t]//g' "${TEMPFILENAME}"`
  33.  
  34. # Parse Buienradar prediction and caclulate amount of rain
  35. while IFS= read -r LINE; do
  36. TIME=`echo "${LINE}" | cut -d'|' -f2`
  37. HOUR=`echo "${TIME}" | cut -d':' -f1`
  38. MINUTE=`echo "${TIME}" | cut -d':' -f2`
  39. if [ "${HOUR}" -lt "${CURRENTHOUR}" ]; then continue; fi
  40. if [ "${HOUR}" -eq "${CURRENTHOUR}" ] && [ "${MINUTE}" -lt "${CURRENTMINUTE}" ]; then continue; fi
  41. RAIN=`echo "${LINE}" | cut -d'|' -f1 |sed 's/^0*//'`
  42. TOTALLINES=$((TOTALLINES+1))
  43. TOTALRAIN=$((TOTALRAIN+RAIN))
  44. if [ "${TOTALLINES}" -eq "${CHECKLINES}" ]; then break; fi
  45. done < <(printf '%s\n' "${PREDICTION}")
  46.  
  47. AVERAGERAIN=$((TOTALRAIN / TOTALLINES))
  48. AVERAGERAINMM=`echo "" | awk "END { print 10^((${AVERAGERAIN}-109)/32) }" | awk '{printf "%.1f\n", $0}'`
  49.  
  50. # Update rain prediction
  51. if ! curl "${HOSTPROTOCOL}://${USERNAME}:${PASSWORD}@${HOSTNAME}:${HOSTPORT}/json.htm?type=command&param=udevice&idx=${PREDICTIONIDX}&nvalue=0&svalue=${AVERAGERAINMM}" >/dev/null 2>&1; then
  52. echo Failed to switch ON
  53. exit 1
  54. fi
  55.  
  56. # Get the current state of the switch in Domoticz
  57. 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
  58. echo Failed to retrieve current state!
  59. exit 1
  60. fi
  61.  
  62. # Set Domoticz switch to ON if rain is predicted
  63. if [ "${AVERAGERAIN}" -ge "99" ] && [ "${CURRENTSTATE}" == "Off" ]; then
  64. if ! curl "${HOSTPROTOCOL}://${USERNAME}:${PASSWORD}@${HOSTNAME}:${HOSTPORT}/json.htm?type=command&param=switchlight&idx=${SWITCHIDX}&switchcmd=On" >/dev/null 2>&1; then
  65. echo Failed to switch ON
  66. exit 1
  67. fi
  68. fi
  69.  
  70. # Set Domoticz switch to OFF if no rain is predicted
  71. if [ "${AVERAGERAIN}" -lt "99" ] && [ "${CURRENTSTATE}" == "On" ]; then
  72. if ! curl "${HOSTPROTOCOL}://${USERNAME}:${PASSWORD}@${HOSTNAME}:${HOSTPORT}/json.htm?type=command&param=switchlight&idx=${SWITCHIDX}&switchcmd=Off" >/dev/null 2>&1; then
  73. echo Failed to switch OFF
  74. exit 1
  75. fi
  76. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement