4/17/2017 - 11:01 AM

solar total.lua

-- Script to pull GoodWe power generation data into Domoticz
-- Written by M Grimwade 2015

-- Function to update a Domoticz sensor object
function update(device, id, power, energy)
      commandArray[1] = {['UpdateDevice'] = id .. "|0|" .. energy}

-- Command array to populate with Domoticz instructions
commandArray = {}

-- The switch ID of my device in Domoticz
todayswitch = 12

-- Load the JSON Lua module
JSON = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()

-- Reference the HTTP module enabling web calls
    http = require 'socket.http'

-- Perform an HTTP request to get the raw string from the REST endpoint
html = http.request 'http://www.goodwe-power.com/Mobile/GetMyPowerStationByUser?username=vanderbas'

-- Decode the JSON formatted string to a Lua table
local t = JSON:decode(html)

-- Pull out the sub table containing the data from the main table
local t2 = t[1]

-- Get the currentPower value from the sub table into a string
local todayPowerStr = t2.value_eDayTotal

-- Check the length of that string
local todayPowerLen = string.len(todayPowerStr)

-- Trim off the Kwh from the string and cast it to an integer
local todayPowerKwVal = tonumber(string.sub(todayPowerStr,1,todayPowerLen-3))

-- Call the update function to add the data to Domoticz
update("Usage", todayswitch, todayPowerKwVal, todayPowerKwVal)

return commandArray