LUA Script Error

Moderator: leecollings

Post Reply
Andree
Posts: 74
Joined: Wednesday 07 June 2017 11:19
Target OS: Windows
Domoticz version:
Contact:

LUA Script Error

Post by Andree »

Hi @all
i've find this Lua script (sorry I don't remember where) for summing consumption from each sensor in one dummy sensor.
This script work fine but sometimes (every minutes) generate an error in LOG on the line 180.
Someone can help me?

Script

Code: Select all

----------------------------------------------------------------------------------------------------------
-- Domoticz IDX and names of the needed devices
----------------------------------------------------------------------------------------------------------
local DeviceName1 = "Consumo Luce Specchio Bagno"       
local DeviceName2 = "Consumo Luce Bagno"       
local DeviceName3 = "Consumo Luci Scale"       
local DeviceName4 = "Consumo Luci Cucina"
local DeviceName5 = "Consumo Luci Parete Sala"
local DeviceName6 = "Consumo Lampada Sale"
local DeviceName7 = "Consumo Luci Camera"
local DeviceName8 = "Consumo Frigo"
local DeviceName9 = "Consumo Lavatrice"
local DeviceName10 = "Consumo Lavastoviglie"
local DeviceName11 = "Consumo HT Camera"
local DeviceName12 = "Consumo TV Sala"
local DeviceName13 = "Consumo HT Sala"
local DeviceName14 = "Consumo Cam Frigo"
local DeviceName15 = "Consumo Cam Pianta"
local DeviceName16 = "Consumo Cam Stufa"

local ConsumptionIDX = 538           -- IDX of the energy device that shows calculated Consumption
local ConsumptionDeviceName = "Consumo Totale"       -- Name of the energy device that shows calculated Consumption
----------------------------------------------------------------------------------------------------------
-- Script parameters
----------------------------------------------------------------------------------------------------------
Energy1 = 0 -- in Watt hours
Power1 = 0    -- in Watts
Energy2 = 0   -- in Watt hours
Power2 = 0   -- in Watts
Energy3 = 0 -- in Watt hours
Power3 = 0    -- in Watts
Energy4 = 0   -- in Watt hours
Power4 = 0   -- in Watts
Energy5 = 0 -- in Watt hours
Power5 = 0    -- in Watts
Energy6 = 0   -- in Watt hours
Power6 = 0   -- in Watts
Energy7 = 0   -- in Watt hours
Power7 = 0   -- in Watts
Energy8 = 0   -- in Watt hours
Power8 = 0   -- in Watts
Energy9 = 0   -- in Watt hours
Power9 = 0   -- in Watts
Energy10 = 0   -- in Watt hours
Power10 = 0   -- in Watts
Energy11 = 0   -- in Watt hours
Power11 = 0   -- in Watts
Energy12 = 0   -- in Watt hours
Power12 = 0   -- in Watts
Energy13 = 0   -- in Watt hours
Power13 = 0   -- in Watts
Energy14 = 0   -- in Watt hours
Power14 = 0   -- in Watts
Energy15 = 0   -- in Watt hours
Power15 = 0   -- in Watts
Energy16 = 0   -- in Watt hours
Power16 = 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 = {}
   Power1, Energy1 = otherdevices_svalues[DeviceName1]:match("([^;]+);([^;]+)")
   if Debug=="YES" then
      print("  ----- ".. DeviceName1 .. "Power = " .. Power1 .. " W");
      print("  ----- ".. DeviceName1 .. "Energy = " .. Energy1 .. " Wh");
   end
   
   Power2, Energy2 = otherdevices_svalues[DeviceName2]:match("([^;]+);([^;]+)")
   if Debug=="YES" then
      print("  ----- ".. DeviceName2 .. "Power = " .. Power2 .. " W");
      print("  ----- ".. DeviceName2 .. "Energy = " .. Energy2 .. " Wh");
   end
 
   Power3, Energy3 = otherdevices_svalues[DeviceName3]:match("([^;]+);([^;]+)")
   if Debug=="YES" then
      print("  ----- ".. DeviceName3 .. "Power = " .. Power3 .. " W");
      print("  ----- ".. DeviceName3 .. "Energy = " .. Energy3 .. " Wh");
   end
   
   Power4, Energy4 = otherdevices_svalues[DeviceName4]:match("([^;]+);([^;]+)")
   if Debug=="YES" then
      print("  ----- ".. DeviceName4 .. "Power = " .. Power4 .. " W");
      print("  ----- ".. DeviceName4 .. "Energy = " .. Energy4 .. " Wh");
   end
 
   Power5, Energy5 = otherdevices_svalues[DeviceName5]:match("([^;]+);([^;]+)")
   if Debug=="YES" then
      print("  ----- ".. DeviceName5 .. "Power = " .. Power5 .. " W");
      print("  ----- ".. DeviceName5 .. "Energy = " .. Energy5 .. " Wh");
   end
 
   Power6, Energy6 = otherdevices_svalues[DeviceName6]:match("([^;]+);([^;]+)")
   if Debug=="YES" then
      print("  ----- ".. DeviceName6 .. "Power = " .. Power6 .. " W");
      print("  ----- ".. DeviceName6 .. "Energy = " .. Energy6 .. " Wh");
   end

   Power7, Energy7 = otherdevices_svalues[DeviceName7]:match("([^;]+);([^;]+)")
   if Debug=="YES" then
      print("  ----- ".. DeviceName7 .. "Power = " .. Power7 .. " W");
      print("  ----- ".. DeviceName7 .. "Energy = " .. Energy7 .. " Wh");
   end
 
   Power8, Energy8 = otherdevices_svalues[DeviceName8]:match("([^;]+);([^;]+)")
   if Debug=="YES" then
      print("  ----- ".. DeviceName8 .. "Power = " .. Power8 .. " W");
      print("  ----- ".. DeviceName8 .. "Energy = " .. Energy8 .. " Wh");
   end
 
   Power9, Energy9 = otherdevices_svalues[DeviceName9]:match("([^;]+);([^;]+)")
   if Debug=="YES" then
      print("  ----- ".. DeviceName9 .. "Power = " .. Power9 .. " W");
      print("  ----- ".. DeviceName9 .. "Energy = " .. Energy9 .. " Wh");
   end
 
   Power10, Energy10 = otherdevices_svalues[DeviceName10]:match("([^;]+);([^;]+)")
   if Debug=="YES" then
      print("  ----- ".. DeviceName10 .. "Power = " .. Power10 .. " W");
      print("  ----- ".. DeviceName10 .. "Energy = " .. Energy10 .. " Wh");
   end

   Power11, Energy11 = otherdevices_svalues[DeviceName11]:match("([^;]+);([^;]+)")
   if Debug=="YES" then
      print("  ----- ".. DeviceName11 .. "Power = " .. Power11 .. " W");
      print("  ----- ".. DeviceName11 .. "Energy = " .. Energy11 .. " Wh");
   end
   
   Power12, Energy12 = otherdevices_svalues[DeviceName12]:match("([^;]+);([^;]+)")
   if Debug=="YES" then
      print("  ----- ".. DeviceName12 .. "Power = " .. Power12 .. " W");
      print("  ----- ".. DeviceName12 .. "Energy = " .. Energy12 .. " Wh");
   end
 
 Power13, Energy13 = otherdevices_svalues[DeviceName13]:match("([^;]+);([^;]+)")
   if Debug=="YES" then
      print("  ----- ".. DeviceName13 .. "Power = " .. Power13 .. " W");
      print("  ----- ".. DeviceName13 .. "Energy = " .. Energy13 .. " Wh");
   end
   
   Power14, Energy14 = otherdevices_svalues[DeviceName14]:match("([^;]+);([^;]+)")
   if Debug=="YES" then
      print("  ----- ".. DeviceName14 .. "Power = " .. Power14 .. " W");
      print("  ----- ".. DeviceName14 .. "Energy = " .. Energy14 .. " Wh");
   end
   
   Power15, Energy15 = otherdevices_svalues[DeviceName15]:match("([^;]+);([^;]+)")
   if Debug=="YES" then
      print("  ----- ".. DeviceName15 .. "Power = " .. Power15 .. " W");
      print("  ----- ".. DeviceName15 .. "Energy = " .. Energy15 .. " Wh");
   end
   
   Power16, Energy16 = otherdevices_svalues[DeviceName16]:match("([^;]+);([^;]+)")
   if Debug=="YES" then
      print("  ----- ".. DeviceName16 .. "Power = " .. Power16 .. " W");
      print("  ----- ".. DeviceName16 .. "Energy = " .. Energy16 .. " Wh");
   end
   
   -- Calculate consumption
   PowerConsumption = Power1 + Power2 + Power3 + Power4 + Power5 + Power6 + Power7 + Power8 + Power9 + Power10 + Power11 + Power12 + Power13 + Power14 + Power15 + Power16
   if Debug=="YES" then
      print("  ----- PowerConsumption = " .. PowerConsumption .. " W");
   end
   EnergyConsumption = Energy1 + Energy2 + Energy3 + Energy4 + Energy5 + Energy6 + Energy7 + Energy8 + Energy9 + Energy10 + Energy11 + Energy12 +Energy13 + Energy14 + Energy15 + Energy16
   if Debug=="YES" then
      print("  ----- EnergyConsumption = " .. EnergyConsumption .. " Wh");
   end
 
   -- Update comsumption device in Domoticz
   if devicechanged[DeviceName1] or devicechanged[DeviceName2] or devicechanged[DeviceName3] or devicechanged[DeviceName4] or devicechanged[DeviceName5] or devicechanged[DeviceName6] or devicechanged[DeviceName7] or devicechanged[DeviceName8] or devicechanged[DeviceName9] or devicechanged[DeviceName10] or devicechanged[DeviceName11] or devicechanged[DeviceName12] or devicechanged[DeviceName13] or devicechanged[DeviceName14] or devicechanged[DeviceName15] or devicechanged[DeviceName16] then
      update(ConsumptionDeviceName, ConsumptionIDX, PowerConsumption, EnergyConsumption,1)
   end
 
return commandArray
Error

Code: Select all

2017-09-06 12:13:00.144 Error: EventSystem: in Consumo Totale: [string "---------------------------------------------..."]:180: attempt to index global 'devicechanged' (a nil value)
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: LUA Script Error

Post by jvdz »

Set the events to act upon from ALL to DEVICES.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Andree
Posts: 74
Joined: Wednesday 07 June 2017 11:19
Target OS: Windows
Domoticz version:
Contact:

Re: LUA Script Error

Post by Andree »

Thx Work!
vicho
Posts: 6
Joined: Tuesday 09 August 2016 21:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: LUA Script Error

Post by vicho »

Andree wrote: Wednesday 06 September 2017 17:16Thx Work!
Can you share the working one script. I have the same problem and can't solve it.
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: LUA Script Error

Post by jvdz »

It has nothing to do with the script itself. You need to set the trigger correctly in de editor!
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
vicho
Posts: 6
Joined: Tuesday 09 August 2016 21:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: LUA Script Error

Post by vicho »

jvdz wrote: Friday 17 June 2022 21:21 It has nothing to do with the script itself. You need to set the trigger correctly in de editor!
Sorry, but can you give me more info how to do that. I can't find where to set the triger for this script. This is the scrip that I use to compute Shelly 3EM total usage and it work and compute the total energy. But ger this error at some time. I think it trigger on changes of some power or energy device.

Code: Select all

----------------------------------------------------------------------------------------------------------
local DeviceName1 = "EVN P0" 		
local DeviceName2 = "EVN P1" 		
local DeviceName3 = "EVN P2" 		
local ConsumptionIDX = 64  			-- IDX of the energy device that shows calculated Consumption
local ConsumptionDeviceName = "EVN Total" 		-- Name of the energy device that shows calculated Consumption
----------------------------------------------------------------------------------------------------------
-- Script parameters
----------------------------------------------------------------------------------------------------------
Energy1 = 0 -- in Watt hours
Power1 = 0 	-- in Watts
Energy2 = 0	-- in Watt hours
Power2 = 0	-- in Watts
Energy3 = 0 -- in Watt hours
Power3 = 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 = {}
	Power1, Energy1 = otherdevices_svalues[DeviceName1]:match("([^;]+);([^;]+)")
	if Debug=="YES" then
		print("  ----- ".. DeviceName1 .. "Power = " .. Power1 .. " W");
		print("  ----- ".. DeviceName1 .. "Energy = " .. Energy1 .. " Wh");
	end
 
	Power2, Energy2 = otherdevices_svalues[DeviceName2]:match("([^;]+);([^;]+)")
	if Debug=="YES" then
		print("  ----- ".. DeviceName2 .. "Power = " .. Power2 .. " W");
		print("  ----- ".. DeviceName2 .. "Energy = " .. Energy2 .. " Wh");
	end
	
	Power3, Energy3 = otherdevices_svalues[DeviceName3]:match("([^;]+);([^;]+)")
	if Debug=="YES" then
		print("  ----- ".. DeviceName3 .. "Power = " .. Power3 .. " W");
		print("  ----- ".. DeviceName3 .. "Energy = " .. Energy3 .. " Wh");
	end
 
	-- Calculate consumption
	PowerConsumption = Power1 + Power2 + Power3
	if Debug=="YES" then
		print("  ----- PowerConsumption = " .. PowerConsumption .. " W");
	end
	EnergyConsumption = Energy1 + Energy2 + Energy3 
	if Debug=="YES" then
		print("  ----- EnergyConsumption = " .. EnergyConsumption .. " Wh");
	end
 
	-- Update comsumption device in Domoticz
	if devicechanged[DeviceName1] or devicechanged[DeviceName2] or devicechanged[DeviceName3] then
		update(ConsumptionDeviceName, ConsumptionIDX, PowerConsumption, EnergyConsumption, 1)
	end
 
return commandArray
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: LUA Script Error

Post by jvdz »

vicho wrote: Sunday 19 June 2022 9:19 Sorry, but can you give me more info how to do that. I can't find where to set the triger for this script.
As I stated: In the Editor page, where you have edited this script you can define the Event it triggers on:
Set the appropriate event type as shown in the Wiki:
https://www.domoticz.com/wiki/Events#Us ... icz_itself
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
vicho
Posts: 6
Joined: Tuesday 09 August 2016 21:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: LUA Script Error

Post by vicho »

jvdz wrote: Sunday 19 June 2022 9:40 As I stated: In the Editor page, where you have edited this script you can define the Event it triggers on:
Set the appropriate event type as shown in the Wiki:
https://www.domoticz.com/wiki/Events#Us ... icz_itself
Thank you a lot. Finaly I find it :). I never seen this big box for trigerr choose for last 6-7 years of ussing Domoticz :).
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: LUA Script Error

Post by jvdz »

Well it really is important to have a script only trigger with the right event or else your system will start doing things at the wrong times or get pretty busy! ;)
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
vicho
Posts: 6
Joined: Tuesday 09 August 2016 21:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: LUA Script Error

Post by vicho »

jvdz wrote: Sunday 19 June 2022 13:37 Well it really is important to have a script only trigger with the right event or else your system will start doing things at the wrong times or get pretty busy! ;)
This is the only one script on LUA that I use and never seen that I can choose the trigers. Other some scripts that I use are writen on dzVents and all work well.
BTW I start to do all from the beginign before one or maybe two weeks. I update my domoticz (use it from 6-7 years ago) and it crash.
I have lots of mysensors 2,4gz + mysensors 866mhz + 433MHZ RFX (heat pump + UFH for my house + solar pannel for hot water + some energy meters). Now I start to install Shelly 3em and 5x 1kW inverters + 6Kw solar panels + 48V power wall. This project is still in progress.
Alll work well, but i never seen before, that I can to choose the trigers for LUA scripst. Thank you again!

p.s. Sorry for my bad English :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest