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
Real consumption
Moderator: leecollings
-
- Posts: 112
- Joined: Sunday 20 May 2018 12:56
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Stable
- Location: NL
- Contact:
Real consumption
Last edited by Knibor on Friday 10 May 2019 8:27, edited 1 time in total.
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: Real consumption
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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
- Posts: 112
- Joined: Sunday 20 May 2018 12:56
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Stable
- Location: NL
- Contact:
Re: Real consumption
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"
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"
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: Real consumption
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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
- Posts: 112
- Joined: Sunday 20 May 2018 12:56
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Stable
- Location: NL
- Contact:
Re: Real consumption
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?
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?
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: Real consumption
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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
- Posts: 112
- Joined: Sunday 20 May 2018 12:56
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Stable
- Location: NL
- Contact:
Re: Real consumption
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?
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?
- Attachments
-
- 254.png (254.44 KiB) Viewed 1756 times
-
- 84.png (199.3 KiB) Viewed 1758 times
-
- Blockly.png (201.64 KiB) Viewed 1759 times
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: Real consumption
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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
- Posts: 112
- Joined: Sunday 20 May 2018 12:56
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Stable
- Location: NL
- Contact:
Re: Real consumption
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!
Who is online
Users browsing this forum: No registered users and 1 guest