Advertisement
Guest User

Ubiquiti mPower values to Domoticz

a guest
Aug 1st, 2015
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.38 KB | None | 0 0
  1. #!/bin/bash                                                                    
  2. #PLUGNAME=MPBureau #Not needed it looks, works fine without
  3. PLUGIP=192.168.4.22
  4. PLUGUSER=ubnt           #default "ubnt"
  5. PLUGPASSWORD=ubnt       #default "ubnt"
  6. SESSIONID=01234567890123456789012345678901
  7.  
  8. DOMOTICZSERVER=192.168.4.4
  9. DOMOTICZPORT=8084
  10.  
  11. #Create virtual sensors in Domoticz and enter their IDX's here. For the sensor types, see Domoticz JSON wiki
  12. IDX_POWER=243          
  13. IDX_VOLT=241           
  14. IDX_CURRENT=           
  15. IDX_PF=                
  16.  
  17.  
  18. if ping -c 1 $PLUGIP > /dev/null ; then  # if host is online then:
  19.    
  20.     #Login to webinterface of plug
  21.     curl --silent -X POST -d "username=$PLUGUSER&password=$PLUGPASSWORD" -b "AIROS_SESSIONID=$SESSIONID" $PLUGIP/login.cgi
  22.  
  23.     #Turn outlet no1 on
  24.     #curl --silent -X PUT -d output=1 -b "AIROS_SESSIONID="$SESSIONID 192.168.4.22/sensors/1 > /dev/null
  25.  
  26.     #Wait for 2 seconds to let plug measure the load
  27.     #sleep 2
  28.    
  29.     #Retrieve all data from JSON-output
  30.     SENSOR=$(curl --silent -b "AIROS_SESSIONID=$SESSIONID" $PLUGIP/sensors)
  31.  
  32.  
  33.     #Grab power (Watt) from retrieved values and leave 1 decimal, stripoff rest (change printf %.1f if you want more/less decimals)
  34.     MPPOWER=$(echo $SENSOR | cut -d "," -f3 | cut -d ":" -f2 | awk '{printf("%.1f\n", $1)}')
  35.     #Send data to Domoticz sensor without displaying any curl output on commandline
  36.     curl --silent $DOMOTICZSERVER:$DOMOTICZPORT'/json.htm?type=command&param=udevice&idx='$IDX_POWER'&svalue='$MPPOWER > /dev/null
  37.     #Display powerconsumption (Watt) on commandline
  38.     echo $MPPOWER 'Watt'
  39.  
  40.  
  41.     #Grab current (Ampère) from retrieved values and leave 1 decimal, stripoff rest (change printf %.3f if you want more/less decimals)
  42.     MPCURRENT=$(echo $SENSOR | cut -d "," -f5 | cut -d ":" -f2 | awk '{printf("%.1f\n", $1)}')
  43.     #Send data to Domoticz sensor without displaying any curl output on commandline
  44.     #curl --silent $DOMOTICZSERVER:$DOMOTICZPORT'/json.htm?type=command&param=udevice&idx='$IDX_CURRENT'&svalue='$MPCURRENT > /dev/null
  45.     #Display current (Ampère) on commandline
  46.     echo $MPCURRENT 'A'
  47.  
  48.  
  49.     #Grab voltage (Volts) from retrieved values and leave 3 decimals, stripoff rest (change printf %.3f if you want more/less decimals)
  50.     MPVOLTAGE=$(echo $SENSOR | cut -d "," -f6 | cut -d ":" -f2 | awk '{printf("%.3f\n", $1)}')
  51.     curl --silent $DOMOTICZSERVER:$DOMOTICZPORT'/json.htm?type=command&param=udevice&idx='$IDX_VOLT'&svalue='$MPVOLTAGE > /dev/null
  52.     #Display mains voltage (Volt) on commandline
  53.     echo $MPVOLTAGE 'Volt'
  54.  
  55.     #Grab powerfactor (cos phi) from retrieved values and leave 2 decimals, stripoff rest (change printf %.2f if you want more/less decimals)
  56.     MPPOFA=$(echo $SENSOR | cut -d "," -f7 | cut -d ":" -f2 | awk '{printf("%.2f\n", $1)}')
  57.     #Send data to Domoticz sensor without displaying any curl output on commandline
  58.     #curl --silent $DOMOTICZSERVER:$DOMOTICZPORT'/json.htm?type=command&param=udevice&idx='$IDX_PF'&svalue='$MPPOFA > /dev/null
  59.     #Display mains voltage (Volt) on commandline
  60.     echo $MPPOFA 'cos phi'
  61.  
  62.     #Display info message
  63.     #echo "Sensors in Domoticz should be updated with new values, have a look at them"
  64.    
  65.     #Turn outlet no1 off
  66.     #curl --silent -X PUT -d output=0 -b "AIROS_SESSIONID="$SESSIONID 192.168.4.22/sensors/1 > /dev/null
  67.  
  68.     #Logout from plug
  69.     curl -b "AIROS_SESSIONID=$SESSIONID" $PLUGIP/logout.cgi
  70.  
  71.  
  72. else #Plug not responding to ping, display error message
  73.     echo "Plug not responding to ping, is it connected to your WLAN?"
  74. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement