Logic Machine Forum
arithmetic on local - error - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: arithmetic on local - error (/showthread.php?tid=4387)



arithmetic on local - error - sx3 - 16.11.2022

I'm trying to subtract numbers from a 4 Byte Signed Int but keep getting this error.
Can't figure out how to do the calculation without getting errors.

I have 2 meters in series, so I want to down count Wh that have been measured twice.


Quote:Energimätare Timme 16.11.2022 20:09:30
User script:14: attempt to perform arithmetic on local 'strHuset1' (a nil value)
stack traceback:


Code:
-- Begär avläsning från statistikmätaren för Bergvärmen
grp.read('7/1/9')

-- Vänta 5 sekunder på resultat från mätaren
os.sleep(5)

-- Läs in värdet och omvandla kWh till Wh på Bergvärmemätningen
local strBVP = grp.getvalue('7/1/9')/1000

-- Läs in värdet från Husförbrukningen i Wh
local res1, strHuset1 = grp.getvalue('7/0/22')

-- Räkna av Bergvärmen från husförbrukningen
local res2, strHuset2 = strHuset1 - strBVP
log(res1, res2)

grp.write('7/4/1', strBVP)
grp.write('7/0/23', strHuset1)
grp.write('7/0/24', strHuset2)
grp.write('7/4/3', true)
os.sleep(20)
grp.write('7/4/3', false)



RE: arithmetic on local - error - admin - 17.11.2022

Use this:
Code:
-- Läs in värdet från Husförbrukningen i Wh
local strHuset1 = grp.getvalue('7/0/22')

-- Räkna av Bergvärmen från husförbrukningen
local strHuset2 = strHuset1 - strBVP

Lua multiple assignment works this way:
Code:
local a, b, c = 1, 2, 3
log(a, b, c) -- a = 1, b = 2, c = 3



RE: arithmetic on local - error - sx3 - 17.11.2022

Ahh alright, so my mistake is that I had the res for logging before the variable?
So in theory, I wouldn't need the "res" if I want to log a value? Would be enough to just "log(strHuset)" if I want to monitor the value?

Cool, learned something new once again. Thank you!


RE: arithmetic on local - error - admin - 17.11.2022

res is not needed, the first value in the assignment will have the object value.
Code:
local strHuset1 = grp.getvalue('7/0/22')
log(strHuset1)

Some functions may return multiple values. Many of them use "res, err" form where res is the resulting value and err is the error text (only in case of an error, res is usually nil then).