Page 1 of 2
Real power consumption
Posted: Saturday 04 March 2017 21:28
by olivierf
I was wondering what my actual power consumption was, which was sort of hard to see when my solar panels were generating power (oke not so very difficult but it was tricky to see in the log).
I created a dummy device (measuring watts) and created this LUA script to calculate it.
I don't expect it to be very accurate especially since the power generated by the panels is only updated every 5 minutes or so (solaredge API).
Maybe its useful to someone else
Code: Select all
if (devicechanged['Power'] ~= meter) then
meter = devicechanged['Power']
--P1 meter uitsplitsen
power1, power2, power3, power4, power5, power6 = devicechanged['Power']:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")
--Zonnepanelen uitsplitsen
paneelpower = otherdevices_svalues['Zonnepanelen']:match("([^;]+);([^;]+)")
--Echte verbruik berekenen: verbruik - teruglevering + panelen opbrengst
stroomverbruik = power5 - power6 + paneelpower
--En in een dummy device gooien met idx 85
commandArray[1] = {['UpdateDevice'] = '85|0|' .. stroomverbruik}
end
Re: Real power consumption
Posted: Monday 06 March 2017 12:28
by Abbadon
Hello, can you tell me how to use that lua with 1 device measuring watts? i would like to know what its kWh consuption
25 EMON.Phase.1.Amp 1.0;0.0;0.0 2017-03-06 12:25:46 1.0;0.0;0.0
26 EMON.Phase.2.Amp 1.2;0.0;0.0 2017-03-06 12:25:46 1.2;0.0;0.0
27 EMON.Phase.3.Amp 1.1;0.0;0.0 2017-03-06 12:25:46 1.1;0.0;0.0
28 EMON.Phase.Total 3.3;0.0;0.0 2017-03-06 12:25:46 3.3;0.0;0.0
29 EMON.V.AC 237.150 2017-03-06 12:25:46 237.150
30 EMON.Power 793.2 2017-03-06 12:25:46 793.2
31 EMON.TEMP 23.8 2017-03-06 12:24:42 23.8
95 Power 0;0;0;0;0;0 2017-03-06 12:17:25 0;0;0;0;0;0
Re: Real power consumption
Posted: Tuesday 07 March 2017 10:32
by olivierf
Abbadon wrote:Hello, can you tell me how to use that lua with 1 device measuring watts? i would like to know what its kWh consuption
25 EMON.Phase.1.Amp 1.0;0.0;0.0 2017-03-06 12:25:46 1.0;0.0;0.0
26 EMON.Phase.2.Amp 1.2;0.0;0.0 2017-03-06 12:25:46 1.2;0.0;0.0
27 EMON.Phase.3.Amp 1.1;0.0;0.0 2017-03-06 12:25:46 1.1;0.0;0.0
28 EMON.Phase.Total 3.3;0.0;0.0 2017-03-06 12:25:46 3.3;0.0;0.0
29 EMON.V.AC 237.150 2017-03-06 12:25:46 237.150
30 EMON.Power 793.2 2017-03-06 12:25:46 793.2
31 EMON.TEMP 23.8 2017-03-06 12:24:42 23.8
95 Power 0;0;0;0;0;0 2017-03-06 12:17:25 0;0;0;0;0;0
What's your question exactly ?
The device you're listing here does not have a kWh meter. It can be calculated from reading the EMON.Power (very often) but realisticly that wont be very accurate (also I wouldn't know how to do that)
Re: Real power consumption
Posted: Tuesday 07 March 2017 12:06
by L2v2P
I have implemented more or less the same functionality. However I also wanted to send the energy (kWh) to the dummy device. I do this with this code:
Code: Select all
WhDagConsumptie = WhDagProductie - WhDagExport + WhDagImport
- WhDagProductie being the energy produced by the solar panels
- WhDagExport being the energy fed back to the grid
- WhDagImport being the energy imported from the grid
I then send the power and energy to a dummy device:
However, it functions not quite as expected. If you go to the charts it doesn't log the energy in the first graph, but it does in the second:
Also the all-time counter resets at 00:00 every day. Any idea what the problem might be?
Re: Real power consumption
Posted: Tuesday 07 March 2017 14:35
by L2v2P
I find this to be quite unusable, especially on days where you have a mix of clouds and sun. I often get negative values for power consumption because the power output of the solar panels has not yet been updated.
Re: Real power consumption
Posted: Tuesday 07 March 2017 15:29
by adjego
When using the API from Solaredge this will never work. That actual power output of the solar panels is at least some minutes behind. I had the same problem when using the API so i switch to measuring it in a different way and created a seperate dummy "smart meter" where i can see the consumption from the panels and from the grid in 1 overview.
Re: Real power consumption
Posted: Tuesday 07 March 2017 17:33
by L2v2P
adjego wrote:When using the API from Solaredge this will never work. That actual power output of the solar panels is at least some minutes behind. I had the same problem when using the API so i switch to measuring it in a different way and created a seperate dummy "smart meter" where i can see the consumption from the panels and from the grid in 1 overview.
Can you share what you did? Because I don't know how to calculate consumption when my solar panels produce any amount of energy.
Re: Real power consumption
Posted: Thursday 09 March 2017 16:19
by adjego
So you have the amount of generated power from the solar panels. Next to that you need the power which has been returned and when you calculate "generated power - returned power" you will have the actual consumed power from the solar panels. Then you add the power used from the net and then you have your actual consumed power of that day.
I measure my solar panel output using a z-wave module which reports every minute.
Home Energy Meter Gen5
All this data i put in a dummy "Smart Meter" which automatically calculates the amount per day, and in the overview you can see how much came from the Net and how much was used directly from the panels.
Darkblue is from the Net, light blue is directly used from the solarpanels.
Re: Real power consumption
Posted: Thursday 09 March 2017 18:15
by L2v2P
Ah, so you circumvent the SolarEdge API. Is once every minute accurate enough (as the P1 reports every 10 seconds)?
Re: Real power consumption
Posted: Thursday 09 March 2017 19:11
by Toulon7559
For clarity and for unambigous information, please apply in the software and the description the correct definitions:
Power is the
actual generation and
actual consumption,
expressed in VA or W(atts)
Energy is the
accumulated generation and the
accumulated consumption,
expressed in kWh. For example over 1 day, over 1 week, 1 month, 1 year, or over the whole lifecycle of registration.
Otherwise it gets confusing .........
Trying to read actual power values from various digital sources such as inverters and from the smartmeter is difficult, because of different intervals and latencies: those read-outs seldomly will be in sync.
Based on experience, I register & display them separately: direct calculation with this values sometimes produces odd results, as described.
If you desire more realistic values, first you will have to filter the registered values, to determine tendencies.
Re: Real power consumption
Posted: Friday 10 March 2017 8:40
by adjego
L2v2P wrote:Ah, so you circumvent the SolarEdge API. Is once every minute accurate enough (as the P1 reports every 10 seconds)?
So what i do is that when the data from the solar panels comes in, i use the most recent data from the smart meter. The data from the smartmeter can be max 9 seconds old, which indeed sometimes causes a small Power calculation error, but that's rearly the case.
So calculation the power:
When power consumption is positive, generated power and consumed power are added together.
When power consumption is negative, consumed power is deducted from the generated power.
Toulon7559 wrote:For clarity and for unambigous information, please apply in the software and the description the correct definitions:
Power is the
actual generation and
actual consumption,
expressed in VA or W(atts)
Energy is the
accumulated generation and the
accumulated consumption,
expressed in kWh. For example over 1 day, over 1 week, 1 month, 1 year, or over the whole lifecycle of registration.
Otherwise it gets confusing .........
Sorry for the confusion, but for this dummy smartmeter i use actual total values because this dummy smartmeter does the day calculations himself. No special day values need to be calculated this way. This gives a nice overview of directly used energy from the solarpanels and from the net.
Here you will find my lua code:
Code: Select all
local idxElektraActualConsumption = 213
local idxElektraMinimum = 66
local idxElektraMaximum = 67
local idxElektraTerug = 177
local nrCommands = 1
local zeroValue = 0
commandArray = {}
function Round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
if (devicechanged['Zon']) then
zonOutput, zonTotal = otherdevices_svalues['Zon']:match("([^;]+);([^;]+)")
zonOutput = Round(tonumber(zonOutput), 0)
zonTotal = tonumber(zonTotal)
zonTotal = zonTotal + 900000
consumedDal, consumedPiek, returnDal, returnPiek, currentUsage, currentReturn = otherdevices_svalues['Stroom']:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")
v1, previousConsumedFromPanel, v3, v4, previousUsage, v6 = otherdevices_svalues['Daadwerkelijke Verbruik']:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")
consumedDal = tonumber(consumedDal)
consumedPiek = tonumber(consumedPiek)
returnDal = tonumber(returnDal)
returnPiek = tonumber(returnPiek)
currentUsage = tonumber(currentUsage)
currentReturn = tonumber(currentReturn)
previousConsumedFromPanel = tonumber(previousConsumedFromPanel)
previousUsage = tonumber(previousUsage)
consumedTotal = consumedDal + consumedPiek
returnTotal = returnDal + returnPiek
currentMinimum = tonumber(otherdevices_svalues['Elektra minimum'])
currentMaximum = tonumber(otherdevices_svalues['Elektra maximum'])
currentTerug = tonumber(otherdevices_svalues['Teruglevering'])
actualConsumedFromPanel = zonTotal - returnTotal
--print('actualConsumedFromPanel: ' .. actualConsumedFromPanel)
--print('previousConsumedFromPanel: ' .. previousConsumedFromPanel)
--print('zonTotal: ' .. zonTotal)
--print('previousUsage: ' .. previousUsage)
--print('zonOutput: ' .. zonOutput)
--print('currentUsage: ' .. currentUsage)
--print('currentReturn: ' .. currentReturn)
-- Check what to do when there is generated power.
if (zonOutput > 0) then
if (currentUsage > 0) then
actualCurrentUsage = zonOutput + currentUsage
elseif (currentReturn >= 0 and zonOutput > currentReturn) then
actualCurrentUsage = zonOutput - currentReturn
else
actualCurrentUsage = previousUsage
end
else
actualCurrentUsage = currentUsage
end
-- There is no change in my house consumption will be below 30 watt. This will fix any calculation errors.
if (actualCurrentUsage < 30) then
actualCurrentUsage = previousUsage
end
-- Because the Z-wave reports power and energy in two steps, sometimes energy did not increase but return did. This is to prevent going back down in consumed energy from panels.
if (actualConsumedFromPanel < previousConsumedFromPanel) then
actualConsumedFromPanel = previousConsumedFromPanel
end
commandArray[nrCommands]={['UpdateDevice'] = idxElektraActualConsumption .. '|0|' .. consumedTotal .. ";" .. actualConsumedFromPanel .. ";" .. zeroValue .. ";" .. zeroValue .. ";" .. actualCurrentUsage .. ";" .. zeroValue}
end
return commandArray
Posted: Friday 19 January 2018 23:38
by Micha123
I think we need a script, that show only consumpted power in KWh, not actual usage, this dont work if one device (P1 meter) send data every 10 second and the api (SolarEdge) every 5 min.
The onky way is to calculate consumpted power.a script that simply listen when the SolarEdge api send data to domoticz an if it receive data from solaredge api then calculate consupted power
Usage + generated solar energy - returned power.
And if a script do that every time after solarede api refresh/send data to domoticz. Wil be fine, so you can see every 5 min your consupted power @your home.
This wil be best acurate, but if that script calculate it every time after SolarEdge send data, is the data from p1 max 9 second old. I thing its not a big problem by calculate with the consupted power not the actual usage power.
Re: Real power consumption
Posted: Thursday 08 February 2018 16:46
by McMelloW
timothee wrote: ↑Tuesday 26 September 2017 20:38
Has anyone got this kind of calculations working with a SolarEdge inverter (linked through the web API in Domoticz)?
At the end of this
topic you can read about reading a SolarEdge Inverter via LAN. I use the perl script
sunspec-monitor to get the basic data from my SolarEdge. This in avoiding to use de SolarEdge website API.
All you need is a LAN connection with your SolarEdge Inverter. This
document shows how to configure your LAN port on your Inverter. You might also need n update of your firmware. I made one phone call to Solaedge NL support and that was started within 5 minutes.
Below you can see examples of the ouput in json or csv.
Code: Select all
This is the json output.
{
"timestamp": "2018-02-08 14:14:06",
"status": "ON (MPPT)",
"ac_power": 1637,
"dc_power": 1661,
"total_production": 207648,
"ac_voltage": 236.70,
"ac_current": 6.95,
"dc_voltage": 377.40,
"dc_current": 4.40,
"temperature": 41.70,
"exported_energy": 0,
"imported_energy": 0
}
Code: Select all
And the csv output
2018-02-08 15:52:26,ON (MPPT),485,492,209221,236.60,2.12,377.20,1.31,40.49,0,0
Finally, to your question. No,I don't have a working calculation yet. See or the values, you can get from the inverter via LAN provides you enough information you need. The value for ac_power (Watt), is almost the same as ac_voltage * ac_cuurent (Watt). The production/time can easy be calculated by subtracting total_production at the end and the begin of a time interval on the most simple way.
Using the SolarEdge web API, the gererated devices shows other values then what I see the device list in Domoticz. And also different form the values I see at my SolarEdge customer website. The values directly from my inverter do match much more with my SolarEdge customer website and I know there is a time difference in real time from the inverter and displayed at the website.
At this moment I am writing a dzVents script where I use an os.execute('perl sunspec-status ......') to run the perl script. The hardest thing is to understand the use and the working of the Counter type device (RFXmeter, RFX meter counter) and the Energy type device (General, kWh). It is very hard to find any detaled information about what is going underneath the hood of these devices. I hope somebody can explain this to me or point me to some documentation.
Re: Real power consumption
Posted: Saturday 08 September 2018 11:15
by akamming
Everyone thanks for the ideas. I created a dzvents script using the ideas in this thread. In no way i want to take credit for the script, because most ideas and pieces of code i found on this forum. I just combined and adjusted to my own needs, however it now works like a charm and since i could not find a fully functioning real power consumption script on this forum, i decided to give something back to the community by posting this script.
prerequisites to make it work
- Sunspec monitor installed on your system. (can be found here
https://github.com/tjko/sunspec-monitor)
- Modbus TCP enabled on your solaredge invertor on the default port (instructions can be found here
https://www.solaredge.com/sites/default ... l-note.pdf)
- a p1 smart smart meter device
- a virtual electricity device (usage, counter) to store the actual energy output of your inverter
- a virtual electricity device (usage, counter) to store the real power consumption
- a virtual temperature device to store the temperature for the inverter (not needed for real power consumption, so you can remove this part from the code if you like)
and make sure in the code below you change
- the ip adress (should be the one of your inverter).
- possible add a port number with using "-p" to the sunspec monitor (if you used another port number then the default 502 port on the inverter)
- possibly the path to sunspec monitor (depending on how you installed sunspec monitor)
- the names of the devices, so they match the devices in your domoticz system
Sorry for some dutch words in the code. But feel free to change to english and repost
Code: Select all
return {
logging = {
level = domoticz.LOG_DEBUG, -- Adjust to your needs
marker = 'ActualPowerConsumption'
},
on = {
devices = {
'Slimme Meter' -- Name of P1 Smart Meter device
}
},
execute = function(domoticz, item)
-- Get SolarEdge input using sunspec monitor
local handle = io.popen('sunspec-status -m 0 -j 192.168.2.2 || echo error')
local result = handle:read("*a")
handle:close()
-- For debugging purposes: Log result
domoticz.log ("JSON output Inverter is "..result)
-- Check if we are succesful
if(result ~= 'error\n') then
-- no error: declare needed opbjects
local SM=domoticz.devices("Slimme Meter") -- P1 Smart Meter Device
local SE=domoticz.devices("Zonnepanelen") -- Virtual Electricity device (usage, counter) in which the actual Solar Output will be stored
local SV=domoticz.devices("Stroom Verbruik") -- Virtual Electricity devie (usage, counter) in which the actual power consumption will be stored
local OT=domoticz.devices("Omvormer Temperatuur") -- Virtual Temperature device, in which the inverter temperature will be stored
local usage=SM.usage
local usagedelivered=SM.usageDelivered
domoticz.log ("Usage = "..usage ..", Delivered = "..usagedelivered..", P1 cumulative="..SM.usage1+SM.usage2-SM.return1-SM.return2)
-- convert JSON object to readable format
local json = domoticz.utils.fromJSON(result)
-- log timestamp for debugging purposes
local Time=require('Time')
domoticz.log("Timestamp inverter = "..json.timestamp..", which is "..Time(json.timestamp).secondsAgo.." second(s) ago")
-- update SolarEdge virtual sensors
domoticz.log("SE.updateElectricity("..json.dc_power..", "..json.total_production..")")
SE.updateElectricity(json.dc_power, json.total_production)
domoticz.log("OT.updateTemperature("..json.temperature..")")
OT.updateTemperature(json.temperature)
-- calculate real energy consumption
local virtuelemeterstand=json.total_production+SM.usage1+SM.usage2-SM.return1-SM.return2
local vermogen=SM.usage-SM.usageDelivered+json.dc_power
-- update real actual usage sensor
domoticz.log("SV.updateElectricity("..vermogen..","..virtuelemeterstand..")")
SV.updateElectricity(vermogen,virtuelemeterstand)
else
domoticz.log('failure with io.popen')
end
end
}
have fun! let me know if you like it.
Re: Real power consumption
Posted: Monday 15 October 2018 12:09
by 1kke
Hello akamming,
It took a while before I had the script running (I'm running domoticz in a Alpine Docker).
But now everything is fine. It's a great script for measuring the actual powerconsumption.
kr./m.vr.gr.,
1kke
Re: Real power consumption
Posted: Friday 06 September 2019 10:48
by akamming
Reviving this thread
I just updated to latest beta (4.11290) and i noticed that my virtual device for the real power consumption is not updated on the webpage anymore.
It is only a GUI problem, because when i press F5 in the browser the measurements are there, so the script apparently still works. I tried in chrome, ie and edge. And they all have the same behaviour. Does anyone else experience this problem?
I also noticed the that P1 meter in the GUI now change exactly at the moment i see a logline of the device that the value was changed (nice!), so it looks pretty realtime: Could this be related to the websockets change which was done on the GUI? Anyone have an idea how to fix so the virtual sensors are also updated realtime?
Re: Real power consumption
Posted: Monday 23 September 2019 20:22
by th3snip3r
akamming wrote: ↑Saturday 08 September 2018 11:15
Everyone thanks for the ideas. I created a dzvents script using the ideas in this thread. In no way i want to take credit for the script, because most ideas and pieces of code i found on this forum. I just combined and adjusted to my own needs, however it now works like a charm and since i could not find a fully functioning real power consumption script on this forum, i decided to give something back to the community by posting this script.
prerequisites to make it work
- Sunspec monitor installed on your system. (can be found here
https://github.com/tjko/sunspec-monitor)
- Modbus TCP enabled on your solaredge invertor on the default port (instructions can be found here
https://www.solaredge.com/sites/default ... l-note.pdf)
- a p1 smart smart meter device
- a virtual electricity device (usage, counter) to store the actual energy output of your inverter
- a virtual electricity device (usage, counter) to store the real power consumption
- a virtual temperature device to store the temperature for the inverter (not needed for real power consumption, so you can remove this part from the code if you like)
and make sure in the code below you change
- the ip adress (should be the one of your inverter).
- possible add a port number with using "-p" to the sunspec monitor (if you used another port number then the default 502 port on the inverter)
- possibly the path to sunspec monitor (depending on how you installed sunspec monitor)
- the names of the devices, so they match the devices in your domoticz system
Sorry for some dutch words in the code. But feel free to change to english and repost
Code: Select all
return {
logging = {
level = domoticz.LOG_DEBUG, -- Adjust to your needs
marker = 'ActualPowerConsumption'
},
on = {
devices = {
'Slimme Meter' -- Name of P1 Smart Meter device
}
},
execute = function(domoticz, item)
-- Get SolarEdge input using sunspec monitor
local handle = io.popen('sunspec-status -m 0 -j 192.168.2.2 || echo error')
local result = handle:read("*a")
handle:close()
-- For debugging purposes: Log result
domoticz.log ("JSON output Inverter is "..result)
-- Check if we are succesful
if(result ~= 'error\n') then
-- no error: declare needed opbjects
local SM=domoticz.devices("Slimme Meter") -- P1 Smart Meter Device
local SE=domoticz.devices("Zonnepanelen") -- Virtual Electricity device (usage, counter) in which the actual Solar Output will be stored
local SV=domoticz.devices("Stroom Verbruik") -- Virtual Electricity devie (usage, counter) in which the actual power consumption will be stored
local OT=domoticz.devices("Omvormer Temperatuur") -- Virtual Temperature device, in which the inverter temperature will be stored
local usage=SM.usage
local usagedelivered=SM.usageDelivered
domoticz.log ("Usage = "..usage ..", Delivered = "..usagedelivered..", P1 cumulative="..SM.usage1+SM.usage2-SM.return1-SM.return2)
-- convert JSON object to readable format
local json = domoticz.utils.fromJSON(result)
-- log timestamp for debugging purposes
local Time=require('Time')
domoticz.log("Timestamp inverter = "..json.timestamp..", which is "..Time(json.timestamp).secondsAgo.." second(s) ago")
-- update SolarEdge virtual sensors
domoticz.log("SE.updateElectricity("..json.dc_power..", "..json.total_production..")")
SE.updateElectricity(json.dc_power, json.total_production)
domoticz.log("OT.updateTemperature("..json.temperature..")")
OT.updateTemperature(json.temperature)
-- calculate real energy consumption
local virtuelemeterstand=json.total_production+SM.usage1+SM.usage2-SM.return1-SM.return2
local vermogen=SM.usage-SM.usageDelivered+json.dc_power
-- update real actual usage sensor
domoticz.log("SV.updateElectricity("..vermogen..","..virtuelemeterstand..")")
SV.updateElectricity(vermogen,virtuelemeterstand)
else
domoticz.log('failure with io.popen')
end
end
}
have fun! let me know if you like it.
Thanks for the script!
Shouldn't AC power being used instead of DC to calculate the real consumption, because this is the power that is released to the 'site'?
Re: Real power consumption
Posted: Monday 23 September 2019 22:03
by akamming
th3snip3r wrote: ↑Monday 23 September 2019 20:22
Shouldn't AC power being used instead of DC to calculate the real consumption, because this is the power that is released to the 'site'?
You're probably right. Never noticed…
Re: Real power consumption
Posted: Tuesday 24 September 2019 9:08
by freijn
The Sunspec script works perfectly here as well.
When implementing the script I found that on a cloudy day the sun goes "off and on" many times which messed up the power consumtion calculation.
As the P1 meter is spitting the output every 3 seconds I have used that update event to trigger the script to retrieve the Solar current and do the calculation as Grid power (-or +) solar power = house consumption.
Sometimes with huge dark clouds I get a mis-reading overall working great.
Re: Real power consumption
Posted: Friday 17 January 2020 13:53
by boskant
Hi olivierf,
I used your code to create my own version of it.
The usage reading of the SM was not working on my side, the reason is unclear to me, maybe because I have a 3 fase meter.
So I created my own usage and delivery.
thanks for your script.
Code: Select all
-- sourse code is from : https://www.domoticz.com/forum/viewtopic.php?t=16440#p224533
-- Modified bcause of faulty reading of P1 values.
----------------------------------------------------------------------------------------------------------
-- Domoticz IDX and names of the needed devices
----------------------------------------------------------------------------------------------------------
-- Script parameters
----------------------------------------------------------------------------------------------------------
Delivery_L1 = 0 -- in Watt hours
Delivery_L2 = 0 -- in Watt hours
Delivery_L3 = 0 -- in Watt hours
Usage_L1 = 0 -- in Watt hours
Usage_L2 = 0 -- in Watt hours
Usage_L3 = 0 -- in Watt hours
CalculatedDelivered = 0 -- in Watt hours
CalculatedUsage = 0 -- in Watt hours
----------------------------------------------------------------------------------------------------------
Debug = "Yes" -- Turn debugging on ("YES") or off ("NO")
----------------------------------------------------------------------------------------------------------
-- CommandArray
----------------------------------------------------------------------------------------------------------
commandArray = {}
local function update(idx, value1, value2)
local cmd = string.format("%d|0|%.2f;%.2f", idx, value1, value2)
table.insert (commandArray, { ['UpdateDevice'] = cmd } )
end
if (devicechanged["Slimme Meter"]) then
if not json then
--print("STATE: Loading JSON")
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
end -- end if : json
local handle = io.popen('/usr/bin/perl /home/pi/SolarEdge2.perl -m=0 -j 192.168.0.3 || echo error')
local result = handle:read("*a")
handle:close()
-- For debugging purposes: Log result
if (debug) then
print ("This is the input from Solaredge "..result)
end -- end if : Debug
if(result ~= 'error\n') then
-- no error: declare needed opbjects
-- First convert JSON object to readable format
local json = json:decode(result)
--data from P1(usage1, usage2, return1, return2, cons, cons)
usage1, usage2, return1, return2, usage, usageDelivered = otherdevices_svalues["Slimme Meter"]:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")
local SE=otherdevices["Zonnepanelen"] -- Virtual Electricity device (usage, counter) in which the actual Solar Output will be stored
local SV=otherdevices["Stroom Verbruik"] -- Virtual Electricity device (usage, counter) in which the actual power consumption will be stored
local OT=otherdevices["Omvormer Temperatuur"] -- Virtual Temperature device, in which the inverter temperature will be stored
local Delivery_L1 = otherdevices["Delivery L1"] -- Delivered power in Fase L1 created by P1 meter
local Delivery_L2 = otherdevices["Delivery L2"] -- Delivered power in Fase L2 created by P1 meter
local Delivery_L3 = otherdevices["Delivery L3"] -- Delivered power in Fase L3 created by P1 meter
local Usage_L1 = otherdevices["Usage L1"] -- Used power in Fase L1 created by P1 meter
local Usage_L2 = otherdevices["Usage L2"] -- Used power in Fase L2 created by P1 meter
local Usage_L3 = otherdevices["Usage L3"] -- Used power in Fase L3 created by P1 meter
CalculatedDelivered=Delivery_L1+Delivery_L2+Delivery_L3
CalculatedUsage=Usage_L1+Usage_L2+Usage_L3
-- For debugging purposes: Log result
if (debug) then
print ("SM.usage1 = "..usage1.." SM.usage2 = "..usage2.." SM.return1 = "..return1.." SM.return2 = "..return2.." Current Power Usage = "..usage.." Current Power Delivery = "..usageDelivered)
print ("Usage L1 = "..Usage_L1.." Usage L2 = "..Usage_L2.." Usage L3 = "..Usage_L3.." Usage L1+L2+L3 = "..CalculatedUsage)
print ("Delivery L1 = "..Delivery_L1.." Delivery L2 = "..Delivery_L2.." Delivery L3 = "..Delivery_L3.." Total L1+L2+L3 = "..CalculatedDelivered)
print ("Total Solar Production = "..json.total_production.." Current Solar Power = "..json.ac_power)
end -- end if : Debug
-- log timestamp for debugging purposes
--local Time=require('Time')
--print ("Timestamp inverter = "..json.timestamp..", which is "..Time(json.timestamp).secondsAgo.." second(s) ago")
-- update SolarEdge virtual sensors
print ("Zonnepanelen("..json.ac_power..", "..json.total_production..")")
update(otherdevices_idx["Zonnepanelen"],json.ac_power,json.total_production)
print ("Omvormer Temperatuur("..json.temperature..")")
update(otherdevices_idx["Omvormer Temperatuur"],json.temperature,0)
-- calculate real energy consumption
local virtuelemeterstand=json.total_production+usage1+usage2-return1-return2
--local vermogen=usage-usageDelivered+json.ac_power
local vermogen=CalculatedUsage-CalculatedDelivered+json.ac_power
-- update real actual usage sensor
print("Stroom Verbruik("..vermogen..","..virtuelemeterstand..")")
update(otherdevices_idx["Stroom Verbruik"],vermogen,virtuelemeterstand)
end -- end if : result
end --end if : devicechanged
return commandArray