Page 1 of 1

help with possible script

Posted: Friday 05 January 2024 10:02
by Dave21w
Would it be possible to write a script so that 2 or more switches are syncronised based on the the last state of change so assuming all are off if one is turned on the others follow and the reverse of this.

Thanks

Re: help with possible script

Posted: Friday 05 January 2024 10:22
by psubiaco
You can try this lua - device script, that you have to save as
scripts/lua/script-device-switches.lua

Code: Select all

commandArray = {}

switches= { "SwitchName1", "SwitchName2", "SwitchName3" }   -- list of switch devices

-- loop through all the changed devices
for deviceName,deviceValue in pairs(devicechanged) do
    if (deviceName in pairs(switches)) then 
        -- a switch changes state => sync all switches
        for dev in pairs(switches) do
            if (otherdevices[dev]~=deviceValue) then
                commandArray[dev]=deviceName
            end
        end
    end
end

return commandArray

Re: help with possible script

Posted: Friday 05 January 2024 11:24
by Dave21w
Wow, that was quick, many thanks. I will give it a go over the weekend when I'm home and see how I get on.

Thanks
Dave

Re: help with possible script

Posted: Friday 05 January 2024 13:34
by willemd
Dave21w wrote: Friday 05 January 2024 10:02 Would it be possible to write a script so that 2 or more switches are syncronised based on the the last state of change so assuming all are off if one is turned on the others follow and the reverse of this.

Thanks
I don't know LUA, but known dzVents.
But my first question is what needs to be done if you run the script and you find that for example 2 out of 5 are off and 3 out of 5 are on.
Should then all go on or all go off? You need to know where they were coming from.
Do you know the previous state? Using dzVents you could store the previous state in a persistent variable.

Re: help with possible script

Posted: Sunday 07 January 2024 10:49
by Dave21w
I’ve not had chance to try the Lua script yet however here is my real world scenario. I only need to sync 2 switches, I have a ground floor dual gang switch, the right sw controls my hall light and the left my first floor landing light, obviously on my landing there is also a switch, this is also a dual as the right sw does the landing and the left does the bathroom light. The switches have an led which is On when the switch is off, either sw module will work even if it is only powered on one side so the ground floor left simply needs to be a remote for the first floor right but have to be sync’d due to the led. If they happen to get out of sync it’s not a major issue but I suppose all off is the best solution.

Thanks

Re: help with possible script

Posted: Sunday 07 January 2024 17:35
by willemd
Dave21w wrote: Sunday 07 January 2024 10:49 I’ve not had chance to try the Lua script yet however here is my real world scenario. I only need to sync 2 switches, I have a ground floor dual gang switch, the right sw controls my hall light and the left my first floor landing light, obviously on my landing there is also a switch, this is also a dual as the right sw does the landing and the left does the bathroom light. The switches have an led which is On when the switch is off, either sw module will work even if it is only powered on one side so the ground floor left simply needs to be a remote for the first floor right but have to be sync’d due to the led. If they happen to get out of sync it’s not a major issue but I suppose all off is the best solution.

Thanks
So your ground-floor-left and first-floor-right are in what is called a "hotel switch" setup which in the past used to be done by wiring only. (the other switches are irrelevant in the story here).
I understand yours can be triggered either manually or by software? Correct?
And the leds, can they be controlled by software? And each switch has its own led?

Re: help with possible script

Posted: Monday 08 January 2024 10:47
by HvdW
Not exactly what you want. Here is a dzvents example. It is triggered by switching Switch 3.
Have a look at the dzvents wiki , search for switch to see more examples.

Code: Select all

return
{
    on =
    {
        devices =
        {
            'Switch 3', -- 'Switch 1', 'Switch 2'  add whatever you like to add
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'Switch control',
    },

    execute = function(domoticz, item)
        -- domoticz.devices('Switch 3').dump()
        domoticz.log(domoticz.devices('Switch 3').dump(),domoticz.LOG_DEBUG)
	domoticz.log(domoticz.devices('Switch 3').state,domoticz.LOG_DEBUG)
	
        if domoticz.devices('Switch 3').state == 'Off' then
            domoticz.devices('Switch 4').switchOff()
        elseif domoticz.devices('Switch 3').state == 'On' then
            domoticz.devices('Switch 4').switchOn()
        end
    end
}

Re: help with possible script

Posted: Tuesday 09 January 2024 9:13
by Dave21w
@psubiaco
Finally got around to trying the script you kindly wrote for me, have edited the switch names but I have an error I don't understand, there is an X against a line in the script (see image) but the Domoticz log mentions a different line no.
lua error.png
lua error.png (12.7 KiB) Viewed 12874 times
2024-01-09 08:06:26.951 Error: EventSystem: in Script #2: [string "commandArray = {} ..."]:7: ')' expected near 'in'

I have tried some online Lua editors and they report the same as the Domoticz log but as I see it the bracket number are correct.
Any help would be great

Thanks

Re: help with possible script

Posted: Tuesday 09 January 2024 17:16
by psubiaco
Sorry, it's never a good idea to send code without trying it.
This is the corrected code (but I never tried it, so maybe it has some mistakes).

Code: Select all

commandArray = {}

switches= { "SwitchName1", "SwitchName2", "SwitchName3" }   -- list of switch devices

-- loop through all the changed devices
for deviceName,deviceValue in pairs(devicechanged) do
    if (deviceName in pairs(switches)) then 
        -- a switch changes state => sync all switches
        for k,dev in pairs(switches) do
            if (otherdevices[dev]~=deviceValue) then
                commandArray[dev]=deviceValue
            end
        end
    end
end

return commandArray

Re: help with possible script

Posted: Tuesday 09 January 2024 21:57
by willemd
This might be a dzVents alternative, also NOT tested.

Code: Select all

-- this assumes the LED state automatically is the reverse of the switch state
-- this also assumes the switch state can be changed both manually and by software
return {
	on = {
		devices = {
			'groundFloorSwitch',
			'firstFloorSwitch',
		}
	},
	data = {
	        previousstate = {initial='Off'},
    },  
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'template',
	},
	execute = function(domoticz, device)
		domoticz.log('Device ' .. device.name .. ' was changed', domoticz.LOG_INFO)
		local currentGroundState=domoticz.devices('groundFloorSwitch').state 
		local currentFirstState=domoticz.devices('firstFloorSwitch').state
		if domoticz.data.previousstate=='Off' then
		    -- previoustate was off and therefore the light and both switches were off
		    if currentGroundState=='On' or currentFirstState=='On' then
		        -- but now one of the two or both have been switched on
		        domoticz.devices('landingLight').switchOn()
		        domoticz.data.previousstate='On'
		        domoticz.devices('groundFloorSwitchh').switchOn()
		        domoticz.devices('firstFloorSwitch').switchOn()
		    end
	    else
	        -- previoustate was on and therefore the ligth and both switches were on
		    if currentGroundState=='Off' or currentFirstState=='Off' then
		        -- but now one of the two or both have been switched off
		        domoticz.devices('landingLight').switchOff()
		        domoticz.data.previousstate='Off'
		        domoticz.devices('groundFloorSwitch').switchOff()
		        domoticz.devices('firstFloorSwitch').switchOff()
		    end
		end   
	end
}

Re: help with possible script

Posted: Wednesday 10 January 2024 9:13
by Dave21w
A big thank you to all who have replied, unfortunately I couldn't get the Lua script to work however with a few name changes to suit my actual switches the code from @willemd works perfectly.
Thanks again guys :)

Re: help with possible script

Posted: Wednesday 10 January 2024 11:53
by PierreT
I run this myself to replicate switch states between different types of hardware:

Code: Select all

return {
	on = {
		devices = {
			12,     -- switch 1
			16,     -- switch 2
		}
	},
	logging = {
		level = domoticz.LOG_INFO,
	},
	execute = function(dz, device)
    	if (device.id == 12) then
	        slaveid = 1352   -- switch 3
	    elseif (device.id == 16) then
	        slaveid = 1353   -- switch 4
	    end
	    local masterstate = device.state
	    local slavestate = dz.devices(slaveid).state
	    dz.log('Master: ' .. masterstate .. ', Slave: ' .. slavestate, dz.LOG_INFO)
		if not (masterstate == slavestate) then
		    if masterstate == "On" then
        	    dz.log('switching slave on', dz.LOG_INFO)
		        dz.devices(slaveid).switchOn()
		    else
        	    dz.log('switching slave off', dz.LOG_INFO)
		        dz.devices(slaveid).switchOff()
		    end
		end
	end
}
This is dzVents and the script as is will only handle one slave device, but it could be extended with a for loop to run through a list of slave devices.

Re: help with possible script

Posted: Wednesday 10 January 2024 15:21
by boum

Code: Select all

if (deviceName in pairs(switches)) then
this is not valid lua syntax.