Page 1 of 1
Newbie question about lua and dzVents
Posted: Saturday 12 October 2024 16:22
by roelvdh
I just started learning lua with dzVents in mind later. For starters I took a very simple problem of reading the value of 2 Domoticz devices, add both values and put the result back into a third (virtual) device, all this running every minute. After reading the docs I assumed the following most basic code would work, but I am obviously missing something and need just a bit of help to get me started.
Code: Select all
commandArray = {}
for deviceName,deviceValue in pairs(otherdevices) do
if (deviceName=='Sessy1 batterijvermogen') or (deviceName=='Sessy2 batterijvermogen') then
commandArray['Batterijvermogen'] = deviceName('Sessy1 batterijvermogen') + deviceName('Sessy2 batterijvermogen')
end
end
return commandArray
.
Re: Newbie question about lua and dzVents
Posted: Saturday 12 October 2024 17:50
by zicht
Hi
I am not sure what you are trying to accomplish here :
Code: Select all
if (deviceName=='Sessy1 batterijvermogen') or (deviceName=='Sessy2 batterijvermogen') then
commandArray['Batterijvermogen'] = deviceName('Sessy1 batterijvermogen') + deviceName('Sessy2 batterijvermogen')
end
I assume it does nothing right now or gives an error.
The DeviceName does not hold anything.
I think you want something like this :
Code: Select all
if devicechanged['Sessy1 batterijvermogen']~=nil or devicechanged['Sessy2 batterijvermogen']~=nil then
local value1=otherdevices['Sessy1 batterijvermogen']
local value2=otherdevices['Sessy2 batterijvermogen']
local value3=value1+value2 --> or incase the value is a string value3=tonumber(value1)+tonumber(value2)
commandArray['Batterijvermogen']=value3
end
explanation :
devicechanged array does only return a value if changed otherwise it returns nil , this can be used to check on changed values. Its a goof habbit to use a value to check against
otherdevices array always returns the value of the sensor.
In my experience using a direct sensor value in
commandArray gives problems sometimes, thus storing it in a local var
Now the sensor batterij vermogen should be updated with the sum of both batteries.
<edit: typo corrected in code>
Re: Newbie question about lua and dzVents
Posted: Saturday 12 October 2024 18:32
by roelvdh
Thank you, zicht, for correcting my first steps, I was going into the wrong direction. This was exactly what I wanted to accomplish and without your help it would have taken a long time to achieve that.
Re: Newbie question about lua and dzVents
Posted: Saturday 12 October 2024 18:50
by zicht
zicht wrote: ↑Saturday 12 October 2024 17:50
Hi
I am not sure what you are trying to accomplish here :
Code: Select all
if (deviceName=='Sessy1 batterijvermogen') or (deviceName=='Sessy2 batterijvermogen') then
commandArray['Batterijvermogen'] = deviceName('Sessy1 batterijvermogen') + deviceName('Sessy2 batterijvermogen')
end
I assume it does nothing right now or gives an error.
The DeviceName does not hold anything.
I think you want something like this :
Code: Select all
if devicechanged['Sessy1 batterijvermogen']~=nil or devicechanged['Sessy2 batterijvermogen']~=nil then
local value1=otherdevices['Sessy1 batterijvermogen']
local value2=otherdevices['Sessy2 batterijvermogen']
local value3=value1+value2 --> or incase the value is a string value3=tonumber(value1)+tonumber(value2)
commandArray['Batterijvermogen']=value3
end
explanation :
devicechanged array does only return a value if changed otherwise it returns nil , this can be used to check on changed values. Its a good habbit to use a value to check against
otherdevices array always returns the value of the sensor.
In my experience using a direct sensor value in
commandArray gives problems sometimes, thus storing it in a local var
Now the sensor batterij vermogen should be updated with the sum of both batteries.
<edit: typo corrected in code>
https://www.domoticz.com/wiki/LUA_commands
Re: Newbie question about lua and dzVents
Posted: Saturday 12 October 2024 19:16
by waltervl
If you want to use dvents you better switch asap as it has far more Domoticz functions to read and write device data so you do not have to create all kind of Lua functions yourself.
Re: Newbie question about lua and dzVents
Posted: Saturday 12 October 2024 19:50
by roelvdh
Strangely, this code
Code: Select all
commandArray = {}
if devicechanged['Sessy1 batterijvermogen']=~nil or devicechanged['Sessy2 batterijvermogen']=~nil then
local value1=otherdevices['Sessy1 batterijvermogen']
local value2=otherdevices['Sessy2 batterijvermogen']
local value3=value1+value2 --> or incase the value is a string value3=tonumber(value1)+tonumber(value2)
commandArray['Batterijvermogen']=value3
end
return commandArray
returns an error
Code: Select all
Error: EventSystem: in Batterijsom: [string "-- ..."]:3: attempt to index a nil value (global 'devicechanged')
I don't understand that. The LUA-interpreter is part of Domoticz, so it should know the array devicechanged
Re: Newbie question about lua and dzVents
Posted: Saturday 12 October 2024 20:50
by jvdz
You probably have a time event trigger enabled as that array is only available for device events.
Re: Newbie question about lua and dzVents
Posted: Saturday 12 October 2024 21:18
by roelvdh
That makes sense, thank you, another time saver. Such things are not easy to find out for a beginner as you would expect the interpreter to be more explicit in such cases. It was a great idea to integrate LUA into Domoticz but I will now quickly change over to dzVents as I expect a more seamless integration there.
Re: Newbie question about lua and dzVents
Posted: Saturday 12 October 2024 23:06
by jvdz
Well it is all pretty good documented in the wiki
https://www.domoticz.com/wiki/LUA_commands, but do understand the steep learning curve and time investment needed to understand the basics.
dzVents is also an extensive set of lua script which makes is a little easier to get started, but I've build all the lua script before it existed so still mostly prefer using straitforward lua scripting.
Re: Newbie question about lua and dzVents
Posted: Saturday 12 October 2024 23:28
by roelvdh
I fully agree about the docs, it's all there and it is quite concise, but also a bit exhausting. After your earlier mail I reread the docs and found good clarification of the problem, which had escaped me at first reading. dzVents looks very powerful and not that difficult to grasp. While longtime LUA-aficionados may prefer to stay put, I changed directions to dzVents and look forward to a longtime happy relationship, thanks to a helpful start.
Re: Newbie question about lua and dzVents
Posted: Sunday 13 October 2024 2:21
by HvdW
I support waltervl's remark to switch to dzVents.
It's so much easier and straightforward.
Fi the trigger devices =
https://www.domoticz.com/wiki/DzVents:_ ... _scripting
Btw if you ask bing or gemini to write you a dzvents script that reacts to a change in a device, you will have the script presented in a few seconds
Re: Newbie question about lua and dzVents
Posted: Sunday 13 October 2024 7:35
by lost
HvdW wrote: ↑Sunday 13 October 2024 2:21
I support waltervl's remark to switch to dzVents.
It's so much easier and straightforward.
Maybe, but that's another logic less programmatic IMO. But even if your brain & dzv trigger logic fits, one advantage to pure Lua that'll remain is the possibility to test code snippets out of Domoticz integration for any non specific part of a program: Just install Lua interpreter on your system & execute you test snippet using system interpreter instead of needing to wait for a device/event or 1mn monitoring the log & risking to hang domoticz (even if from Lua, this should be much more difficult than from python!).
Re: Newbie question about lua and dzVents
Posted: Sunday 13 October 2024 10:56
by HvdW
Tip:
If you insist being swift you can create a test button and trigger anything 'on device' to not having to wait and the log is right beside you in Domoticz.
My former post was triggered by this sentence in your first post: "I just started learning lua with dzVents in mind later."
Re: Newbie question about lua and dzVents
Posted: Sunday 13 October 2024 15:13
by roelvdh
Thank you!