help with possible script Topic is solved

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
Dave21w
Posts: 381
Joined: Sunday 29 November 2015 21:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: UK
Contact:

help with possible script

Post 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
User avatar
psubiaco
Posts: 222
Joined: Monday 20 August 2018 9:38
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Italy
Contact:

Re: help with possible script

Post 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
Paolo
--
I use DomBus modules to charge EV car, get a full alarm system, control heat pump, fire alarm detection, lights and much more. Video
Facebook page - Youtube channel
Dave21w
Posts: 381
Joined: Sunday 29 November 2015 21:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: UK
Contact:

Re: help with possible script

Post 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
willemd
Posts: 642
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: help with possible script

Post 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.
Dave21w
Posts: 381
Joined: Sunday 29 November 2015 21:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: UK
Contact:

Re: help with possible script

Post 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
willemd
Posts: 642
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: help with possible script

Post 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?
HvdW
Posts: 612
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: help with possible script

Post 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
}
Bugs bug me.
Dave21w
Posts: 381
Joined: Sunday 29 November 2015 21:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: UK
Contact:

Re: help with possible script

Post 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 12855 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
User avatar
psubiaco
Posts: 222
Joined: Monday 20 August 2018 9:38
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Italy
Contact:

Re: help with possible script

Post 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
Paolo
--
I use DomBus modules to charge EV car, get a full alarm system, control heat pump, fire alarm detection, lights and much more. Video
Facebook page - Youtube channel
willemd
Posts: 642
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: help with possible script

Post 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
}
Dave21w
Posts: 381
Joined: Sunday 29 November 2015 21:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: UK
Contact:

Re: help with possible script

Post 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 :)
PierreT
Posts: 51
Joined: Wednesday 03 May 2023 10:12
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: help with possible script

Post 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.
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: help with possible script

Post by boum »

Code: Select all

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

Who is online

Users browsing this forum: Google [Bot] and 1 guest