problem with some lua scripts.

Moderator: leecollings

Post Reply
sjoemie1985
Posts: 27
Joined: Thursday 26 September 2019 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

problem with some lua scripts.

Post by sjoemie1985 »

i also posted this in an other topic, but when i saw that that topic was solved is made an new topic with my problem so here it is. :)

Good morning,

i have some problems with uploading the right data to pvoutput.org the enrgy used and power Used are continues on 0 in stead of data.
so what am is doing wrong?
i am using the following 2 scripts.

i have also the sems portal plugin in my domoticz.

Device Calculation script.

Code: Select all

-- /home/pi/domoticz/scripts/lua/script_device_calculate_consumption.lua
-- This script collects the values below from Domoticz
--   * The Power and energy values (import and export) from a smartmeter 
--   * The Power and energy values from a Solar power inverter
-- It then calculates the consumed power and energy from the values above with the formula's
--   * EnergyConsumption = EnergyGeneration + EnergyImport - EnergyExport
--   * PowerConsumption = PowerGeneration + PowerImport - PowerExport
-- It then updates a virtual device which displays the consumed power and energy in Domoticz

----------------------------------------------------------------------------------------------------------
-- Domoticz IDX and names of the needed devices
----------------------------------------------------------------------------------------------------------
local GenerationDeviceName = "CurrentOutput" 		-- Device name of the Generated energy
local EnergyDeviceName = "Power" 		-- Name of the energy device that shows imported and exported energy
local ConsumptionIDX = 31  			-- IDX of the energy device that shows calculated Consumption
local ConsumptionDeviceName = "ConsumptionDevice" 		-- Name of the energy device that shows calculated Consumption

----------------------------------------------------------------------------------------------------------
-- Require parameters
----------------------------------------------------------------------------------------------------------
local http = require("socket.http")

----------------------------------------------------------------------------------------------------------
-- Script parameters
----------------------------------------------------------------------------------------------------------
EnergyImportLow = 0	-- in Watt hours
EnergyImportHigh = 0	-- in Watt hours
EnergyExportLow = 0	-- in Watt hours
EnergyExportHigh = 0	-- in Watt hours
EnergyGeneration = 0 	-- in Watt hours
PowerGeneration = 0 	-- in Watts
EnergyImport = 0	-- in Watt hours
PowerImport = 0		-- in Watts
EnergyConsumption = 0 	-- in Watt hours
PowerConsumption = 0 	-- in Watts
Debug = "YES" 		-- Turn debugging on ("YES") or off ("NO")

----------------------------------------------------------------------------------------------------------
-- Lua Functions
----------------------------------------------------------------------------------------------------------
function update(device, id, power, energy, index)
	commandArray[index] = {['UpdateDevice'] = id .. "|0|" .. power .. ";" .. energy}
end 

----------------------------------------------------------------------------------------------------------
-- CommandArray
----------------------------------------------------------------------------------------------------------
commandArray = {}
	-- Generated
	PowerGeneration, EnergyGeneration = otherdevices_svalues[GenerationDeviceName]:match("([^;]+);([^;]+)")
	if Debug=="YES" then
		print("  ----- PowerGeneration = " .. PowerGeneration .. " W");
		print("  ----- EnergyGeneration = " .. EnergyGeneration .. " Wh");
	end

	EnergyImportLow, EnergyImportHigh, EnergyExportLow, EnergyExportHigh, PowerImport, PowerExport = otherdevices_svalues[EnergyDeviceName]:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")
	EnergyImport = EnergyImportLow + EnergyImportHigh
	EnergyExport = EnergyExportLow + EnergyExportHigh
	if Debug=="YES" then
		print("  ----- PowerImport = " .. PowerImport .. " W");
		print("  ----- EnergyImportLow = " .. EnergyImportLow .. " Wh");
		print("  ----- EnergyImportHigh = " .. EnergyImportHigh .. " Wh");
		print("  ----- EnergyImport = " .. EnergyImport .. " Wh");
		print("  ----- PowerExport = " .. PowerExport .. " W");
		print("  ----- EnergyExportLow = " .. EnergyExportLow .. " Wh");
		print("  ----- EnergyExportHigh = " .. EnergyExportHigh .. " Wh");
		print("  ----- EnergyExport = " .. EnergyExport .. " Wh");
	end

	-- Calculate consumption
	PowerConsumption = PowerGeneration + PowerImport - PowerExport
	if Debug=="YES" then
		print("  ----- PowerConsumption = " .. PowerConsumption .. " W");
	end
	EnergyConsumption = EnergyGeneration + EnergyImport - EnergyExport
	if Debug=="YES" then
		print("  ----- EnergyConsumption = " .. EnergyConsumption .. " Wh");
	end

	-- Update comsumption device in Domoticz
	if devicechanged[EnergyDeviceName] then
		update(ConsumptionDeviceName, ConsumptionIDX, PowerConsumption, EnergyConsumption, 1)
	end
	
return commandArray

TIME SCRIPT

Code: Select all

-- /home/pi/domoticz/scripts/lua/script_time_upload_to_PVoutput.lua
-- This script collects the values below from Domoticz
--   * The Power generation, energy generation and voltage from a Solar power inverter
--   * The temperature from a outside temperature sensor
--   * The Power consumption and energy consumption which is calculated in another Lua script 
-- And uploads all of the values to a PVoutput account.
--
-- For more information about PVoutput, see their user documentation on http://www.pvoutput.org/help.html

----------------------------------------------------------------------------------------------------------
-- Domoticz IDX of devices
----------------------------------------------------------------------------------------------------------
local GenerationDeviceName = "PowerOutput"	-- Device name of the Generated energy
local ConsumptionDeviceName = "ConsumptionDevice" 	-- Name of the energy device that shows calculated Consumption
local VoltageDeviceName = "VoltageOutput"	-- Name of the voltage device that shows voltage of the inverter
local TemperatureDeviceName = "Temperatuur" -- Name of the temperature device that shows outside temperature

----------------------------------------------------------------------------------------------------------
-- PVoutput parameters
----------------------------------------------------------------------------------------------------------
local PVoutputApi = "c550e7568ba649922fd015828f********"		-- Your PVoutput api key
local PVoutputSystemID = "699" 					-- Your PVoutput System ID
local PVoutputPostInterval = 5  					-- The number of minutes between posts to PVoutput (normal is 5 but when in donation mode it's max 1)
local PVoutputURL = '://pvoutput.org/service/r2/addstatus.jsp?key='	-- The URL to the PVoutput Service

----------------------------------------------------------------------------------------------------------
-- Require parameters
----------------------------------------------------------------------------------------------------------
local http = require("socket.http")

----------------------------------------------------------------------------------------------------------
-- Script parameters
----------------------------------------------------------------------------------------------------------
EnergyGeneration = 0 	-- v1 in Watt hours
PowerGeneration = 0 	-- v2 in Watts
EnergyConsumption = 0 	-- v3 in Watt hours
PowerConsumption = 0 	-- v4 in Watts
CurrentTemp = 0 	-- v5 in celcius
Voltage = 0 		-- v6 in volts
c1 = 1 			-- c1 = 0 when v1 is today's energy. c1 = 1 when v1 is lifetime energy.
Debug = "YES" 		-- Turn debugging on ("YES") or off ("NO")

----------------------------------------------------------------------------------------------------------
-- Lua Functions
----------------------------------------------------------------------------------------------------------
function UploadToPVoutput(self)
	b, c, h = http.request("http" .. PVoutputURL .. PVoutputApi .. "&sid=".. PVoutputSystemID .. "&d=" .. os.date("%Y%m%d") .. "&t=" .. os.date("%H:%M") .. 

	"&v1=" .. EnergyGeneration .. "&v2=" .. PowerGeneration .. "&v3=" .. EnergyConsumption .. "&v4=" .. PowerConsumption .. "&v5=" .. CurrentTemp .. "&v6=" .. Voltage .. "&c1=" .. c1 )
	if b=="OK 200: Added Status" then
		print(" -- Current status successfully uploaded to PVoutput.org")
	else
		print(" -- " ..b)
	end
	
	print("  --Energy generation (v1) = ".. EnergyGeneration .. " Wh")
	print("  --Power generation (v2) = " .. PowerGeneration .. " W")
	print("  --Energy consumption (v3) = " .. EnergyConsumption .. " Wh")
	print("  --Power consumption (v4) = " .. PowerConsumption .. " W")
	print("  --Current temperature (v5) = " .. CurrentTemp .. " C")
	print("  --Voltage (v6) = " .. Voltage .. "V")
	print("  --Cumulative Flag (c1) = " .. c1 .. "")
end

function update(device, id, power, energy, index)
	commandArray[index] = {['UpdateDevice'] = id .. "|0|" .. power .. ";" .. energy}
end 

----------------------------------------------------------------------------------------------------------
-- CommandArray
----------------------------------------------------------------------------------------------------------
commandArray = {}

	time = os.date("*t")
	if PVoutputPostInterval > 1 then
		TimeToGo = PVoutputPostInterval - (time.min % PVoutputPostInterval)
		print('Time to go before upload to PVoutput: ' ..TimeToGo.. " minutes")
	end
	
	if((time.min % PVoutputPostInterval)==0)then
		-- Generated
		PowerGeneration, EnergyGeneration = otherdevices_svalues[GenerationDeviceName]:match("([^;]+);([^;]+)")
		if Debug=="YES" then
			print(" ---- The total generated energy is " .. EnergyGeneration .. " Wh");
			print(" ---- The current generated power is " .. PowerGeneration .. " W");
		end

		-- Voltage
		Voltage = otherdevices_svalues[VoltageDeviceName] :match("([^;]+)")
		if Debug=="YES" then
			print(" ---- The voltage of the inverter is " .. Voltage .. " V");
		end
		
		-- Temperature
		CurrentTemp = otherdevices_svalues[TemperatureDeviceName]:match("([^;]+)")
		if Debug=="YES" then
			print(" ---- The outside temperature is " .. CurrentTemp .. " C.");
		end
		--EnergyGeneration = "0"
		--PowerGeneration = "0"
		-- Consumption
		EnergyConsumption, PowerConsumption = otherdevices_svalues[ConsumptionDeviceName]:match("([^;]+);([^;]+)")
		if Debug=="YES" then
			print(" ---- The total consumed energy is " .. EnergyConsumption .. " Wh");
			print(" ---- The current consumed power is " .. PowerConsumption .. " W");
		end
		-- Upload data to PVoutput	
		UploadToPVoutput()
	end

return commandArray
I get also the following error in domoticz log
...ticz/scripts/lua/script_device_calculate_consumption.lua:71: attempt to perform arithmetic on global 'PowerGeneration' (a string value)
2019-09-26 10:39:45.976 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_calculate_consumption.lua: ...ticz/scripts/lua/script_device_calculate_consumption.lua:71: attempt to perform arithmetic on global 'PowerGeneration' (a string value)
2019-09-26 10:39:45.995 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_calculate_consumption.lua: ...ticz/scripts/lua/script_device_calculate_consumption.lua:71: attempt to perform arithmetic on global 'PowerGeneration' (a string value)

i also think something is wrong with the energy import high and low.
because the low is higher than the importhigh
2019-09-26 10:44:18.992 Status: LUA: ----- EnergyGeneration = 339500.0 Wh
2019-09-26 10:44:18.992 Status: LUA: ----- PowerImport = 64 W
2019-09-26 10:44:18.992 Status: LUA: ----- EnergyImportLow = 51778 Wh
2019-09-26 10:44:18.992 Status: LUA: ----- EnergyImportHigh = 26840 Wh
2019-09-26 10:44:18.992 Status: LUA: ----- EnergyImport = 78618 Wh
2019-09-26 10:44:18.992 Status: LUA: ----- PowerExport = 0 W
2019-09-26 10:44:18.992 Status: LUA: ----- EnergyExportLow = 39607 Wh
2019-09-26 10:44:18.992 Status: LUA: ----- EnergyExportHigh = 75129 Wh

Image


so what script do i have to use to get the right data to pvoutput.org and what am i doing wrong?
sjoemie1985
Posts: 27
Joined: Thursday 26 September 2019 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: problem with some lua scripts.

Post by sjoemie1985 »

i think i have the sollution.

i placed in my device calculation script 2 times the device name from my P1 meter from domoticz and than i saw information in the dummy don't know if that is okay?
Image

i can't find wich device should be filled in on the xxxxx from these lua scripts.
because i thougt that the generation device name should be my solar panels?
and the energy devicename the name off my P1 power?

Image
the usage i think is the energy that i use from the power net provider?

and delivery is what my solar panels are delivering to the power net?
Thorgal789
Posts: 852
Joined: Wednesday 15 August 2018 14:38
Target OS: -
Domoticz version:
Contact:

Re: problem with some lua scripts.

Post by Thorgal789 »

Code: Select all

2019-09-26 10:39:45.976 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_calculate_consumption.lua: ...ticz/scripts/lua/script_device_calculate_consumption.lua:71: attempt to perform arithmetic on global 'PowerGeneration' (a string value)
line 71 > PowerConsumption = PowerGeneration + PowerImport - PowerExport

Print all value in domoticz logs to check wich one is not an integer, you will find the guilty.
for me it's PowerGeneration, so the problem is from
PowerGeneration, EnergyGeneration = otherdevices_svalues[GenerationDeviceName]:match("([^;]+);([^;]+)")
Perhaps your device "GenerationDeviceName" have not date in good format.
Print the value of PowerGeneration, just after this line, I m sure you haven't an integer value.
sjoemie1985
Posts: 27
Joined: Thursday 26 September 2019 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: problem with some lua scripts.

Post by sjoemie1985 »

Thorgal789 wrote: Thursday 03 October 2019 21:26

Code: Select all

2019-09-26 10:39:45.976 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_calculate_consumption.lua: ...ticz/scripts/lua/script_device_calculate_consumption.lua:71: attempt to perform arithmetic on global 'PowerGeneration' (a string value)
line 71 > PowerConsumption = PowerGeneration + PowerImport - PowerExport

Print all value in domoticz logs to check wich one is not an integer, you will find the guilty.
for me it's PowerGeneration, so the problem is from
PowerGeneration, EnergyGeneration = otherdevices_svalues[GenerationDeviceName]:match("([^;]+);([^;]+)")
Perhaps your device "GenerationDeviceName" have not date in good format.
Print the value of PowerGeneration, just after this line, I m sure you haven't an integer value.
what device is my generation device?
Should that be a device from my solar panels from out of domoticz?
Thorgal789
Posts: 852
Joined: Wednesday 15 August 2018 14:38
Target OS: -
Domoticz version:
Contact:

Re: problem with some lua scripts.

Post by Thorgal789 »

According to the code value are

GenerationDeviceName is something with POWER;ENERGY
EnergyDeviceName is something on the form x;x;x;x;x;x
ConsumptionIDX is something on the form POWER;ENERGY
ConsumptionDeviceName something like POWER;ENERGY

So on my side I will try

GenerationDeviceName > PowerOutput
EnergyDeviceName > Power
ConsumptionIDX > 31
ConsumptionDeviceName > ConsumptionDevice
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest