This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

arithmetic on local - error
#1
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)
Reply
#2
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
Reply
#3
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!
Reply
#4
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).
Reply


Forum Jump: