Page 1 of 1
trigger, trigger and still trigger
Posted: Monday 23 September 2019 19:08
by darrepac
Hi
Coming from LUA, but willing to learn dzvents, I have still difficulty to understand how to write trigger the right way.
I have this current script that works well (thanks to support here):
Code: Select all
return {
on = {
timer = {'at 5:00', 'at 11:00', 'at 19:00'}
},
data = {
temp = { initial = 20 } --20 comme level pas comme temperature, correspond à 45°C
},
execute = function(domoticz, timer)
print ("Avant:" .. domoticz.data.temp)
if (timer.trigger == 'at 5:00') then
print ("Passage de la consigne a 20°C")
domoticz.devices('Ballon ECS Bas').switchSelector(0)
elseif (timer.trigger == 'at 11:00') then
domoticz.devices('Ballon ECS Bas').switchSelector(domoticz.data.temp)
print("Retour de la consigne d origine")
elseif (timer.trigger == 'at 19:00') then
domoticz.data.temp = domoticz.devices('Ballon ECS Bas').level
print ("prise en compte de la consigne de temperature")
domoticz.devices('Ballon ECS Bas').switchSelector(10)
print("Chauffage de l'ECS moins important la nuit")
end
print ("Apres:" .. domoticz.data.temp)
end
}
So my script is triggered at 5, 11 and 19.
Now, I would like to trigger a new action when many conditions are met:
(between 11:05 and 16) and (consommation == 0) and (Production Eau Chaude == 'Off')
Obviously i would like to avoid the script to be triggered every minute between 11:05 and 16, so would be better to trigger it first from "Production Eau Chaude" which is the parameter that will change the least. But I have no idea how to do that!
In LUA, I would have done a time script and check if the conditions are met every minute...
Re: trigger, trigger and still trigger
Posted: Monday 23 September 2019 21:17
by waaren
darrepac wrote: Monday 23 September 2019 19:08
Coming from LUA, but willing to learn dzvents, I have still difficulty to understand how to write trigger the right way.
Now, I would like to trigger a new action when many conditions are met:
(between 11:05 and 16) and (consommation == 0) and (Production Eau Chaude == 'Off')
Obviously i would like to avoid the script to be triggered every minute between 11:05 and 16
You could do it like this. The function in the timer section will be called every minute and if it evaluates to true, the execute = section will be executed. If you would put the same function in the devices = section, it would be evaluated at every device update (of all devices) in the system.
Code: Select all
return {
on = {
timer = {
function(domoticz)
return domoticz.devices('Production Eau Chaude').state == 'Off' and
domoticz.time.matchesRule('at 11:05-16:00') and
domoticz.devices('consommation').usage == 0
end},
},
execute = function(domoticz, device)
domoticz.log('All trigger rules true', domoticz.LOG_FORCE)
end
}
Re: trigger, trigger and still trigger
Posted: Tuesday 24 September 2019 8:21
by darrepac
Thanks!
Interesting.
Questions:
1/about
Code: Select all
execute = function(domoticz, device)
, it should not be
Code: Select all
execute = function(domoticz, timer)
as we are triggering with timer??
2/ How can I combine this trigger with my current trigger (At 5, 11 and 19), like this?
Code: Select all
return {
on = {
timer = {'at 5:00', 'at 11:00', 'at 19:00',
function(domoticz)
return domoticz.devices('Production Eau Chaude').state == 'Off' and
domoticz.time.matchesRule('at 11:05-16:00') and
domoticz.devices('consommation').usage == 0
end}
},
Re: trigger, trigger and still trigger
Posted: Tuesday 24 September 2019 9:38
by waaren
darrepac wrote: Tuesday 24 September 2019 8:21
1/about
Code: Select all
execute = function(domoticz, device)
, it should not be
Code: Select all
execute = function(domoticz, timer)
as we are triggering with timer ?
domoticz and device are here just names of the objects passed to the execute function.
The first parm is the domoticz object and the second parm is whatever triggered the execute function. Just try this
Code: Select all
return
{
on =
{
timer =
{
function(domoticz)
return domoticz.devices('Production Eau Chaude').state == 'Off'
and
(
domoticz.time.matchesRule('at 5:00') or
domoticz.time.matchesRule('at 11:00') or
domoticz.time.matchesRule('at 19:00')
)
and
domoticz.devices('consommation').usage == 0
end
},
},
execute = function(dz, item)
dz.log('All trigger rules true', dz.LOG_FORCE)
dz.utils.dumpTable(item) -- this will dump the object with all attributes and functions as string to the logfile
end
}
2/ How can I combine this trigger with my current trigger ?
See above
Re: trigger, trigger and still trigger
Posted: Tuesday 24 September 2019 9:45
by darrepac
I may be oldfashioned but I feel that it is not straightforward comparing to LUA (that I was not knowing before I played with it for Domoticz)
Re: trigger, trigger and still trigger [Solved]
Posted: Tuesday 24 September 2019 10:18
by waaren
darrepac wrote: Tuesday 24 September 2019 9:45
I may be oldfashioned but I feel that it is not straightforward comparing to LUA (that I was not knowing before I played with it for Domoticz)
The issue here is that you try to prevent a Lua time trigger to be executed every minute. That's is not possible in domoticz.
The way domoticz deals with Lua time triggers is simply to execute all of them every minute. dzVents is hiding that from you by intercepting that trigger using the main script. (<domoticz>/dzVents/runtime/dzVents.lua) that main script interprets all your user scripts and only execute the ones needed.
Your idea to optimize the handling of the script by having a device as script trigger is not an optimization as domoticz does trigger all Lua device scripts on every device change. Again dzVents is hiding that from you.
The transparant way of dealing with your requirement in dzVents would be:
return
Code: Select all
{
on =
{
timer =
{ 'at 5:00', 'at 11:00', 'at 19:00' },
},
execute = function(dz, item)
if domoticz.devices('Production Eau Chaude').state == 'Off' and domoticz.devices('consommation').usage == 0 then
-- do stuff here
end
end
}
Could be that I misunderstood your initial requirement. In that case please share the classic Lua script that will do what you need and I will try to create the corresponding dzVents variant.
Re: trigger, trigger and still trigger
Posted: Tuesday 24 September 2019 10:28
by darrepac
Ok I understand the logic...
But it come to my mind a more generic question: if everything (except device change) is triggered every minutes, it would cause a bottleneck? Why not having the possibility to offset some scripts (one by 10sec, another by 20sec etc): so instead of running at each minutes and 0 seconds, you can run at each minutes and 10 seconds for example....
Re: trigger, trigger and still trigger
Posted: Tuesday 24 September 2019 10:52
by waaren
darrepac wrote: Tuesday 24 September 2019 10:28
But it come to my mind a more generic question: if everything (except device change) is triggered every minutes, it would cause a bottleneck? Why not having the possibility to offset some scripts (one by 10sec, another by 20sec etc): so instead of running at each minutes and 0 seconds, you can run at each minutes and 10 seconds for example....
That's more a question for domoticz core developers and not relevant for dzvents as dzVents is in essence just one Lua script, triggered by time (every minute) and by other events when they occur (devices, groups, scenes, variables, security and httpResponse)
When it is triggered it will execute only the user scripts (which are functions of the main dzVents scripts) of which any of the conditions in the on = sections are true. Furthermore the domoticz event system requires any script (including dzVents) to return control within 10 seconds. So in that timeframe dzVents needs to handle all user scripts.
I guess if you would have many time based classic Lua scripts it might become an issue but fact is that Lua and Python are pretty fast and I never red any issue on this forum that the amount of Lua, Blockly, Python or dzVents scripts turned into a bottleneck.