Page 3 of 7

Re: Lua script for uploading energy values to PVoutput

Posted: Friday 06 May 2016 17:14
by markk
Hi

Thanks for your help.

Re your first point, I don't know what you mean by "print PVO_URL". Sorry for being so stupid.

EDIT: Re your second point. Yes, I had the api and sid the wrong way around. The script now runs and connects to PVOutput but no energy generation is uploaded. It is just uploading a value of 0kwh.



PVOutput.png
PVOutput.png (72.93 KiB) Viewed 6565 times

Re: Lua script for uploading energy values to PVoutput

Posted: Tuesday 10 May 2016 15:23
by markk
Hi

I've amended the script slightly to upload my energy used data and, although the daily used total is uploading ok, the Power isn't. This is just showing 0w all the time. My generation is currently being uploaded by PVuploader so power generation is showing on this. I'd love to know how to get the power to show so that I can use Lua for both.

I've attached the sample to show what is being shown and the script i'm using. I'd be grateful if someone could help please.
PVGraph.JPG
PVGraph.JPG (327.39 KiB) Viewed 6550 times
PVscript.JPG
PVscript.JPG (169.55 KiB) Viewed 6550 times

Re: Lua script for uploading energy values to PVoutput

Posted: Tuesday 05 July 2016 10:04
by balans
Hi i`m having difficulty when following the wiki
i use the script_device_calculate_consumption.lua wich looks doing ok on the dashboard doiing its calculations from my solarpanels and p1 meter
the script_time_upload_to_PVoutput.lua however does nothing and gives a error in the log

Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_upload_to_PVoutput.lua: .../domoticz/scripts/lua/script_time_upload_to_PVoutput.lua:90: attempt to index field '?' (a nil value)

when i run the script manual using

pi@minibian:~/domoticz/scripts/lua$ lua script_time_upload_to_PVoutput.lua
lua: script_time_upload_to_PVoutput.lua:83: attempt to index global 'otherdevices_svalues' (a nil value)
stack traceback:
script_time_upload_to_PVoutput.lua:83: in main chunk
[C]: in ?

this same sort of error is also there in

pi@minibian:~/domoticz/scripts/lua$ lua script_device_calculate_consumption.lua
lua: script_device_calculate_consumption.lua:51: attempt to index global 'otherdevices_svalues' (a nil value)
stack traceback:
script_device_calculate_consumption.lua:51: in main chunk
[C]: in ?

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 = "Zonnenpanelen" 		-- Device name of the Generated energy
local EnergyDeviceName = "Power" 		-- Name of the energy device that shows imported and exported energy
local ConsumptionIDX = 15  			-- IDX of the energy device that shows calculated Consumption
local ConsumptionDeviceName = "consumption" 		-- 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 = "NO" 		-- 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
 
	-- Imported Exported
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

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 = "Zonnenpanelen" 	-- Device name of the Generated energy
local ConsumptionDeviceName = "consumption" 	-- Name of the energy device that shows calculated Consumption
local VoltageDeviceName = "Volt uac1"	-- Name of the voltage device that shows voltage of the inverter
local TemperatureDeviceName = "Temperature SMA" 	-- Name of the temperature device that shows outside temperature
 
----------------------------------------------------------------------------------------------------------
-- PVoutput parameters
----------------------------------------------------------------------------------------------------------
local PVoutputApi = "xxxxx" 				-- Your PVoutput api key
local PVoutputSystemID = "xx" 					-- 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 = "NO" 		-- 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.")
	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
 
		-- Consumption
		PowerConsumption, EnergyConsumption = 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 copied the scripting files multiple times with notepad ++ from the wiki just to make sure i didn`t made a copy failure
but i just cant seem to figure this one out with my skills
maybe some package missing

Re: Lua script for uploading energy values to PVoutput

Posted: Tuesday 05 July 2016 14:06
by balans
Just to reply to myself , ifinally found it out it was a IDX of devices i renamed and forgot what it was the Volt uac1 was renamed by myself to Voltuac1SMA and Temparature SMA to TemparatureSMA.
Stupid to overlook but sometimes it is best to step away from it and step in fresh after a walk with the dog

Re: Lua script for uploading energy values to PVoutput

Posted: Tuesday 05 July 2016 17:32
by balans
Ha i spoke to soon the upload thing is working but when i run the calculation script there still is this error

lua script_device_calculate_consumption.lua
lua: script_device_calculate_consumption.lua:51: attempt to index global 'otherdevices_svalues' (a nil value)
stack traceback:
script_device_calculate_consumption.lua:51: in main chunk
[C]: in ?


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


it`s the line with -> PowerGeneration, EnergyGeneration..........

wich what i think is connected to

local GenerationDeviceName = "Zonnenpanelen" -- Device name of the Generated energy
local EnergyDeviceName = "Power" -- Name of the energy device that shows imported and exported energy
local ConsumptionIDX = 15 -- IDX of the energy device that shows calculated Consumption
local ConsumptionDeviceName = "consumption" -- Name of the energy device that shows calculated Consumption

wich i think is correct so i`m open for suggestions

ps here my link to pv

http://pvoutput.org/intraday.jsp?id=51272&sid=46635

Re: Lua script for uploading energy values to PVoutput

Posted: Saturday 17 September 2016 23:30
by Derik
mmm
Strange thing happend...

I use the simple script to upload my winddelen tot pvo:

Code: Select all

commandArray = {}


 print ("Winddelen naar P.V.O.")

        date = os.date("*t")
        if (date.min % 10 == 0) then
            solar = otherdevices_svalues[' 3x Jonge Held']
            now, total = solar:match("(%d+);(%d+)")
        --  print(solar..' '..now..' '..total)

        -- Upload data to PVoutput every 10 mins
            baseURL = "http://pvoutput.org/service/r2/addstatus.jsp?"
            SID = "15962"
            API = "c53c834f1af40035b0ea535"
            PVO_URL = baseURL .. "sid=" .. SID .. "&key=" .. API .. "&d=" .. os.date("%Y%m%d") .. "&t=" .. os.date("%H:%M")
            PVO_URL = PVO_URL .. "&v1=" .. total .. "&v2=" .. now .. "&c1=1"
            --print(PVO_URL)
            commandArray['OpenURL'] = PVO_URL

        end
        
return commandArray
Works good...
Only i will try to upload my gasmeter from domoticz [p1 ] with this script.
Strange but only this is not working.
Why is the P1 [ just the same counter in domoicz ] not upload the data?

Re: Lua script for uploading energy values to PVoutput

Posted: Saturday 17 September 2016 23:46
by Derik
Other thing...

I use the P1 usb....
Can i use the code: http://www.domoticz.com/wiki/Upload_ene ... m_Domoticz

Because there are two devices.
Import and export... devices
ScreenShot017.jpg
ScreenShot017.jpg (31.07 KiB) Viewed 6348 times
Only when i use the usb i do have only 1 IDX.....

Re: Lua script for uploading energy values to PVoutput

Posted: Thursday 29 September 2016 20:50
by Derik
bump please....

I am searching for a simple script to upload my P1 meter in seperate data to Pvo [ PVO i have multile sids ]
So a day /night use script
a day / night deliver script
and the gas ...

Is there some that have this simple script working

Thanks

Re: Lua script for uploading energy values to PVoutput

Posted: Wednesday 05 October 2016 20:26
by Derik
please??

There is something strange with the lua script with the P1 smart meter.
When i replace the script setting then i mis the Pvouput api...?
So i do not get this cript working..

Some one have a working P1 example for me....

Re: Lua script for uploading energy values to PVoutput

Posted: Sunday 16 October 2016 16:54
by Derik
nobody....
I am realy stuck with this script there are things that are not correct... [ i think ]+

Code: Select all

Replace
	-- Imported
	PowerImport, EnergyImport = otherdevices_svalues[ImportedDeviceName]:match("([^;]+);([^;]+)")
	if Debug=="YES" then
		print("  ----- PowerImport = " .. PowerImport .. " W");
		print("  ----- EnergyImport = " .. EnergyImport .. " Wh");
	end
 
	-- Exported
	PowerExport, EnergyExport = otherdevices_svalues[ExportedDeviceName]:match("([^;]+);([^;]+)")
	if Debug=="YES" then
		print("  ----- PowerExport = " .. PowerExport .. " W");
		print("  ----- EnergyExport = " .. EnergyExport .. " Wh");
	end
Not to find in the code:

Code: Select all

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
 
		-- Consumption
		PowerConsumption, EnergyConsumption = 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
And more of this problems...
simple read the P1 wit generation and use in 1 sid
as a option the gas... other sid

Please...

Re: Lua script for uploading energy values to PVoutput

Posted: Sunday 20 November 2016 13:49
by videodrome
Hi
i edited the first script "script_device_calculate_consumption.lua" with notpad + and charged in the scripts/lua of Domoticz, but it gives me a message in the log
9: unexpected symbol near char(194)

what's wrong in my script?

Re: Lua script for uploading energy values to PVoutput

Posted: Sunday 20 November 2016 14:01
by jvdz
Did you change the encoding of the file with your editor?

Jos

Re: Lua script for uploading energy values to PVoutput

Posted: Sunday 20 November 2016 16:22
by videodrome
jvdz wrote:Did you change the encoding of the file with your editor?

Jos
i saved the file as lua script. Now i tried to use Luaedit 2010 and make a debug. No more "unexpected symbol" message, but:

Code: Select all

22: module 'socket.http' not found:
no field package.preload['socket.http']
no file '/usr/local/share/lua/5.2/socket/http.lua'
no file '/usr/local/share/lua/5.2/socket/http/init.lua'
no file '/usr/local/lib/lua/5.2/socket/http.lua'
no file '/usr/local/lib/lua/5.2/socket/http/init.lua'
no file './socket/http.lua'
no file '/usr/local/lib/lua/5.2/socket/http.so'
no file '/usr/local/lib/lua/5.2/loadall.so'
no file './socket/http.so'
no file '/usr/local/lib/lua/5.2/socket.so'
no file '/usr/local/lib/lua/5.2/loadall.so'
no file './socket.so' 
I have installed Lua 5.1.5

Re: Lua script for uploading energy values to PVoutput

Posted: Thursday 24 November 2016 1:14
by videodrome
videodrome wrote:
jvdz wrote:Did you change the encoding of the file with your editor?

Jos
i saved the file as lua script. Now i tried to use Luaedit 2010 and make a debug. No more "unexpected symbol" message, but:

Code: Select all

22: module 'socket.http' not found:
no field package.preload['socket.http']
no file '/usr/local/share/lua/5.2/socket/http.lua'
no file '/usr/local/share/lua/5.2/socket/http/init.lua'
no file '/usr/local/lib/lua/5.2/socket/http.lua'
no file '/usr/local/lib/lua/5.2/socket/http/init.lua'
no file './socket/http.lua'
no file '/usr/local/lib/lua/5.2/socket/http.so'
no file '/usr/local/lib/lua/5.2/loadall.so'
no file './socket/http.so'
no file '/usr/local/lib/lua/5.2/socket.so'
no file '/usr/local/lib/lua/5.2/loadall.so'
no file './socket.so' 
I have installed Lua 5.1.5
anyone?

Re: Lua script for uploading energy values to PVoutput

Posted: Thursday 24 November 2016 18:12
by jvdz
You need to install these LUA libraries. They are also used by DTGBOT and can find them here: http://www.domoticz.com/forum/viewtopic.php?f=21&t=7279

Jos

Re: Lua script for uploading energy values to PVoutput

Posted: Thursday 08 December 2016 22:49
by videodrome
jvdz wrote:You need to install these LUA libraries. They are also used by DTGBOT and can find them here: http://www.domoticz.com/forum/viewtopic.php?f=21&t=7279

Jos
Installed the libraries. Now the problem is:

Code: Select all

568 Error: EventSystem:  in /home/pi/domoticz/scripts/lua/script_device_calculate_consumption.lua: ...ticz/scripts/lua/script_device_calculate_consumption.lua:68: attempt to perform arithmetic on global 'PowerImport' (a nil value) 
This is my script
The line 68 is

-- Calculate consumption
PowerConsumption = PowerGeneration + PowerImport - PowerExport

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 = "Produzione Fv istantanea" 		-- Device name of the Generated energy
local ImportedDeviceName = "Energia Prelevata" 		-- Name of the energy device that shows imported energy
local ExportedDeviceName = "Energia Immessa" 		-- Name of the energy device that shows exported energy
local ConsumptionIDX = 46  			-- IDX of the energy device that shows calculated Consumption
local ConsumptionDeviceName = "Autoconsumo" 		-- Name of the energy device that shows calculated Consumption

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

----------------------------------------------------------------------------------------------------------
-- Script parameters
----------------------------------------------------------------------------------------------------------
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 = "NO" 		-- 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

	-- Imported
	PowerImport, EnergyImport = otherdevices_svalues[ImportedDeviceName]:match("([^;]+);([^;]+)")
	if Debug=="YES" then
		print("  ----- PowerImport = " .. PowerImport .. " W");
		print("  ----- EnergyImport = " .. EnergyImport .. " Wh");
	end

	-- Exported
	PowerExport, EnergyExport = otherdevices_svalues[ExportedDeviceName]:match("([^;]+);([^;]+)")
	if Debug=="YES" then
		print("  ----- PowerExport = " .. PowerExport .. " W");
		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[ImportedDeviceName] then
		update(ConsumptionDeviceName, ConsumptionIDX, PowerConsumption, EnergyConsumption, 1)
	end
	
return commandArray
The sensor n. 40 is PowerImport
Image

Re: Lua script for uploading energy values to PVoutput

Posted: Thursday 08 December 2016 22:57
by jvdz
Seems a nil is returned for ImportedDeviceName = "Energia Prelevata" by this line:

Code: Select all

   -- Imported
   PowerImport, EnergyImport = otherdevices_svalues[ImportedDeviceName]:match("([^;]+);([^;]+)")
Is the devices name correct?
It could also help to set debug to yes so some information is shown while testing.

Jos

Re: Lua script for uploading energy values to PVoutput

Posted: Thursday 08 December 2016 23:04
by videodrome
jvdz wrote:Seems a nil is returned for ImportedDeviceName = "Energia Prelevata" by this line:

Code: Select all

   -- Imported
   PowerImport, EnergyImport = otherdevices_svalues[ImportedDeviceName]:match("([^;]+);([^;]+)")
Is the devices name correct?
It could also help to set debug to yes so some information is shown while testing.

Jos
As you see in the image the name sensor n. 40 is "Energia Prelevata" the same in the script. The sensor read the data in Kwh and the script is in watt....

How can i set the debug?

Re: Lua script for uploading energy values to PVoutput

Posted: Friday 09 December 2016 9:01
by jvdz
Ah yes, I see the device in the image. I also see that it has only one value and think that statement expect 2 values separated by a semicolon (;). Hope somebody can help that knows what should be expected here as I do not know the device.
Debugging is set by changing the value of the variable debug at the top to "YES":

Code: Select all

Debug = "NO"       -- Turn debugging on ("YES") or off ("NO")
Jos

Re: Lua script for uploading energy values to PVoutput

Posted: Saturday 06 May 2017 0:49
by R0yk3
Hi,
I hope somebody can help me.
I read the complete forum post, but i'm fairly a newbie.
i get this error

Code: Select all

2017-05-06 00:42:18.998 LUA: ----- PowerGeneration = 0 W
2017-05-06 00:42:18.998 LUA: ----- EnergyGeneration = 0.0 Wh
2017-05-06 00:42:18.998 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_calculate_consumption.lua: ...ticz/scripts/lua/script_device_calculate_consumption.lua:58: attempt to perform arithmetic on global 'EnergyImportLow' (a nil value)
where i have in the script:

Code: Select all

local GenerationDeviceName = "Elektra" 		-- My P1 output
local EnergyDeviceName = "Elektra" 		-- My p1 output
local ConsumptionIDX = "198"  			-- IDX of the dummy switch
local ConsumptionDeviceName = "Elektra_verbruik" 		-- Name of the dummy switch
if i change the generation device to my pv, he values change to:

Code: Select all

2017-05-06 00:40:24.136 LUA: ----- PowerGeneration = 0.000 W
2017-05-06 00:40:24.136 LUA: ----- EnergyGeneration = 0.000 Wh
Where am i misreading the lott..??