variable on/off construct  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

variable on/off construct

Post 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
}
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
User avatar
boum
Posts: 136
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: variable on/off construct

Post 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
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: variable on/off construct

Post 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)
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: variable on/off construct

Post 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
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
User avatar
boum
Posts: 136
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: variable on/off construct

Post 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.
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: variable on/off construct

Post 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!
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: variable on/off construct

Post 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
}
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: variable on/off construct

Post 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.




Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: variable on/off construct  [Solved]

Post by pvklink »

:oops: :oops: :oops: :oops: :oops: :oops:

yes, indeed.....
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: variable on/off construct

Post 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
}
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest