Newbie question about lua and dzVents

Moderator: leecollings

Post Reply
roelvdh
Posts: 37
Joined: Saturday 18 January 2020 13:40
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: NL
Contact:

Newbie question about lua and dzVents

Post 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

.
zicht
Posts: 271
Joined: Sunday 11 May 2014 11:09
Target OS: Windows
Domoticz version: 2023.1+
Location: NL
Contact:

Re: Newbie question about lua and dzVents

Post 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>
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)
roelvdh
Posts: 37
Joined: Saturday 18 January 2020 13:40
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: NL
Contact:

Re: Newbie question about lua and dzVents

Post 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.
zicht
Posts: 271
Joined: Sunday 11 May 2014 11:09
Target OS: Windows
Domoticz version: 2023.1+
Location: NL
Contact:

Re: Newbie question about lua and dzVents

Post 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
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)
User avatar
waltervl
Posts: 5397
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Newbie question about lua and dzVents

Post 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.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
roelvdh
Posts: 37
Joined: Saturday 18 January 2020 13:40
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: NL
Contact:

Re: Newbie question about lua and dzVents

Post 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
Last edited by roelvdh on Saturday 12 October 2024 20:55, edited 1 time in total.
User avatar
jvdz
Posts: 2210
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Newbie question about lua and dzVents

Post by jvdz »

You probably have a time event trigger enabled as that array is only available for device events.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
roelvdh
Posts: 37
Joined: Saturday 18 January 2020 13:40
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: NL
Contact:

Re: Newbie question about lua and dzVents

Post 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.
User avatar
jvdz
Posts: 2210
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Newbie question about lua and dzVents

Post 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.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
roelvdh
Posts: 37
Joined: Saturday 18 January 2020 13:40
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: NL
Contact:

Re: Newbie question about lua and dzVents

Post 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.
HvdW
Posts: 539
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Newbie question about lua and dzVents

Post 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
Bugs bug me.
lost
Posts: 643
Joined: Thursday 10 November 2016 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Newbie question about lua and dzVents

Post 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!).
HvdW
Posts: 539
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Newbie question about lua and dzVents

Post 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."
Bugs bug me.
roelvdh
Posts: 37
Joined: Saturday 18 January 2020 13:40
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: NL
Contact:

Re: Newbie question about lua and dzVents

Post by roelvdh »

Thank you!
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests