SHARE
TWEET

Smappee to Domoticz

tomvanswam Jul 30th, 2015 (edited) 807 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # Gets energy usage from Smappee API and posts to a Domoticz virtual power sensor (actual+counter)
  3. # Dependencies: curl bc jq logger
  4. # Run from crontab every 5 minutes (because the script retrieves 5 min values)
  5. # Questions or remarks please mail me at tomoticz(AT)vanswam.com
  6. # Or drop a note at: http://www.domoticz.com/forum/viewtopic.php?f=31&t=7312
  7. # To be added:
  8. #       solid error handling in case of API downtime
  9. #       better retrieval last actual value
  10.  
  11. # 2015-AUG-07: Updated sanity check of consumption value.
  12. # 2015-AUG-10: Updated some comments and code (Thanks SimonRG for the feedback)
  13.  
  14. CLIENT_SECRET=""        # Request a client_secret by sending an email to support(AT)smappee.com
  15. CLIENT_ID=""            # Request a client_id by sending an email to support(AT)smappee.com
  16. USERNAME=""             # Your Smappee username
  17. PASSWORD=""             # Your Smappee password
  18. DOMO_USER=""            # Domoticz username
  19. DOMO_PASS=""            # Domoticz password
  20. DOMO_IP=""              # Domoticz IP Address
  21. DOMO_PORT=""            # Domoticz Port
  22. IDX=""                  # Virtual Power Sensor Smappee
  23. JQ="/usr/local/bin/jq"  # Path to jq executable
  24.  
  25. #Redirect all output to syslog
  26. exec 1> >(logger -s -t $(basename $0)) 2>&1
  27.  
  28. #echo "$(date)"
  29. #Get access_token
  30. access_token=`curl -s -X POST https://app1pub.smappee.net/dev/v1/oauth2/token -d "grant_type=password&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&username=$USERNAME&password=$PASSWORD" | $JQ -r '.access_token'`
  31. #echo "Access Token: $access_token"
  32.  
  33. #Get Service Location number
  34. service_location=`curl -s -X GET -H "Authorization: Bearer $access_token" 'https://app1pub.smappee.net/dev/v1/servicelocation' | $JQ -r '.serviceLocations[0].serviceLocationId'`
  35. #echo "Service Location: $service_location"
  36.  
  37. #Calculate time to fetch
  38. end=$(date +%s)
  39. start=$(($end - 630)) #fetch last 10 min + a bit extra, to not get an exact match on a 5 min interval.
  40. end=$(($end * 1000))
  41. start=$(($start * 1000))
  42.  
  43. #Get consumption at said time
  44. consumption=`curl -s -X GET -H "Authorization: Bearer $access_token" "https://app1pub.smappee.net/dev/v1/servicelocation/$service_location/consumption?aggregation=1&from=$start&to=$end" | $JQ -r '.consumptions[0].consumption'`
  45.  
  46. if echo "$consumption" | egrep "[-+]?([0-9]*\.[0-9]+|[0-9]+)" >/dev/null ; then
  47.         POWER=`echo $consumption*12 | bc`
  48.         echo "Consumption: $POWER"
  49.  
  50.         ENERGY_OLD=$(</var/log/smappee.kwh)
  51.         echo "Previous Power: $ENERGY_OLD"
  52.  
  53.         #Sanity check of imported last value
  54.         if echo "$ENERGY_OLD" | egrep "[-+]?([0-9]*\.[0-9]+|[0-9]+)" >/dev/null ; then
  55.                 ENERGY=`echo $ENERGY_OLD+$consumption | bc`
  56.         else
  57.                 ENERGY=$ENERGY_OLD
  58.         fi
  59.  
  60.         echo "$ENERGY" > /var/log/smappee.kwh
  61.         echo "Power: $ENERGY"
  62.  
  63.         #curl -s -H "Accept: application/json" "http://$DOMO_USER:$DOMO_PASS@$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$IDX&nvalue=0&svalue=$POWER" > /dev/null
  64.         curl -s -H "Accept: application/json" "http://$DOMO_USER:$DOMO_PASS@$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$IDX&nvalue=0&svalue=$POWER;$ENERGY" > /dev/null
  65. else
  66.         echo "Power invalid, no update to Domoticz"
  67. fi
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top