Page 1 of 1

Rainforest RFA-Z109 Eagle Power Meter Bash Script

Posted: Friday 30 June 2017 18:30
by ben53252642
This is a linux bash script I wrote for getting usage information from the Rainforest RFA-Z109 Eagle and loading it into Domoticz.

I have this setup working successfully, my energy retailer in Australia is Red Energy and my distributer is CitiPOWER in Victoria.

I simply created an account here to link the Eagle with my CitiPOWER smart meter:

Once linked the below script is able to get data from the Eagle. You will need to fill in both the Eagle and Domoticz configuration sections of the script and also set the Domoticz Device IDX number in the "# Send data to Domoticz virtual sensor type: Electricity (Instant+Counter)" section which is currently set to 635.

getdata.sh

Code: Select all

#!/bin/bash
while true; do

# Eagle Configuration (you can find the CloudID and IN numbers on the back of your Eagle device)
EagleIPAddress="IPADDRESS"
CloudID="CLOUDID"
IN="INNUMBER"
MAC="MACADDRESS" #// Format example: e8p5b20050003ac1

# Domoticz Configuration
domoticzserver="IPADDRESS"
domoticzport="80"
domoticzuser="USERNAME"
domoticzpass="PASSWORD"

# Get the data
data=$(curl -su "$CloudID":"$IN" "http://"$EagleIPAddress"/cgi-bin/cgi_manager" -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-GB,en-US;q=0.8,en;q=0.6' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'Accept: */*' -H 'X-Requested-With: XMLHttpRequest' --data $"<LocalCommand>\n  <Name>get_usage_data</Name>\n  <MacId>0x"$MAC"</MacId>\n</LocalCommand>\n<LocalCommand>\n  <Name>get_price_blocks</Name>\n  <MacId>0x"$MAC"</MacId>\n</LocalCommand>\n" --compressed)
# Meter connection status
status=$(echo "$data" | grep '{"meter_status":' | awk -F'"' '{print $4}')

# If Eagle is connected to the meter, fetch usage data and load it into Domoticz
if [ "$status" == "Connected" ]; then
	# Current usage
	current=$(echo "$data" | grep '"demand":' | sed 's/[^0-9.]*//g')
	currentunits=$(echo "$data" | grep '"demand_units":' | awk -F'"' '{print $4}')
	# Meter reading
	fromgrid=$(echo "$data" | grep '"summation_delivered":' | sed 's/[^0-9.]*//g')
	togrid=$(echo "$data" | grep '"summation_received":' | sed 's/[^0-9.]*//g')
	gridunits=$(echo "$data" | grep '"summation_units":' | awk -F'"' '{print $4}')
	# Display data in terminal
	echo
	echo "Connection Status"":" "$status"
	echo "Current" "$currentunits"":" "$current"
	echo "From Grid" "$gridunits"":" "$fromgrid"
	echo "To Grid" "$gridunits"":" "$togrid"
	# Do some math to prepare the data for Domoticz
	if [ "$currentunits" == "kW" ]; then
		currentwatts=$(echo "$current * 1000" | bc -l) #// Convert kW to Watts
	fi
	fromgriddomoticz=$(echo "$fromgrid * 1000" | bc -l) #// Multiply by 1000 for Domoticz
	# Send data to Domoticz virtual sensor type: Electricity (Instant+Counter)
	curl -s "http://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=635&nvalue=0&svalue={$currentwatts};{$fromgriddomoticz}"
else
	echo "No connection to Meter, will check again shortly..."
fi

sleep 15
done
Domoticz virtual sensor type: Electricity (Instant+Counter)
Screen Shot 2017-07-01 at 2.25.30 am.png
Screen Shot 2017-07-01 at 2.25.30 am.png (63.6 KiB) Viewed 1240 times
Bash terminal output should look similar to this:
Screen Shot 2017-07-01 at 2.26.26 am.png
Screen Shot 2017-07-01 at 2.26.26 am.png (56.94 KiB) Viewed 1240 times
Below is a script I use for running the getdata.sh in the background, requires screen to be installed: sudo apt-get install screen

startup.sh

Code: Select all

#!/bin/bash
/usr/bin/screen -S eaglemonitor -d -m /root/eagle/getdata.sh
Here is the startup script I've got in /etc/init.d to start the startup.sh script when the system boots

eaglemonitor

Code: Select all

#!/bin/sh
### BEGIN INIT INFO
# Provides: eaglemonitor
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 5
# Default-Stop:
# Description:
### END INIT INFO

case "$1" in
'start')
        sudo /root/eagle/startup.sh
        ;;
'stop')
        ;;
*)
        echo "Usage: $0 { start | stop }"
        ;;
esac
exit 0
Screen Shot 2017-07-01 at 4.55.13 pm.png
Screen Shot 2017-07-01 at 4.55.13 pm.png (297.41 KiB) Viewed 1220 times
This script is meant for users with moderate to advanced Linux knowledge. :D