When I started in Domoticz I got my information from the manual " DzVents:_next_generation_LUA_scripting "
When you look at the examples they use execute function (domoticz, time) if the trigger is time, device if the trigger is a device, sensor if it is a sensor and item if its something else. But waaren showed working scripts with only dz or domoticz in it when I posted a problem script.
Can I use the execute function (dz) always and what does the second command do when you use it?
how to use function correctly [Solved]
Moderator: leecollings
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: how to use function correctly
Every dzVents script use the execute function. This function can have 1-3 parameters (all optional)MeAlbert wrote: Monday 24 February 2020 15:20 Can I use the execute function (dz) always and what does the second command do when you use it?
The first parm is the domoticz object This object gives you access to almost everything in your Domoticz system, including all methods to manipulate them—like modifying switches or sending notifications.
The object-type of the second parm depends on what triggered the script. It can be a time object, a device object or any other object that triggered the script.
The third parm contains some information on the trigger but is no longer needed because from dzVents V2.4.0 onwards all information can be found in the 2nd parm. It is only there for backward compatibility purposes.
You can name these parms to whatever you prefer and use in the rest of your scrip as long as complies with the Lua naming convention for identifiers. They can be any string of letters, digits, and underscores, not beginning with a digit. and not equal to a reserved word like fuction, else, etc..
So if you use dz for the domoticz object any reference to that object must use dz.
A Lua object is a table. A table can contain attributes, functions and... sub-tables.
You can inspect any object in your script by using domoticz.utils.dumpTable(objectname) ( or dz.utils.dumpTable(objectname) if you used dz as the name of the first parm of the execute function). It will dump the string representation of all items contained in the object.
Described in detail in the dzVents wiki
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
MeAlbert
- Posts: 65
- Joined: Friday 06 December 2019 14:23
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2020.1
- Contact:
Re: how to use function correctly
Waaren. Thanks for this info. Is more clear to me than the wiki. Some parts I read ten times and still don't understand what is meant.
Maybe it has to do with the way it is setup but I realize it is something from me.
But I have one question left. In this script you start with timer but do not use time in the function. When must i use time and when can I leave it away?
Maybe it has to do with the way it is setup but I realize it is something from me.
But I have one question left. In this script you start with timer but do not use time in the function. When must i use time and when can I leave it away?
Code: Select all
return
{
on =
{
timer = { 'every minute' }
},
logging =
{
--level = domoticz.LOG_DEBUG, -- for debugging
level = domoticz.LOG_INFO,
marker = "WP vermogen per uur"
},
execute = function (dz)- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: how to use function correctly
If you look a the full script you can see that the triggering object (timer) is not used in the execute function. So there is no need to pass this parameter to the function. The first parm = domoticz object (here with the name dz) is used 4 times in the execute function. That's why it must be passed to the function.MeAlbert wrote: Monday 24 February 2020 23:28 In this script you start with timer but do not use time in the function. When must i use time and when can I leave it away?
- Spoiler: show
Code: Select all
return
{
on =
{
timer = { 'every minute' }
},
logging =
{
--level = domoticz.LOG_DEBUG, -- for debugging
level = domoticz.LOG_INFO,
marker = "WP vermogen per uur"
},
execute = function (dz, item)
local heatPump = dz.devices(64) -- target: define as electric + counter
local WpPower = dz.devices(70).WhActual -- source
local newTotal = heatPump.WhTotal + WpPower / 60
dz.log ('WpPower = ' .. WpPower)
dz.log ('newTotal = ' .. newTotal)
heatPump.updateElectricity ( WpPower, newTotal )
dz.utils.dumpTable(item)
end
}Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
MeAlbert
- Posts: 65
- Joined: Friday 06 December 2019 14:23
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2020.1
- Contact:
Re: how to use function correctly [Solved]
Ok. Understand now. So if a statement is triggered by a device that is not used you can leave out device. Same with time when not used etc.
Clear to me. Hope that some day I can make my scripts as clean as you can.
Thanks a lot.
Clear to me. Hope that some day I can make my scripts as clean as you can.
Thanks a lot.
Who is online
Users browsing this forum: No registered users and 1 guest