Monitor import / export to grid and toggle smart plug using emonpi / emoncms?

Hi,

I’m wanting to monitor the import/export figures to the grid from a home with a solar installation, then run a command based on the result. Is this possible with emonpi / emoncms, via API? Or is there a better way to do it?

e.g. If exporting to grid at > 1.5kW > turn smart plug on. If importing from grid, turn it off.

I’ve sussed out how to remotely change the state of the tp smart plug as below.

curl --request POST "https://wap.tplinkcloud.com?token=MYToken HTTP/1.1" \
 --data '{"method":"passthrough", "params": {"deviceId": "MYDeviceID", "requestData": "{\"system\":{\"set_relay_state\":{\"state\":1}}}" }}' \
 --header "Content-Type: application/json"

Thanks.

Have a look at node-red, you might be able to adapt the evse flow.

Thanks.

I found my old pi2 so bought a board to connect 2 ct clamps and have thrown this together, which for now seems like it works…

*I’m no programmer, so excuse any/all poor coding below!

#!/bin/bash
plug=on

#loop script forever
while true
do

#set rw permissions
rpi-rw >/dev/null

#setup serial device used for ct clamps
stty -F /dev/ttyAMA0 raw speed 38400 >/dev/null

#print ctdata to file
timeout 15 cat /dev/ttyAMA0 > ctdata.csv

#format data to 1 value for ct1 
tail -n 1 ctdata.csv > ct1.csv
awk -F. '{print $1}' ct1.csv > ct1_only.csv

#set ct1 variable
ct1=$(cat ct1_only.csv)

#format data to 1 value for ct2
cut --complement -f 1,3-4 -d, ctdata.csv > ct2.csv
tail -n 1 ct2.csv > ct2_.csv
awk -F. '{print $1}' ct2_.csv > ct2_only.csv

#set ct2 variable
ct2=$(cat ct2_only.csv)

#remove files
rm ctdata.csv
rm ct1.csv
rm ct1_only.csv
rm ct2.csv
rm ct2_.csv
rm ct2_only.csv

#use wattage values from files to change plug state

#on
if [[ "$plug" = "off" ]]
then
echo "Plug state is currently OFF"

if (( ct1 <= xyz )) && (( ct2 >= xyz )); then

   echo "Exporting to Grid - Turning Plug On"
curl --request POST "https://wap.tplinkcloud.com?token=MyToken HTTP/1.1" \
 --data '{"method":"passthrough", "params": {"deviceId": "MyDevice", "requestData": "{\"system\":{\"set_relay_state\":{\"state\":1}}}" }}' \
 --header "Content-Type: application/json"
plug=on
sleep 15m
else
sleep 15s
echo "sleeping 15secs - plug assumed off"
fi
fi




#off
if [[ "$plug" = "on" ]]
then
echo "Plug state is currently ON"
sleep 20s

if (( ct1 > xyz )); then

   echo "Importing from Grid - Turning Plug Off"
   curl --request POST "https://wap.tplinkcloud.com?token=MyToken HTTP/1.1" \
 --data '{"method":"passthrough", "params": {"deviceId": "MyDevice", "requestData": "{\"system\":{\"set_relay_state\":{\"state\":0}}}" }}' \
 --header "Content-Type: application/json"
plug=off
sleep 15m
else

echo "Starting Again!"
sleep 15s
fi
fi
done
exit