Page 1 of 1
variable on/off construct
Posted: Wednesday 10 July 2019 9:32
by pvklink
I have a large script with all kind of groups in it like 5 devices that go On at evening and the same for off.
This script has a lot of groups and timers. Lot of the groups are the same, only difference is to put the devices On or Off...
So i tried to write my first function in a script to reuse some of the code.
What i cant script is how to make a variable on/off command
dz.devices('lantaarn1').<on/off variable> same for a parameter for/aftermin
When this works i can delete the half of my groups...
Code: Select all
return {
on = { devices = { 'test' }},
execute = function(dz, item, info)
local logcode = 3 -- log alles, 0 = niets, 1 dom, 2 global, 3 alles... evt per regel ook in te regelen
local messageTable = {}
local add = 'add'
local del = 'del'
local chg = 'chg'
local function globalMessage(action, message, logcode)
if logcode == nil then logcode = 3 end
if action == add and logcode > 0 then
messageTable = dz.helpers.addMessage(dz, item, info, message, messageTable, logcode)
elseif action == del then
dz.globalData.mylogging = message
dz.devices('timerlog').updateText(message)
elseif action == chg then
dz.helpers.dumpMessages(dz, messageTable)
end
end
local function groep1(action,timer)
dz.devices('lantaarn1') .. '.switch' .. action .. '()' -- also paste afterxxxx
dz.devices('lantaarn2') .. '.switch' .. action .. '()' -- also paste afterxxxx
dz.devices('lantaarn3') .. '.switch' .. action .. '()' -- also paste afterxxxx
dz.devices('lantaarn4') .. '.switch' .. action .. '()' -- also paste afterxxxx
dz.devices('lantaarn5') .. '.switch' .. action .. '()' -- also paste afterxxxx
globalMessage(add, ' Groep1 is ' .. action,3)
end
groep1(item.state) -- also .afterMin(30) as a parameter has to be optional
globalMessage(chg) -- dump
end
}
Re: variable on/off construct
Posted: Wednesday 10 July 2019 11:56
by boum
In Lua, the syntax
obj.bla is the same as
obj["bla"] so you could do:
Code: Select all
local devName = "Light1"
local func = "switchOn"
local timer = "afterSec"
local timerVal = 5
dz.devices(devName)[func]()[timer](timerVal)
I tried to add conditional timer too:
Code: Select all
local switchedDev = dz.devices(devName)[func]()
if timer then
switchedDev[timer](timerVal)
end
Re: variable on/off construct
Posted: Wednesday 10 July 2019 13:16
by pvklink
yes that is working!
Is it possible to give more records/data in the call so i can put groups on/off?
tried
local function groep1(actions)
for action in pairs(actions) do
local devName = x
local func = "switch" .. y
local timer = z
local timerVal = q
dz.devices(devName)[func]()[timer](timerVal)
end
end
actions = {'lantaarn1:Off,afterSec:5','lantaarn2:On,afterMin:5',}
groep1(actions)
Re: variable on/off construct
Posted: Wednesday 10 July 2019 13:47
by pvklink
I think the best way to solve this is to expand the IFTTTVAR solution from @waaren..
That solution reacts on a json call that fills a uservar with values. A script then reads this values and puts an action on one device
This is a great working api for IFTTT and nodered/mqtt. When this IFTTTVAR uses a json structure instead of an own-defined structure it can do more then one device action based on that json structure..
i even dont need extra functions for the problems described in this posts
Re: variable on/off construct
Posted: Wednesday 10 July 2019 13:56
by boum
Well, if you already have ifttt set up, that's good.
To do that in dzVents, I would have done something like that:
Code: Select all
local actions =
{
{ device = 'Light1', action = 'On', delays = { afterSec = 5, forMin = 10 } },
{ device = 'Light2', action = 'Off', delays = { afterMin = 1 } },
{ device = 'Plug3', action = 'On' },
}
local executeActions = function(dz, actions)
for _,action in pairs(actions) do
local device = dz.devices(action.device)
local switch = 'switch' .. action.action
local switchedDev = device[switch]()
local delays = action.delays
if delays then
for delayFunc,delayVal in pairs(delays) do
switchedDev[ delayFunc ]( delayVal )
end
end
end
end
executeActions(domoticz, actions)
(I haven't tried the script in DZ, I just checked the syntax, there may be issues with it)
Anyway, that all depends on how you have your actions/devices stored and the amount of maintenance you'll have to do with that.
Re: variable on/off construct
Posted: Wednesday 10 July 2019 20:05
by pvklink
i am going to try it

a new version of ifttvar is more work, and for after my holiday...
i already have made all my groups with your variable on/off construction... less code, less errors thanks!
Re: variable on/off construct
Posted: Saturday 13 July 2019 0:24
by pvklink
ok, i tried this
and get some errors
7-13 00:24:36.163 Error: EventSystem: Lua script Script #1 did not return a commandArray
2019-07-13 00:24:36.538 Error: EventSystem: Lua script Script #1 did not return a commandArray
2019-07-13 00:24:39.226 Error: EventSystem: Lua script Script #1 did not return a commandArray
2019-07-13 00:24:39.422 Error: EventSystem: Lua script Script #1 did not return a commandArray
Code: Select all
return {
on = { devices = { 'test' }},
execute = function(dz, item, info)
local actions =
{
-- { device = 'Light1', action = 'On', delays = { afterSec = 5, forMin = 10 } },
-- { device = 'Light2', action = 'Off', delays = { afterMin = 1 } },
{ device = 'lantaarn', action = 'On' },
}
local executeActions = function(dz, actions)
for _,action in pairs(actions) do
local device = dz.devices(action.device)
local switch = 'switch' .. action.action
local switchedDev = device[switch]()
local delays = action.delays
if delays then
for delayFunc,delayVal in pairs(delays) do
switchedDev[ delayFunc ]( delayVal )
end
end
end
end
executeActions(dz, actions)
end
}
Re: variable on/off construct
Posted: Saturday 13 July 2019 7:47
by waaren
pvklink wrote:ok, i tried this
and get some errors
7-13 00:24:36.163 Error: EventSystem: Lua script Script #1 did not return a commandArray
You saved the script as Lua but it is of type dzVents.
Re: variable on/off construct [Solved]
Posted: Saturday 13 July 2019 9:54
by pvklink
Re: variable on/off construct
Posted: Saturday 13 July 2019 14:15
by pvklink
It works!
After my holiday going to ask you pro's to change the iftttvar script so that it can
- handles more than one device and
- uses json format instead of my own format...
- can switch on/off different kind off switches with major properties like after etc.
i use the ifttvar as an api between ifttt and domoticz and it works fantastic!
iftttvar
Code: Select all
local iftttvar = "iftttvar"
return
{
on =
{ variables = { iftttvar },
},
logging =
{
level = domoticz.LOG_ERROR,
marker = 'ifttt',
},
execute = function(dz, item, info)
local validBaseTypes = { device=true, group=true, scene=true, uservariable=false, camera=false }
local validActions = { ON=true, OFF=true }
local result = dz.utils.stringSplit(item.value,'/') -- aangepast was geen local
local name = result[1]
local action = string.upper(result[2])
local parm1 = result[3]
local stype = result[4]
local logcode = 3
local messageTable = {}
local add = 'add'
local del = 'del'
local chg = 'chg'
local function globalMessage(action, message, logcode)
if logcode == nil then logcode = 3 end
if action == add and logcode > 0 then
messageTable = dz.helpers.addMessage(dz, item, info, message, messageTable, logcode)
elseif action == del then
dz.globalData.mylogging = message
dz.devices('timerlog').updateText(message)
elseif action == chg then
dz.helpers.dumpMessages(dz, messageTable)
end
end
if parm1 == nil then parm1 = 'undefined' end -- par1 is de selectornaam of selectorlevel
if stype == nil then stype = 'undefined' end -- stype is of par naam 1 of level is 2
local function getBaseType(name)
for i, tuple in ipairs(_G.domoticzData) do
if tuple.name == name then
return tuple.baseType, tuple.id
end
end
end
local baseType, idx = getBaseType(name)
if baseType and validBaseTypes[baseType] then
local target
if baseType == 'device' then
target = dz.devices(name)
elseif baseType == 'group' then
target = dz.groups(name)
elseif baseType == 'scene' then
target = dz.scenes(name)
validActions.OFF = nil -- No Off action for scenes
end
if validActions[action] then
if action == 'ON' then
if parm1 ~= 'undefined' then
if stype == '1' or stype == 'undefined' then
globalMessage(add, ' IFTTT AAN: ,settings: ' .. tostring(item.value), logcode)
target.switchSelector(parm1) --parm selector_name
elseif stype == '2' then
globalMessage(add, ' IFTTT AAN: settings: ' .. tostring(item.value), logcode)
target.switchSelector(tonumber(parm1)) --parm selector_id
else -- foute waarde
globalMessage(add, ' IFTTT AAN: foute waarde, settings: ' .. tostring(item.value), logcode)
end
else
globalMessage(add, ' IFTTT AAN: settings: ' .. tostring(item.value), logcode)
target.switchOn()
end
elseif action == 'OFF' then
globalMessage(add, ' IFTTT AAN: settings: ' .. tostring(item.value), logcode)
target.switchOff()
else -- no other actions implemented yet
globalMessage(add, 'IFTTT AAN: foute action waarde, settings: ' .. action, logcode)
end
else
globalMessage(add, 'IFTTT AAN: action: ' .. action .. ' is not implemented for ' .. baseType .. ' Name ' .. name .. '!', logcode)
end
elseif baseType then
globalMessage(add, ' IFTTT AAN: onbekende basetype: ' .. baseType, logcode) -- dz.LOG_ERROR evt ook doorgeven
else
globalMessage(add, ' IFTTT AAN: name not found!: ' .. name, logcode)
end
globalMessage(chg) -- dump
end
}