Script to enhance Switch Sub/Slave options

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.
Post Reply
User avatar
lonebaggie
Posts: 107
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Script to enhance Switch Sub/Slave options

Post by lonebaggie » Saturday 02 September 2017 23:56

I have created a LUA script to link two switches together. The sub/slave option in Domoticz is limited. This script will link multiple switches together in switch pairs (source and destination). The actions of the source switch will effect the destination. The options are :-
  • Mirror source to Destination
    Toggle source to Destination
    Turn destination on when source is turned on
    Turn destination off when source is turned off
    Mirror Dimmer switches or Sets Dimmer to a set value
    Hide switches
    Update user variables
    Use third switch for conditional Access if true or false switch
The script can either action the switch or simply change the state on the switch in Domoticz. This is useful for resetting switches without triggering the "payload"

The script requires a string user variable to be created called ST-Setting. This variable is then populated with the switch IDx numbers and the commands required. Multiple switches can be added to the user variable to create "ripple effects" . Warning there is only limited syntax checking so creating a switch loop is possible.

The script contains the syntax required to link source and destination switches together

Create a new LUA device event called Switch-Link-1.8 and cut and paste the following

Code: Select all

-- This program links two switches together so the actions of the one switch effects the other
-- 
--These commands can  trigger the switches or just effect the Domoticz switches without triggering the action of the switches
--
--
--
-- The program relies on a user string variable to be created in Domoticz interface with the name ST-Setting 
-- Use the following syntax for the string value
--
--                   IDx|(1)(2)(T)(F)(IDx)|IDx|Command|IDx|(T)(F)(IDx)|IDx|Commands etc
--
-- Where IDx          is the source IDx number of virtual switch (obtain the IDx value from the Device list)
--       (T)(F)(IDx)* Only trigger destination if test switch is on (T) or Off (F). 
--       (1)(2)       (1) way switch source affects destination (2) way switch souce and destination effects each other 
--       IDx          is the destination IDx number of virtual switch to be linked to the source switch
--
--       Commands 
--
--       D          Duplicated. ( Source will force  the destination switch to match 
--       T          Toggled     ( Source will toggle the destination switch to be the opposite )
--       p          on          ( source will change the destination switch to be on)
--       M          Off         ( source will change the destination switch to be off)
--       Fnn*       Fixed       ( source Dim value will mirror the Destination if dim value set to 00 or  change value to nn)
--       H          Hide        ( Source will Hide destination switch. h will un-dide hidden switch )
--       
--
--       Uccc..     UVariable   ( Source will trigger IDx of User Variable (string only) to be replaced with 
--                                charactors specified. @t@ will insert time, @d@ date, @i@ Idx, 
--                                                      @n@ name  @v@ value @l@ last update) 
--
-- Example 120|1|123|T 
--
-- Using lowercase letters for the above will change the switch in Domoticz without triggering the action. Usful for resetting
-- switches
--
-- *There is bug in Domoticz. Dim Slider will not match value using fnn . Fnn works correctly but will trigger action
-- see https://www.domoticz.com/forum/viewtopic.php?t=14935
-- 
--  
-- Ver 1.0
--
-- Initial program
--
-- Ver 1.1
--
-- Bug in Command array only actioned last switch action. Updaded to create comand array index
--
-- Ver 1.2 
--
-- Change switch test from on and off to off and not off , to allow switch link to work with non-similer switches.
--
-- Ver 1.3
--
-- Added Dimmer switch mirror. Changed commands names to be more logic
--
-- Vesion 1.4 
--
-- Added trigger by log
--
-- Version 1.5
--
-- Added Hide and unhide switches and set uservariables
--
-- Version 1.6 
--
-- Changed User variables switch to use iDX number of User variable rather than name  
--
-- Version 1.7 
--
-- Updated to allow Conditional switching. Major change now 4 varibles  per switch  
--
-- Version 1.8
--
-- Added two way switching ( to reduce ST-Setting length and complexity) and error checking
--



commandArray = {}

-- functions
function split(pString, pPattern)
    local Table = {}  
    local fpat = "(.-)" .. pPattern
    local last_end = 1
    local s, e, cap = pString:find(fpat, 1)
    while s do
        if s ~= 1 or cap ~= "" then
            table.insert(Table,cap)
        end
        last_end = e+1
        s, e, cap = pString:find(fpat, last_end)
    end
    if last_end <= #pString then
        cap = pString:sub(last_end)
        table.insert(Table, cap)
    end
   return Table
end

function idxname(deviceIDX)
   local idx = tonumber(deviceIDX)
   for i, v in pairs(otherdevices_idx) do
      if v == idx then
         return i
      end
   end
   return 0
end
function idxvars(uridx)
    urlcall=assert(io.popen("curl 'http://127.0.0.1:8080/json.htm?type=command&param=getuservariable&idx=" .. uridx .. "'" ))
        local urlreply = urlcall:read('*all')
    urlcall:close()
    local urlsplit = split(urlreply,",")
    local urlname = string.gsub(urlsplit[2], '"', "")
    urlname = string.sub(urlname,7)
    urlname = string.sub(urlname,12)
    return urlname
end

-- variables
id=0
cmd=0
Vars = {}
Vars=split(uservariables["ST-Setting"], "|")
Dname = ""
Dname1 = ""
Dname2 = ""
runflag = "T"
s=0
e=0
etime = os.clock()
-- start program
print ("Switch Link started")
-- ensue ST-Settings conatin info
if string.len(uservariables["ST-Setting"]) < 1  then
    print ("No ST-Setting Exit script")
    print ("Switch link completed in " .. os.clock()-etime)
    return commandArray
end
if #Vars % 4 ~= 0 then
    print ("ST-Setting invalid Exit script")
    print ("Switch link completed in " .. os.clock()-etime)
    return commandArray
end
-- check for two way switch. Add revese of switch in Table. Test if valid commands for 1 and 2 way switch
for i=1, #Vars,4 do 
    if Vars[i+1] == "2" then
        if string.match("DTPMFHUch",Vars[i+3])  then 
            print (Vars[i] .. "|" ..  Vars[i+1] .. "|" ..  Vars[i+2] .. "|" ..  Vars[i+3] .. " invalid two way Switch")
            print ("Switch link completed in " .. os.clock()-etime)
            return commandArray
        end
        c= #Vars + 1
        Vars[c] = Vars[i+2]
        Vars[c+1] = "1"
        Vars[c+2] = Vars[i]
        Vars[c+3] = Vars[i+3]
    end
     if Vars[i+1] == "1" then
        if Vars[i] == Vars[i+2] then
            if string.match("DTPMFHU",Vars[i+3])  then 
                print (Vars[i] .. "|" ..  Vars[i+1] .. "|" ..  Vars[i+2] .. "|" ..  Vars[i+3] .. " invalid Switch loop")
                print ("Switch link completed in " .. os.clock()-etime)
                return commandArray
            end
        end
     end
end    
-- loop through all the devices
for DomDevice,DeviceValue in pairs(devicechanged) do
-- get IDx number of device
    if q == null then
        print ("Switch Link triggered by " .. DomDevice)
        q = 1
    end
    id = tostring(otherdevices_idx[DomDevice])
--loop through all variables parsed from ST-Setting 
    for i=1,#Vars,4 do
-- if device found action command
        if (Vars[i] == id) then
-- setup counter for number of commandArray updates
            cmd=cmd+1
-- setup fixed variables for device name and action to perform
            action = string.sub(Vars[i+3],1,1)
            Dname = idxname(Vars[i])
            Dname1 = idxname(Vars[i+2])
            if Dname1 == 0 then
                Dname1 = idxvars(Vars[i+2])
            end
-- test for conditinal result
            if string.len(Vars[i+1]) > 0 then 
                Dname2 = idxname(string.sub(Vars[i+1], 2))
                flag = string.sub(Vars[i+1], 1, 1)
                if otherdevices[Dname2] ~= "On" and flag == "T" then 
                    rint("If option triggered result is " .. runflag) 
                    runflag = "F"
                end
                if otherdevices[Dname2] == "On" and flag == "F" then 
                    runflag = "F"
                    rint("If option triggered result is " .. runflag) 
                end
            end
-- duplicate switch 
            if action == "D" and runflag == "T" then
                commandArray[cmd]={[Dname1] = DeviceValue} 
                print (Dname1 .. " Match  " .. Dname)
            end
-- duplicate swith in Domoticz only
            if action == "d" and runflag == "T"  then 
                if DeviceValue ~="Off" then
                    Dvalue = Vars[i+2] .. "|1|on"
                    commandArray[cmd]={["UpdateDevice"] = Dvalue}
                end
                if DeviceValue == "Off" then
                    Dvalue = Vars[i+2] .. "|0|off"
                    commandArray[cmd]={["UpdateDevice"] = Dvalue}
                end
                print (Dname1 .. " Domoticz switch match " .. Dname)
            end
-- Toggle destination switch
            if action == "T" and runflag == "T"  then
                if DeviceValue ~="Off" then
                    Dvalue = idxname(Vars[i+2])
                    commandArray[cmd]={[Dname1] = "Off"}
                    print (Dname1 .. " Off " .. Dname .. " On")
                end
                if DeviceValue == "Off" then
                    commandArray[cmd]={[Dname1] = "On"}
                    print (Dname1 .. " On " .. Dname .. " Off")
                end
            end
-- turn destination switch on as long as source not off
            if action == "P"  and runflag == "T" then
                if DeviceValue ~="Off" then
                    commandArray[cmd]={[Dname1] = "On"}
                    print (Dname1 .. " turn on the same as  " .. Dname)
                end
            end
-- turn destination switch off as long as source not on
            if action == "M" and runflag == "T"  then
                commandArray[cmd]={[Dname1] = "Off"}     
                print (Dname1 .. " turn off the same as  " .. Dname)
            end
-- turn destination switch off as long as source is off
            if action == "m" and runflag == "T"  then
                Dvalue = Vars[i+2] .. "|0|off"
                commandArray[cmd]={["UpdateDevice"] = Dvalue}
                print (Dname1 .. " Domoticz switch off the same as  " .. Dname)
            end
-- Mirror Dim value to destination
            if action == "F" and runflag == "T"  then
                if string.sub(Vars[i+3], 2, 3) == "00" then
                    commandArray[cmd]={[Dname1] = (DeviceValue .. " AFTER 1")}     
                    print (Dname1 .. " dimmed to  the same as  " .. Dname)    
                else
                    dimvalue = ("Set Level:" .. string.sub(Vars[i+2], 2, 3) .. "*") 
                    commandArray[cmd]={[Dname1] = dimvalue}     
                    print (Dname1 .. " dimmed to " .. string.sub(Vars[i+2], 2, 3))    
                end
             end
-- Hides destination switch 
            if action == "H" and runflag == "T"  then
                Dname1 = ("$" .. Dname1)
                url = ("http://127.0.0.1:8080/json.htm?type=command&param=renamedevice&idx=" .. Vars[i+2] .. "&name=" .. Dname1)
                commandArray[cmd]={["OpenURL"] = url}
            end
            if action == "U" and runflag == "T"   then 
                d = tostring(os.date("%x"))
                t = tostring(os.date("%X"))
                l = otherdevices_lastupdate[Dname]
                v = otherdevices_svalues[Dname]
                uvar = string.sub(Vars[i+3],2)
                uvar = string.gsub(uvar, "@d@", d) 
                uvar = string.gsub(uvar, "@t@", t) 
                uvar = string.gsub(uvar, "@i@", Vars[i])
                uvar = string.gsub(uvar, "@n@", Dname)
                uvar = string.gsub(uvar, "@l@", l)
                uvar = string.gsub(uvar, "@v@", v)
                uvar = string.gsub(uvar, " ", "%%20")
                url = ("http://127.0.0.1:8080/json.htm?param=updateuservariable&type=command&vname=" .. Dname1 .."&vtype=2&vvalue=" .. uvar )
                print ("User Variable " .. Dname1 .. " triggered with " .. uvar)  
                commandArray[cmd]={["OpenURL"] = url}
            end
-- Toggle  destination switch on in Domoticz
            if action == "t" and runflag == "T"  then
                if DeviceValue ~="Off" then
                    Dvalue = Vars[i+2] .. "|0|off"
                    commandArray[cmd]={["UpdateDevice"] = Dvalue}
                    print (Dname1 .. " Domoticz switch off " .. Dname .. " On")
                end
                if DeviceValue == "Off" then
                    Dvalue = Vars[i+2] .. "|1|on"
                    commandArray[cmd]={["UpdateDevice"] = Dvalue}
                    print (Dname1 .. " Domoticz switch on " .. Dname .. " Off")
                end
            end
-- Mirror Dim value to destination
            if action == "f" and runflag == "T"  then
                if string.sub(Vars[i+3], 2, 3) == "00" then
                    Dvalue = Vars[i+2] .. "|2|" .. string.sub(DeviceValue, 12, 14)  
                    commandArray[cmd]={["UpdateDevice"] = Dvalue}
                    print (Dname1 .. " dimmed to  the same as  " .. Dname)    
                else
                    Dvalue = Vars[i+1] .. "|2|" .. string.sub(Vars[i+3], 2, 3)   
                    commandArray[cmd]={["UpdateDevice"] = Dvalue}
                    print (Dname1 .. " dimmed to " .. string.sub(Vars[i+3], 2, 3))    
                end
            end
-- turn destination switch in Domoticz on as long as source not off
            if action == "p" and runflag == "T"  then
                if DeviceValue ~="Off" then
                    Dvalue = Vars[i+2] .. "|1|On"
                    commandArray[cmd]={["UpdateDevice"] = Dvalue}
                    print (Dname1 .. " Domoticz switch on the same as  " .. Dname)
                end
            end
-- Shows destination switch 
            if action == "h" and runflag == "T"  then
                url = ("http://127.0.0.1:8080/json.htm?type=command&param=renamedevice&idx=" .. Vars[i+2] .. "&name=" .. Dname1)
                commandArray[cmd]={["OpenURL"] = url}
            end
            if action == "u" and runflag == "T"   then
                d = tostring(os.date("%x"))
                t = tostring(os.date("%X"))
                l = otherdevices_lastupdate[Dname]
                v = otherdevices_svalues[Dname]
                uvar = string.sub(Vars[i+3],2)
                uvar = string.gsub(uvar, "@d@", d) 
                uvar = string.gsub(uvar, "@t@", t) 
                uvar = string.gsub(uvar, "@i@", Vars[i])
                uvar = string.gsub(uvar, "@n@", Dname)
                uvar = string.gsub(uvar, "@l@", l)
                uvar = string.gsub(uvar, "@v@", v)
                Dname1 = ("Variable:" .. Dname1)
                print ("User Variable " .. Dname1 .. " updated with " .. uvar)
                commandArray[cmd]={[Dname1] = uvar}
            end
        end
    end
end
print ("Switch link completed in " .. os.clock()-etime)
return commandArray

Updated features code updated to 1.8 Syntax changed see examples
Last edited by lonebaggie on Wednesday 27 September 2017 0:10, edited 18 times in total.

User avatar
lonebaggie
Posts: 107
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by lonebaggie » Sunday 03 September 2017 10:37

Instructions on use

Create a string user variable called ST-Setting and add the following values

Syntax is :-

source IDx|(1)(2)(T)(F)(IDx)|Destination IDx|Command

where IDx is the switch IDx number . The source switch when triggered will affect the destination switch in (1) way switching. In (2) way switching source and destination can effect each other ( not all switch actions are supported in (2) way switching.

A third switch can be used as option conditional trigger if (T)rue or (F)alse options used. IF third switch true (on) destination switch triggered.

The following commands are used to set switch actions on the destination switch.
  • D Duplicated. ( Source will force the destination switch to match )
    T Toggled ( Source will toggle the destination switch to be the opposite )
    P Plus ( source will change the destination switch to be on)
    M Minus ( source will change the destination switch to be off)
    Fnn Fixed ( source Dim value will mirror the Destination if the nn value set to 00 or fix the value to nn)
    H Hide ( Source will Hide destination switch. h will un-hide hidden switch )
    Ucc. U Variable ( Source will trigger IDx of User Variable (string only) to be replaced with
    characters specified. @t@ will insert time, @d@ date, @i@ Idx, @n@ name of switch
    @l@ last update, @v@ value of switch
Using lowercase commands of the above will only change the switch within Domoticz and will not trigger the action of the switch.

Here are some examples of the type of slave switches you can setup

116|1|116|m

One way Switch. When switch 116 is turned on it will automatically turn off. This will create the equivalent of a push on or push off switch except you can use a standard switch and select different icons see https://www.domoticz.com/forum/viewtop ... =4&t=18228

116|1|117|d|116|1|34|d|116|1|87|d

This will create the equivalent of a scene without triggering the switch actions. So switch 116 will force switch 117,34 and 87 to match 116 settings.

45|1|56|T

One way switch.Sets switch 56 to be the opposite of switch 45

47|1|57|F00

One way switch. Mirrors dimmer switch 47 to dimmer switch 57 also works for selector switches

39|1|57|F67

One way switch . Sets dimmer switch 57 to 67% when switch 39 is triggered

47|1|57|F00|47|1|68|F00|47|1|72|F00

Create Master Dimmer switch 47 to control all other dimmers switches 57,68 and 72

33|1|59|F10

One way.Set selector switch 59 to level 10 when switch 33 triggered

116|1|117|H

Hide switch 117 when switch 116 triggered

116|1|117|h

show hidden switch 117 when switch 116 triggered

98|1|19|USwitch @i@ triggered at @t@

User Variable IDx 19 is triggered by switch 98 ( will start Lua scripts) and contents overwritten with "Switch 98 triggered at 10:34:10"

45|T110|124|D

Switch 124 will match switch 45 when switch 110 is on. If switch 110 is off , switch 45 will have no effect on switch 124

112|2|114|d

Two way switch. Switch 114 matches 112 if 112 triggered. If 114 is triggered 112 will match 114

Obvious switch loops 114|2|115|T and 113|1|113|D are detected by the program and will generate error messages and terminated the script.

hope this helps on how to use
Last edited by lonebaggie on Thursday 28 September 2017 15:25, edited 17 times in total.

User avatar
lonebaggie
Posts: 107
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by lonebaggie » Tuesday 05 September 2017 23:20

Bug

using fnn to change a dimmer switch state without triggering the action , will change switch value but not the slider position.
This is known issue see viewtopic.php?t=14935

has anybody a solution to this ?

User avatar
lonebaggie
Posts: 107
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by lonebaggie » Friday 08 September 2017 23:43

Script updated 1.5 new triggers hide switch and updates user variables

User avatar
lonebaggie
Posts: 107
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by lonebaggie » Monday 18 September 2017 22:32

Script updated to 1.7 . Now has optional If clause . Destination switch will only be affected if a third switch is true or false. Note this is optional, but you must leave empty parameter ||. Existing ST-Setting must be updated or program will fail with ST-Setting invalid Exit script

Also added @l@ and @v@ last update and switch value to User Variables

User avatar
lonebaggie
Posts: 107
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by lonebaggie » Tuesday 26 September 2017 23:58

Script updated to 1.8. Now has some error correction. Can also setup 2 way switches to reduce length and complexity of ST-Setting.

Syntax changed slightly. Switch must be defined as (1) way , (2) way , (T)rue or (F)alse . See script or instructions on use . Both updated.

BuddyRich
Posts: 8
Joined: Friday 04 November 2016 16:26
Target OS: Windows
Domoticz version:
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by BuddyRich » Monday 02 October 2017 0:32

Im having an issue with a garage door contact sensor switch and a door lock switch (virtual) that I am trying to keep in sync. It works 1/2 the time.

Ive made the contact sensor the master (idx 3) and door lock (idx 112) the slave. I want the virtual switch to mirror the state of the contact switch. The virtual switch actually sends a json command to open, which is why I use lower case d. I don't want the switch to trigger, just change its state.

The door itself can be activated via a Domoticz z-wave switch or manually via a remote door opener or via a key for the garage, which causes the virtual switch to go out of sync.

This is my ST-String setting:

3|1|112|d

Here is an example:
To start door is closed. Contact shows closed.

I use the domoticz switch to open. The door opens, the timestamp on all three modify, but the state doesn't match.
Capture-open.PNG
Capture-open.PNG (119.33 KiB) Viewed 2465 times
here's the log.

Code: Select all

2017-10-01 18:12:57.806 User: Admin initiated a switch command (2/Garage Door Switch/On)
2017-10-01 18:12:57.806 OpenZWave: Domoticz has send a Switch command! NodeID: 3 (0x03)
2017-10-01 18:12:57.962 dzVents: Switch Link started
2017-10-01 18:12:57.962 dzVents: Switch Link triggered by Garage Door Switch
2017-10-01 18:12:57.962 dzVents: Switch link completed in 0
2017-10-01 18:12:57.962 (Aeon Labs ZStick) Light/Switch (Garage Door Switch)
2017-10-01 18:12:58.509 dzVents: Switch Link started
2017-10-01 18:12:58.509 dzVents: Switch Link triggered by Garage Door Switch
2017-10-01 18:12:58.509 dzVents: Switch link completed in 0.0010000000038417
2017-10-01 18:12:58.524 (Aeon Labs ZStick) Light/Switch (Garage Door Switch)
2017-10-01 18:12:58.712 dzVents: Switch Link started
2017-10-01 18:12:58.712 dzVents: Switch Link triggered by Garage Door Sensor
2017-10-01 18:12:58.712 dzVents: garage Domoticz switch match Garage Door Sensor
2017-10-01 18:12:58.712 dzVents: Switch link completed in 0.0010000000038417
2017-10-01 18:12:58.759 EventSystem: Script event triggered: Switch-Link-1.8
2017-10-01 18:12:58.774 (Aeon Labs ZStick) Light/Switch (Garage Door Sensor)
2017-10-01 18:13:01.255 Notification sent (email) => Success 
If I use the virtual switch to close (open the door). It is temporarily is out of sync, but once the contact sensor reads closed it sets the virtual switch correctly.
Capture.Sync.PNG
Capture.Sync.PNG (133.67 KiB) Viewed 2465 times
once it hits closed on the sensor:
CaptureSync2.PNG
CaptureSync2.PNG (135.64 KiB) Viewed 2465 times
and the log:

Code: Select all

 2017-10-01 18:24:59.783 User: Admin initiated a switch command (112/garage/Off)
2017-10-01 18:24:59.954 dzVents: Switch Link started
2017-10-01 18:24:59.954 dzVents: Switch Link triggered by garage
2017-10-01 18:24:59.954 dzVents: Switch link completed in 0.0010000000038417
2017-10-01 18:24:59.954 (Dummy) Lighting 1 (garage)
2017-10-01 18:25:00.080 User: Admin initiated a switch command (02/Garage Door Switch/On)
2017-10-01 18:25:00.080 OpenZWave: Domoticz has send a Switch command! NodeID: 3 (0x03)
2017-10-01 18:25:00.314 dzVents: Switch Link started
2017-10-01 18:25:00.314 dzVents: Switch Link triggered by Garage Door Switch
2017-10-01 18:25:00.314 dzVents: Switch link completed in 0.0010000000038417
2017-10-01 18:25:00.329 (Aeon Labs ZStick) Light/Switch (Garage Door Switch)
2017-10-01 18:25:00.783 dzVents: Switch Link started
2017-10-01 18:25:00.783 dzVents: Switch Link triggered by Garage Door Switch
2017-10-01 18:25:00.783 dzVents: Switch link completed in 0.0019999999931315
2017-10-01 18:25:00.798 (Aeon Labs ZStick) Light/Switch (Garage Door Switch)
2017-10-01 18:25:08.267 dzVents: Switch Link started
2017-10-01 18:25:08.267 dzVents: Switch Link triggered by Garage Door Sensor
2017-10-01 18:25:08.267 dzVents: garage Domoticz switch match Garage Door Sensor
2017-10-01 18:25:08.267 dzVents: Switch link completed in 0.0019999999931315
2017-10-01 18:25:08.314 EventSystem: Script event triggered: Switch-Link-1.8
2017-10-01 18:25:08.330 (Aeon Labs ZStick) Light/Switch (Garage Door Sensor)
2017-10-01 18:25:11.035 Notification sent (email) => Success 
Im thinking there is a natural delay (as the door closes) before it triggers closed but changing from closed to open happens too quickly. I don't know.

User avatar
lonebaggie
Posts: 107
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by lonebaggie » Saturday 07 October 2017 22:36

the program can only change id 112 if ID 3 is triggered. Remember all LUA device scripts are activated when a switch is triggered. The last event log show the Garage Door switch triggered the switch link but no action was carried out . The switch link program also runs before the last Aeon labs would this put it out of sync ?

would a two sync help 3|2|112|d or only trigger on third switch ( Id 2 ?) being on 3|T2|112|d

BuddyRich
Posts: 8
Joined: Friday 04 November 2016 16:26
Target OS: Windows
Domoticz version:
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by BuddyRich » Monday 13 November 2017 16:05

I guess that may be the issue. Im trying to duplicate the state of the sensor (not the switch) to the virtual switch. Is a contact sensor considered a switch? It does have an On and Off value and can trigger a script. I want the virtual switch to show open when it senses the door open, and closed when its closed - that way it doesn't matter what I use (actual domo switch, key or remote) to open or close the door.

BuddyRich
Posts: 8
Joined: Friday 04 November 2016 16:26
Target OS: Windows
Domoticz version:
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by BuddyRich » Monday 13 November 2017 20:02

So I did some more testing, just with two virtual switches.

Both door lock types.

garage (idx 112) and garagestate (idx 212).

I used this as my variable:

212|1|112|d

and I manually activated the 212 switch to test. It changes from locked to unlocked, but looking at the log of 112, it just keeps getting the on command, and never an off command.

If I use this as my variable:

212|1|112|t

It just keeps getting off as the command.

Here's the log with two pushes with 212|1|112|d and 2 pushes with 212|1|112|t

Code: Select all

 2017-11-13 12:50:56.140 User: Admin initiated a switch command (212/garagestate/On)
2017-11-13 12:50:56.235 dzVents: Debug: Dumping domoticz data to C:\Program Files\Domoticz\scripts\dzVents\/domoticzData.lua
2017-11-13 12:50:56.266 dzVents: Debug: Event trigger type: device
2017-11-13 12:50:56.329 dzVents: Debug: Device-adapter found for garagestate: Switch device adapter
2017-11-13 12:50:56.344 dzVents: Debug: Processing device-adapter for garagestate: Switch device adapter
2017-11-13 12:50:56.344 dzVents: Debug: Device-event for: garagestate value: Locked
2017-11-13 12:50:56.344 dzVents: Debug: Searching for scripts for changed item: garagestate
2017-11-13 12:50:56.344 dzVents: Switch Link started
2017-11-13 12:50:56.344 dzVents: Switch Link triggered by garagestate
2017-11-13 12:50:56.344 dzVents: garage Domoticz switch match garagestate
2017-11-13 12:50:56.344 dzVents: Switch link completed in 0.0010000000038417
2017-11-13 12:50:56.391 EventSystem: Script event triggered: Switch-Link-1.8
2017-11-13 12:50:56.391 (Dummy) Lighting 1 (garagestate)
2017-11-13 12:51:00.540 dzVents: Debug: Dumping domoticz data to C:\Program Files\Domoticz\scripts\dzVents\/domoticzData.lua
2017-11-13 12:51:00.572 dzVents: Debug: Event trigger type: time
2017-11-13 12:51:24.538 User: Admin initiated a switch command (212/garagestate/Off)
2017-11-13 12:51:24.633 dzVents: Debug: Dumping domoticz data to C:\Program Files\Domoticz\scripts\dzVents\/domoticzData.lua
2017-11-13 12:51:24.664 dzVents: Debug: Event trigger type: device
2017-11-13 12:51:24.727 dzVents: Debug: Device-adapter found for garagestate: Switch device adapter
2017-11-13 12:51:24.742 dzVents: Debug: Processing device-adapter for garagestate: Switch device adapter
2017-11-13 12:51:24.742 dzVents: Debug: Device-event for: garagestate value: Unlocked
2017-11-13 12:51:24.742 dzVents: Debug: Searching for scripts for changed item: garagestate
2017-11-13 12:51:24.742 dzVents: Switch Link started
2017-11-13 12:51:24.742 dzVents: Switch Link triggered by garagestate
2017-11-13 12:51:24.742 dzVents: garage Domoticz switch match garagestate
2017-11-13 12:51:24.742 dzVents: Switch link completed in 0.0020000000076834
2017-11-13 12:51:24.789 EventSystem: Script event triggered: Switch-Link-1.8
2017-11-13 12:51:24.789 (Dummy) Lighting 1 (garagestate)
2017-11-13 12:52:00.032 dzVents: Debug: Dumping domoticz data to C:\Program Files\Domoticz\scripts\dzVents\/domoticzData.lua
2017-11-13 12:52:00.064 dzVents: Debug: Event trigger type: time
2017-11-13 12:53:00.115 dzVents: Debug: Dumping domoticz data to C:\Program Files\Domoticz\scripts\dzVents\/domoticzData.lua
2017-11-13 12:53:00.131 dzVents: Debug: Event trigger type: time
2017-11-13 12:53:49.665 dzVents: Debug: Dumping domoticz data to C:\Program Files\Domoticz\scripts\dzVents\/domoticzData.lua
2017-11-13 12:53:49.680 dzVents: Debug: Event trigger type: uservariable
2017-11-13 12:53:49.711 dzVents: Debug: Variable-event for: ST-Setting value: 212|2|112|t
2017-11-13 12:53:49.711 dzVents: Debug: Searching for scripts for changed item: ST-Setting
2017-11-13 12:53:56.821 User: Admin initiated a switch command (212/garagestate/On)
2017-11-13 12:53:56.915 dzVents: Debug: Dumping domoticz data to C:\Program Files\Domoticz\scripts\dzVents\/domoticzData.lua
2017-11-13 12:53:56.930 dzVents: Debug: Event trigger type: device
2017-11-13 12:53:56.993 dzVents: Debug: Device-adapter found for garagestate: Switch device adapter
2017-11-13 12:53:57.009 dzVents: Debug: Processing device-adapter for garagestate: Switch device adapter
2017-11-13 12:53:57.009 dzVents: Debug: Device-event for: garagestate value: Locked
2017-11-13 12:53:57.009 dzVents: Debug: Searching for scripts for changed item: garagestate
2017-11-13 12:53:57.009 dzVents: Switch Link started
2017-11-13 12:53:57.009 dzVents: Switch Link triggered by garagestate
2017-11-13 12:53:57.009 dzVents: garage Domoticz switch off garagestate On
2017-11-13 12:53:57.009 dzVents: Switch link completed in 0.0010000000038417
2017-11-13 12:53:57.071 EventSystem: Script event triggered: Switch-Link-1.8
2017-11-13 12:53:57.071 (Dummy) Lighting 1 (garagestate)
2017-11-13 12:54:00.173 dzVents: Debug: Dumping domoticz data to C:\Program Files\Domoticz\scripts\dzVents\/domoticzData.lua
2017-11-13 12:54:00.189 dzVents: Debug: Event trigger type: time
2017-11-13 12:54:00.501 User: Admin initiated a switch command (212/garagestate/Off)
2017-11-13 12:54:00.607 dzVents: Debug: Dumping domoticz data to C:\Program Files\Domoticz\scripts\dzVents\/domoticzData.lua
2017-11-13 12:54:00.638 dzVents: Debug: Event trigger type: device
2017-11-13 12:54:00.700 dzVents: Debug: Device-adapter found for garagestate: Switch device adapter
2017-11-13 12:54:00.716 dzVents: Debug: Processing device-adapter for garagestate: Switch device adapter
2017-11-13 12:54:00.716 dzVents: Debug: Device-event for: garagestate value: Unlocked
2017-11-13 12:54:00.716 dzVents: Debug: Searching for scripts for changed item: garagestate
2017-11-13 12:54:00.716 dzVents: Switch Link started
2017-11-13 12:54:00.731 dzVents: Switch Link triggered by garagestate
2017-11-13 12:54:00.731 dzVents: garage Domoticz switch off garagestate On
2017-11-13 12:54:00.731 dzVents: Switch link completed in 0.0019999999931315
2017-11-13 12:54:00.778 EventSystem: Script event triggered: Switch-Link-1.8
2017-11-13 12:54:00.778 (Dummy) Lighting 1 (garagestate) 
It says its matching the state but it doesn't.

Here's the state log from idx 112

Code: Select all

2017-11-13 12:54:00	Off
2017-11-13 12:53:57	Off
2017-11-13 12:51:24	On
2017-11-13 12:50:56	On
and the log from idx 212

Code: Select all

2017-11-13 12:54:00	Off
2017-11-13 12:53:56	On
2017-11-13 12:51:24	Off
2017-11-13 12:50:56	On

User avatar
lonebaggie
Posts: 107
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by lonebaggie » Tuesday 14 November 2017 15:12

Looks like I may have a bug , you are using the lowercase t and d , these will only change the state in Domoticz . You may have to refresh the web interface to confirm the new state. Not sure if the log recognises these types of changes will confirm.

If the destination switch is just a virtual switch with no payload or your own scripts require a logged changed try using uppercase commands. Be very careful of switch loops

If the above does not work, can you create two new virtual on/off switches (keep it simple) and just confirm the above actions on those

vlamofiel
Posts: 3
Joined: Tuesday 12 December 2017 21:06
Target OS: Raspberry Pi
Domoticz version: 3.8153
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by vlamofiel » Tuesday 12 December 2017 21:12

Awesome script lonebaggie,

I've been experimenting with it for a while now, but have two questions. Not sure if this is the right spot, though.
  • I have a remote that only is able to send a ON signal. I would like on every ON signal a (kaku) switch to toggle state. So 'remote ON' results in Kaku ON, on next 'remote ON' Kaku turns OFF. I was assuming this was possible with your script, but didn't find out how.
  • Second I wonder if it's possible to define more than one syntax. I believe Domoticz only allows one user variable with the same name...

User avatar
lonebaggie
Posts: 107
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by lonebaggie » Wednesday 20 December 2017 0:43

vlamofiel thank you glad this script is of use

As your switch is only switching on . it is difficult to know when to toggle the destination switch off . Maybe using a third virtual switch as a count or toggle . Does this option help :-

45|T110|124|D

Switch 124 will match switch 45 when switch 110 is on. If switch 110 is off , switch 45 will have no effect on switch 124

don't understand your second point . You can concatenate switch commands into the ST-Setting Variable (limited to 200 chars ?) separated by the "|"

If you need more variables or commands then this script may not be suitable . Do not try to make very elaborate or complex switch links as this script will be called every time ANY switch is triggered and may slow Domoticz down significantly .



Hope this helps

vlamofiel
Posts: 3
Joined: Tuesday 12 December 2017 21:06
Target OS: Raspberry Pi
Domoticz version: 3.8153
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by vlamofiel » Wednesday 20 December 2017 9:37

Mr. Lonebaggie, thanks for the reply.
I see the difficulty in making that 'conditional' switch. I now have something in Lua, but that indeed does a check if 124 (target/light) is on, if so, it turns it off, when 45 (on-button) receives high again.

The concatenate was not clear for me, nor described above I think. So this mean that one 'command' always consists of 4 'selectors' and therefor three pipes.

Thanks again.

User avatar
lonebaggie
Posts: 107
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by lonebaggie » Wednesday 20 December 2017 21:14

Glad your sorted

Yes commands are concatenated as you described , like all programmers , we should be banned from writing any user documentation :)
You described in one sentence what I failed convey in numerous examples . At least if anybody else make use of this script it will be clear

Wob76
Posts: 121
Joined: Wednesday 19 April 2017 6:31
Target OS: Linux
Domoticz version:
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by Wob76 » Friday 29 December 2017 12:57

Hi,

Interesting in using your script to sync up some Dummy switches that link back to my lighting system, I have Dummy Switches for Groups\Rooms, these link back to groups on the system that operate all the lights together, I don't read back the dimmer values of lights so it would be nice to feed any function on the group device to all the devices in the group, so domoticz will more closely resemble the state of the devices, since the group device has already auctioned the lights (via MQTT) I don't want the devices to trigger a MQTT message, and as such I am using lower case letters, I can live with the dimmer not moving.

I also want a 2 way sync, as I can't read back my group status, so I want to trust the devices in it, plus, I prefer the group to show on if any 1 device in it is on.

So, my question is this, if I use the f00 action, it doesn't seem to mirror ON\OFF states, easy fixed by added a second set for d, but it seems a bit excessive, since why would you mirror the dimmer and not the state?

So I have a Group device 121 and 2 devices in that group 122 and 123, I had success with the following line.

Code: Select all

121|2|122|f00|121|2|122|d|121|2|123|f00|121|2|123|d
Playing around with dimmer values and state on all devices seems to function how I would like, so I wanted to expand to all the lights in my system, this is a problem, as my string is too long for the user variable. I have 6 groups, some with 2 bulbs, some with 4, so I have a LONG line, if I wasn't requiring both f00 and d, it would fit, but at the moment it is a no go, is the a work around, or is there a limit on the number of switches the script will handle?

My current line, that is too long.

Code: Select all

121|2|122|f00|121|2|122|d|121|2|123|f00|121|2|123|d|129|2|125|f00|129|2|125|d|129|2|126|f00|129|2|126|d|129|2|127|f00|129|2|127|d|129|2|128|f00|129|2|128|d|130|2|131|f00|130|2|131|d|130|2|132|f00|130|2|132|d|152|2|148|f00|152|2|148|d|152|2|149|f00|152|2|149|d|152|2|150|f00|152|2|150|d|152|2|151|f00|152|2|151|d|153|2|154|f00|153|2|154|d|153|2|155|f00|153|2|155|d|153|2|156|f00|153|2|156|d|153|2|157|f00|153|2|157|d|162|2|163|f00|162|2|163|d|162|2|164|f00|162|2|164|d

Wob76
Posts: 121
Joined: Wednesday 19 April 2017 6:31
Target OS: Linux
Domoticz version:
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by Wob76 » Friday 29 December 2017 13:52

OK, a little more testing, the issue with f00 not mirroring the state is the same issue as the dimmer, you can see an "off" in the logs, but the switch still shows as On. It is not just a visual issue though as it appears they also still show the wrong state in JSON.

An off switch shows as
Status" : "Set Level: 52 %",

So this is a bigger issue than I had hoped, as I use a JSON query in my lighting interface, do you know that state of this bug, or any other way to update the device without double up on lines?

I can avoid the variable length issue by placing my line into your script in place of the variable call, but just wondering how the script will handle that many switches?

Thanks,
Wob

User avatar
lonebaggie
Posts: 107
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by lonebaggie » Tuesday 02 January 2018 0:18

Wow. I never envisioned this script to handle that many switches . The script is not only limited by the user variable size limit , but the way Domoticz works . This script will be called when ever anything changes. So the longer the string the longer the script will take to run.

I found the following issue
Bug

using fnn to change a dimmer switch state without triggering the action , will change switch value but not the slider position.
This is known issue see viewtopic.php?t=14935

has anybody a solution to this ?
is this a similar issue ?

Unfortunately due to other issues I have been experiencing with my setup , I am moving to the dark-side and am transferring everything to Home-assistant , so I wont be monitoring these forums much anymore.

Wob76
Posts: 121
Joined: Wednesday 19 April 2017 6:31
Target OS: Linux
Domoticz version:
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by Wob76 » Tuesday 02 January 2018 3:33

Yeah, the issue seems to be linked to that bug, but a bigger issue as the JSON value is also left unchanged, sort of breaks it for me. I think I'll just look at a solution inside the node I am using to control the lights.

Good luck with HASS.

simon_rb
Posts: 689
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Script to enhance Switch Sub/Slave options

Post by simon_rb » Friday 06 April 2018 19:04

Great script, thank you. Is there a way to mirror 2 way the dimmer status but not the on/off side?

Many Thanks

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests