which script type? how function?

Moderator: leecollings

Post Reply
User avatar
abdolhamednik
Posts: 39
Joined: Sunday 27 September 2020 7:33
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Contact:

which script type? how function?

Post by abdolhamednik »

hello everyone

as a beginner here in lua, am i right?
all: script will run with change in everything: minutes, variables, devices
time: will run every minutes
device: will run when any device changes, e.g a sensor's temperature, a relay, an input contact,...

again am i right?
we can have many scripts with type 'all' or 'device' or 'time' that all of them are ON(active) simultaneously

and finally:
why should an 'all' script not be seen in log always next to every other script that has run at that moment?

thank you in advance
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: which script type? how function?

Post by waaren »

abdolhamednik wrote: Tuesday 16 February 2021 6:58 as a beginner here in lua, am i right?
all: script will run with change in everything: minutes, variables, devices
time: will run every minutes
device: will run when any device changes, e.g a sensor's temperature, a relay, an input contact,...
true
again am i right?
we can have many scripts with type 'all' or 'device' or 'time' that all of them are ON(active) simultaneously
true. But there can be only one script executing at the same time. They will all be executed after each other.
and finally:
why should an 'all' script not be seen in log always next to every other script that has run at that moment?
Any type of script is only triggered once per event. So only one time with every device, -variable change and once per minute.
You can have many time and many device, variable triggered scripts and they will all execute based on these triggers.

For example if you have 8 "device triggered" scripts and 2 "all triggered" scripts, all 10 will be executed on every device change.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
abdolhamednik
Posts: 39
Joined: Sunday 27 September 2020 7:33
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Contact:

Re: which script type? how function?

Post by abdolhamednik »

very good waaren...like always :)

can u help me what to do with this tiny all-triggered script. i see it there in log rarely :?

Code: Select all

commandArray = {}

wind = tonumber(uservariables['wind'])
storm = tonumber(uservariables['storm'])
WIND_MAX = tonumber(uservariables['WIND_MAX'])

temp_out = tonumber(uservariables['temp_out'])
humid_out = tonumber(uservariables['humid_out'])

t1_min = tonumber(uservariables['t1_min'])
t1_max = tonumber(uservariables['t1_max'])
h1_min = tonumber(uservariables['h1_min'])
h1_max = tonumber(uservariables['h1_max'])

t2_min = tonumber(uservariables['t2_min'])
t2_max = tonumber(uservariables['t2_max'])
h2_min = tonumber(uservariables['h2_min'])
h2_max = tonumber(uservariables['h2_max'])

mild1 = tonumber(uservariables['mild1']) --is it mild outside dependent on settings of salon 1?
mild2 = tonumber(uservariables['mild2'])
--------------------------------------------
neartmax1 = (2 * t1_max + t1_min)/3
neartmin1 = (t1_max + 2 * t1_min)/3
nearhmax1 = (2 * h1_max + h1_min)/3
nearhmin1 = (h1_max + 2 * h1_min)/3

neartmax2 = (2 * t2_max + t2_min)/3
neartmin2 = (t2_max + 2 * t2_min)/3
nearhmax2 = (2 * h2_max + h2_min)/3
nearhmin2 = (h2_max + 2 * h2_min)/3
--------------------------------------------
if wind > WIND_MAX and storm == 0 then
    commandArray['Variable:storm'] = '1'
    print("------stormy !!!----")
    commandArray[#commandArray + 1] = { ['alarm' ] = 'On' }
    commandArray[#commandArray + 1] = { ['alarm' ] = 'Off AFTER ' .. 10 }
elseif wind < WIND_MAX/2 and storm == 1 then
    commandArray['Variable:storm'] = '0'
end
------------------mild1---------------------
if temp_out > neartmin1 and temp_out < neartmax1 and humid_out > nearhmin1 and humid_out < nearhmax1 then
    if storm == 0 and mild1 == 0 then
        commandArray['Variable:mild1'] = '1'
        print("------biroon-1 motadel!----")
    end
elseif temp_out <= t1_min or temp_out >= t1_max or humid_out <= h1_min or humid_out >= h1_max or storm == 1 then
    if mild1 == 1 then
        commandArray['Variable:mild1'] = '0'
    end
end
-------------------mild2--------------------
if temp_out > neartmin2 and temp_out < neartmax2 and humid_out > nearhmin2 and humid_out < nearhmax2 then
    if storm == 0 and mild2 == 0 then
        commandArray['Variable:mild2'] = '1'
        print("------biroon-2 motadel!----")
    end
elseif temp_out <= t2_min or temp_out >= t2_max or humid_out <= h2_min or humid_out >= h2_max or storm == 1 then
    if mild2 == 1 then
        commandArray['Variable:mild2'] = '0'
    end
end

return commandArray
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: which script type? how function?

Post by waaren »

abdolhamednik wrote: Tuesday 16 February 2021 12:13 can u help me what to do with this tiny all-triggered script. i see it there in log rarely :?
If you don't see anything in the log then probably none of the if / elseif conditions evaluate to true.

I added some extra print lines in below version. It will dump all uservars and the commandArray to the log and might help you in debugging the script.

Code: Select all

commandArray = {}

local function tprint (t, indent, done)
  local done = done or {} 
  indent = indent or 0
  for key, value in pairs (t) do
    pre = (string.rep (" ", indent)) -- indent it
    if type (value) == "table" and not done[value] then
      done [value] = true
      print (pre .. tostring (key) .. ":");
      tprint (value, indent + 2, done)
    elseif type(value) == 'function' and not done[value] then
      print( pre .. (tostring (key) .. "()"))
    else
      pre = pre .. (tostring (key) .. ": ")
      print (pre .. tostring(value))
    end
  end
end

local function var2Num(var)
    print (tostring(var) ..': ' .. (tonumber(uservariables[var] or 0 ) ) )    
    return tonumber(uservariables[var] or 0  )
end    

print ('------------------- All uservars' )
tprint(uservariables)

local wind = var2Num('wind')
local storm = var2Num('storm')
local WIND_MAX = var2Num('WIND_MAX')

local temp_out = var2Num('temp_out')
local humid_out = var2Num('humid_out')

local t1_min = var2Num('t1_min')
local t1_max = var2Num('t1_max')
local h1_min = var2Num('h1_min')
local h1_max = var2Num('h1_max')

local t2_min = var2Num('t2_min')
local t2_max = var2Num('t2_max')
local h2_min = var2Num('h2_min')
local h2_max = var2Num('h2_max')

local mild1 = var2Num('mild1') --is it mild outside dependent on settings of salon 1?
local mild2 = var2Num('mild2')
--------------------------------------------
local neartmax1 = (2 * t1_max + t1_min)/3
local neartmin1 = (t1_max + 2 * t1_min)/3
local nearhmax1 = (2 * h1_max + h1_min)/3
local nearhmin1 = (h1_max + 2 * h1_min)/3

local neartmax2 = (2 * t2_max + t2_min)/3
local neartmin2 = (t2_max + 2 * t2_min)/3
local nearhmax2 = (2 * h2_max + h2_min)/3
local nearhmin2 = (h2_max + 2 * h2_min)/3
--------------------------------------------
if wind > WIND_MAX and storm == 0 then
    commandArray['Variable:storm'] = '1'
    print("------stormy !!!----")
    commandArray[#commandArray + 1] = { ['alarm' ] = 'On' }
    commandArray[#commandArray + 1] = { ['alarm' ] = 'Off AFTER ' .. 10 }
elseif wind < WIND_MAX/2 and storm == 1 then
    commandArray['Variable:storm'] = '0'
end
print ('------------------- commandArray after if block 1' )
tprint(commandArray)

------------------mild1---------------------
if temp_out > neartmin1 and temp_out < neartmax1 and humid_out > nearhmin1 and humid_out < nearhmax1 then
    if storm == 0 and mild1 == 0 then
        commandArray['Variable:mild1'] = '1'
        print("------biroon-1 motadel!----")
    end
elseif temp_out <= t1_min or temp_out >= t1_max or humid_out <= h1_min or humid_out >= h1_max or storm == 1 then
    if mild1 == 1 then
        commandArray['Variable:mild1'] = '0'
    end
end
print ('------------------- commandArray after if block 2' )
tprint(commandArray)
-------------------mild2--------------------
if temp_out > neartmin2 and temp_out < neartmax2 and humid_out > nearhmin2 and humid_out < nearhmax2 then
    if storm == 0 and mild2 == 0 then
        commandArray['Variable:mild2'] = '1'
        print("------biroon-2 motadel!----")
    end
elseif temp_out <= t2_min or temp_out >= t2_max or humid_out <= h2_min or humid_out >= h2_max or storm == 1 then
    if mild2 == 1 then
        commandArray['Variable:mild2'] = '0'
    end
end

print ('------------------- commandArray after if block 3' )
tprint(commandArray)

return commandArray

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
abdolhamednik
Posts: 39
Joined: Sunday 27 September 2020 7:33
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Contact:

Re: which script type? how function?

Post by abdolhamednik »

thanks waaren. u are a helpful kind person
this is the result:

Code: Select all

2021-02-17 10:19:01.247 Status: EventSystem: Script event triggered: Is_Smart
2021-02-17 10:19:01.276 Status: LUA: L_out No change 1day!
2021-02-17 10:19:01.277 Status: LUA: Wind No change 1day!
2021-02-17 10:19:01.307 Status: EventSystem: Script event triggered: Sensor_Averages
2021-02-17 10:19:01.352 Status: LUA: ------------------- All uservars
2021-02-17 10:19:01.353 Status: LUA: pd1toggle: 7
2021-02-17 10:19:01.353 Status: LUA: senserror: 1
2021-02-17 10:19:01.353 Status: LUA: HEATER1_AUTO: 1
2021-02-17 10:19:01.353 Status: LUA: NOBURNER_WAIT: 2
.
.
.
2021-02-17 10:19:01.392 Status: LUA: h2_min: 35.0
2021-02-17 10:19:01.392 Status: LUA: h2_max: 30.0
2021-02-17 10:19:01.392 Status: LUA: mild1: 0
2021-02-17 10:19:01.393 Status: LUA: mild2: 0
2021-02-17 10:19:01.393 Status: LUA: ------------------- commandArray after if block 1
2021-02-17 10:19:01.393 Status: LUA: ------------------- commandArray after if block 2
2021-02-17 10:19:01.393 Status: LUA: ------------------- commandArray after if block 3

so, is it true that a script runs, but doesn't show its name in log if it makes no change in vars or outputs?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: which script type? how function?

Post by waaren »

abdolhamednik wrote: Wednesday 17 February 2021 7:51 so, is it true that a script runs, but doesn't show its name in log if it makes no change in vars or outputs?
Almost.

the message "EventSystem: Script event triggered: <path/nameOfScript>" will be send to the log if the setting for
log 'event script triggers' ( via [setup][settings][other] ) is set like
log.png
log.png (14.59 KiB) Viewed 378 times
AND

there is something in the commandArray table.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
abdolhamednik
Posts: 39
Joined: Sunday 27 September 2020 7:33
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Contact:

Re: which script type? how function?

Post by abdolhamednik »

waaren
i'd really like to understand the robust functional "domoticz" and "lua" completely
but i found no complete step-by-step guide. so, the forum is the only way for me
  • lua.org; i think it differs somehow from the lua in domoticz and
to stop this confusion, is there any good idea?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: which script type? how function?

Post by waaren »

abdolhamednik wrote: Wednesday 17 February 2021 11:07 to stop this confusion, is there any good idea?
The Lua in domoticz is a complete implementation of the Lua as described at Lua.org. The differences are the triggering of the scripts, the tables passed from domoticz to Lua with the information on devices, uservars, etc and the commandArray passed back from Lua to domoticz.

For generic Lua knowledge, lua.org is an excellent source of information
For domoticz specifics look at this wiki page

and if that is not sufficient, start coding and amaze yourself. If you got stuck with something after searching, trying, debugging then this forum is the place to go.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest