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

**URL:** <https://community.openenergymonitor.org/t/monitor-import-export-to-grid-and-toggle-smart-plug-using-emonpi-emoncms/8391>\
**Category:** Getting Started\
**Tags:** solarpv, solarpvdivert, emoncms, emonpi\
**Created:** [29 August 2018 15:38 UTC](https://community.openenergymonitor.org/t/monitor-import-export-to-grid-and-toggle-smart-plug-using-emonpi-emoncms/8391 "2018-08-29T15:38:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phproxy](https://community.openenergymonitor.org/letter_avatar_proxy/v4/letter/p/0ea827/32.png) [@phproxy](https://community.openenergymonitor.org/u/phproxy)\
**Post date:** [29 August 2018 15:38 UTC](https://community.openenergymonitor.org/t/monitor-import-export-to-grid-and-toggle-smart-plug-using-emonpi-emoncms/8391/1 "2018-08-29T15:38:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Pukka](https://community.openenergymonitor.org/letter_avatar_proxy/v4/letter/p/d07c76/32.png) [@Pukka](https://community.openenergymonitor.org/u/Pukka)\
**Post date:** [29 August 2018 19:31 UTC](https://community.openenergymonitor.org/t/monitor-import-export-to-grid-and-toggle-smart-plug-using-emonpi-emoncms/8391/2 "2018-08-29T19:31:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![phproxy](https://community.openenergymonitor.org/letter_avatar_proxy/v4/letter/p/0ea827/32.png) [@phproxy](https://community.openenergymonitor.org/u/phproxy)\
**Post date:** [4 September 2018 11:26 UTC](https://community.openenergymonitor.org/t/monitor-import-export-to-grid-and-toggle-smart-plug-using-emonpi-emoncms/8391/3 "2018-09-04T11:26:02Z")

</div>

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!

```auto
#!/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

```
