Levdbas
4/17/2017 - 11:01 AM

Momenteel opgewekt

Momenteel opgewekt

-- 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}
   return
end

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

-- The switch ID of my device in Domoticz
switchIdx = 8

-- 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 currentPowerStr = t2.currentPower

-- Check the length of that string
local currentPowerLen = string.len(currentPowerStr)

-- Trim off the Kwh from the string and cast it to an integer
local currentPowerKwVal = tonumber(string.sub(currentPowerStr,1,currentPowerLen-2))

-- Multiply the Kwh value to get the wh value
local currentPowerWVal = currentPowerKwVal * 1000

-- Call the update function to add the data to Domoticz
update("Usage", switchIdx, currentPowerWVal, currentPowerWVal)

return commandArray