Page 1 of 1
Real consumption
Posted: Thursday 09 May 2019 22:17
by Knibor
Hi, can someone help me with this Lua script, I can't get it to work.
local idx = 254
commandArray = {}
if (devicechanged['Power'] ~= meter) then
meter = devicechanged['Power']
--P1 meter uitsplitsen
power1, power2, power3, power4, power5, power6 = devicechanged['Power']:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")
--Echte verbruik berekenen: verbruik - teruglevering
stroomverbruik = power5 - power6
commandArray[1] = {['UpdateDevice'] = idx .. '|0|' .. stroomverbruik}
end
Re: Real consumption
Posted: Friday 10 May 2019 2:53
by waaren
Knibor wrote: ↑Thursday 09 May 2019 22:17
Could be something like
Code: Select all
commandArray = {}
local meter = 1991 -- idx of virtual counter
local P1 = "Power" -- name of Power device
function stringSplit(text, sep)
local sep = sep or '%s'
local t = {}
for str in string.gmatch(text, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
for device, value in pairs(devicechanged) do
if device == P1 then
local values = stringSplit(value,";")
local used = values[5]
local produced = values[6]
print ( "Used: " .. used .. " Watt",
"Produced: " .. produced .. " Watt")
local result = used - produced
print ("From the grid: " .. result .. " Watt")
commandArray[#commandArray + 1] = {['UpdateDevice'] = meter .. '|0|' .. result}
end
end
return commandArray
Re: Real consumption
Posted: Friday 10 May 2019 10:20
by Knibor
Hi, thanks for the Lua Script.
I have used the virtual sensor "Usage (Electric)" so that I have a readout in Watts.
Is it possible show only the positive values, so the values only when I deliver?
When I have "usage" it can stay on "0"
Re: Real consumption
Posted: Friday 10 May 2019 10:45
by waaren
Knibor wrote: ↑Friday 10 May 2019 10:20
I have used the virtual sensor "Usage (Electric)" so that I have a readout in Watts.
Is it possible show only the positive values, so the values only when I deliver?
When I have "usage" it can stay on "0"
Sure.
Code: Select all
commandArray = {}
local meter = 1992 -- idx of virtual usage device
local P1 = "Power" -- name of Power device
function stringSplit(text, sep)
local sep = sep or '%s'
local t = {}
for str in string.gmatch(text, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
for device, value in pairs(devicechanged) do
if device == P1 then
local values = stringSplit(value,";")
local used = values[5]
local produced = values[6]
print ( "Used: " .. used .. " Watt",
"Produced: " .. produced .. " Watt")
local result = math.max(produced - used,0)
print ("Sending to the grid: " .. result .. " Watt")
commandArray[#commandArray + 1] = {['UpdateDevice'] = meter .. '|0|' .. result}
end
end
return commandArray
Re: Real consumption
Posted: Friday 10 May 2019 22:42
by Knibor
Hi,
I copy paste the script and only changed the IDX number.
I have an error in the Log file.
2019-05-10 22:04:00.831 Error: EventSystem: in Echte levering: [string "commandArray = {}..."]:15: bad argument #1 to 'pairs' (table expected, got nil)
Dit i do something wrong?
Re: Real consumption
Posted: Friday 10 May 2019 23:09
by waaren
Knibor wrote: ↑Friday 10 May 2019 22:42
Did I do something wrong?
Have you saved it as a Lua device type script ?
Added test for that
Code: Select all
commandArray = {}
local meter = 1992 -- idx of virtual usage device
local P1 = "Power" -- name of Power device
function stringSplit(text, sep)
local sep = sep or '%s'
local t = {}
for str in string.gmatch(text, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
if not devicechanged then
print ("No devices seem to have changed. did you save the script as type device ?")
return commanddArray
end
for device, value in pairs(devicechanged) do
if device == P1 then
local values = stringSplit(value,";")
local used = values[5]
local produced = values[6]
print ( "Used: " .. used .. " Watt",
"Produced: " .. produced .. " Watt")
local result = math.max(produced - used,0)
print ("Sending to the grid: " .. result .. " Watt")
commandArray[#commandArray + 1] = {['UpdateDevice'] = meter .. '|0|' .. result}
end
end
return commandArray
Re: Real consumption
Posted: Saturday 11 May 2019 14:29
by Knibor
Hi, Thanks for the example script, but I have a problem to get it work in conjunction with "Blockly"
It has the name "Echte stroom" with IDX 254
When I use this script, I have a readout on a virtual device "Echte stroom" but when I use the virtual name "Echte stroom" in a "Blockley" program, the program will not work.
But when I use the virtual device "Zonne energie" with IDX 84 in the "Blockley" program it works!
With device IDX 84, I measure only the total delivery of the 3 fase, but with IDX 254 the deference of (usage and delivery) of the 3 fase meter.
It this problem to solve?
Re: Real consumption
Posted: Saturday 11 May 2019 15:11
by waaren
Knibor wrote: ↑Saturday 11 May 2019 14:29
It this problem to solve?
I don't see why not but fail to understand why you want to mix three different type of code (Blockley, dzVents and Lua) to handle your requirements.
Happy to help but with a preference for a solution where you use dzVents from start to finish.
So if you can send me a PM with a description of your requirements, physical and virtual sensortypes and switches I can have a look.
PM can be in Dutch and I always share the end-result on the forum to enable other members to cherry-pick from it.
Re: Real consumption
Posted: Sunday 12 May 2019 9:50
by Knibor
Hi, I just send a PM but I can"t see if it send in the Outbox. Maybe it takes some interaction time on the Domoticz server!