Page 1 of 1

LUA script to add up user variable

Posted: Saturday 09 April 2022 18:10
by jotheman
I think I have a really easy question, but I am just to unfamiliar with LUA to get this to work.
Using the script below I want to have an action in to get the user variable "dryer_run_counter" (which I created as a Domoticz-variable) and add 1 to it (count up with one), but I just don't seem to be able to do so. Can someone help with this?

I tried the code "commandArray['Variable:' .. dryer_run_counter]=(tonumber(uservariables[dryer_run_counter]))+1" or "dryer_run_counter = dryer_run_counter+1" but I think I am getting the syntax wrong. Who can help me in getting the syntax right. I found quite some examples, but none of them include the commandArray-bit that Domoticz seems to be using...

Code: Select all

Script = 'Vaatwasser Checker'
Debug = 'Y'

-- Functions
function Debug_msg (msg)
if (Debug == 'Y') then print('>> ' .. Script .. ': '.. msg) end
end

-- Variable
local dryer_status_uservar = 'vaatwasser_status'
local energy_consumption = 'Vaatwasser (SC5 - Wattage Verbruik)' --Name of Z-Wave plug that contains actual consumption of dryer (in Watts)
local dryer_counter_uservar = 'vaatwasser_counter' --Name of the uservariable that will contain the counter that is needed
local dryer_run_counter = tonumber('Vaatwasser_teller')
local idle_minutes = 40 --The amount of minutes the consumption has to stay below the 'consumption_lower' value
local consumption_upper = 10 --If usage is higher than this value (Watts), the dryer has started
local consumption_lower = 5 --If usage is lower than this value (Watts), the dryer is idle for a moment/done washing
local dryer_switch = 'Vaatwassert'
--sWatt, sTotalkWh = otherdevices_svalues[energy_consumption]:match("([^;]+);([^;]+)")
sWatt, sTotalkWh = otherdevices_svalues['Vaatwasser (SC5 - Wattage Verbruik)']
local energy_consumption = otherdevices_svalues['Vaatwasser (SC5 - Wattage Verbruik)']
--local waarde = otherdevices_svalues['Vaatwasser (SC5 - Wattage Verbruik)']
--local waarde = otherdevices_svalues['Server (SC2 - Wattage Verbruik)']
--Debug_msg('Waarde (' ..waarde.. 'W)')
dryer_usage = tonumber(sWatt)

--Debug_msg('Power usage currently (' ..energy_consumption.. 'W)')
    
-- dryer_usage = tonumber(otherdevices_svalues[energy_consumption])
commandArray = {}
--Virtual switch is off, but consumption is higher than configured level, so washing has started
if (dryer_usage > consumption_upper) and uservariables[dryer_status_uservar] == 0 then
commandArray['Variable:' .. dryer_status_uservar]='1'
Debug_msg('Current power usage (' ..dryer_usage.. 'W) is above upper boundary (' ..consumption_upper.. 'W), so vaatwassing has started!')
commandArray['Variable:' .. dryer_counter_uservar]=tostring(idle_minutes)
commandArray['Vaatwassert'] = 'On'
else
--Debug_msg('Current power usage (' ..dryer_usage.. 'W). upper boundary (' ..consumption_upper.. 'W)')
end
--Washing machine is not using a lot of energy, check the counter
if (dryer_usage < consumption_lower) and uservariables[dryer_status_uservar] == 1 then
commandArray['Variable:' .. dryer_counter_uservar]=tostring(math.max(tonumber(uservariables[dryer_counter_uservar]) - 1, 0))
Debug_msg('Current power usage (' ..dryer_usage.. 'W) is below lower boundary (' ..consumption_lower.. 'W), vaatwasser is idle or almost ready')
Debug_msg('Subtracting counter, old value: ' ..uservariables[dryer_counter_uservar].. ' minutes')
commandArray['Droger'] = 'On'
elseif ((uservariables[dryer_counter_uservar] ~= idle_minutes) and uservariables[dryer_status_uservar] == 1) then
commandArray['Variable:' .. dryer_counter_uservar]=tostring(idle_minutes)
commandArray['Vaatwassert'] = 'On'
print('Resetting Vaatwasser Timer')
end
--dryer is done
if ((uservariables[dryer_status_uservar] == 1) and uservariables[dryer_counter_uservar] == 0) then
Debug_msg('Vaatwasser is DONE')
Debug_msg('Current power usage dryer ' ..dryer_usage.. 'W')
Debug_msg('Vaatwasser is done, please go empty it!')
--commandArray['SendNotification']='Droger#De vaatwasser is klaar!#0'
commandArray['Variable:' .. dryer_status_uservar]='0'
commandArray['Vaatwassert'] = 'Off'
--commandArray['Variable:' .. dryer_run_counter]=(tonumber(uservariables[dryer_run_counter]))+1
--dryer_run_counter = dryer_run_counter+1
end
return commandArray