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.
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
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 :
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>
Last edited by zicht on Saturday 12 October 2024 18:49, edited 1 time in total.
Rpi & Win x64. Using : cam's,RFXCom, LaCrosse, RFY, HuE, google, standard Lua, Tasker, Waze traveltime, NLAlert&grip2+,curtains, vacuum, audioreceiver, smart-heating&cooling + many more (= automate all repetitive simple tasks)
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.
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 :
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.
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.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
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
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.
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.
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.
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!).
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."