multi switches off another switch on

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

Moderator: leecollings

Post Reply
anno
Posts: 36
Joined: Wednesday 01 March 2017 13:00
Target OS: Windows
Domoticz version: all
Contact:

multi switches off another switch on

Post by anno »

I want to turn on a virtual switch on when switch 1 to 10 are all off? and the virtual switch off when more that 2 switches are on.

So
switch 1 is off
switch 2 is off
switch 3 is off
switch 4 is off
switch 5 is off
switch 6 is off
switch 7 is off
switch 8 is off
switch 9 is off
switch 10 is off

then

virtual switch on

are there more than 3 switches on

then

virtual switch off

How do i do that? i have a dzvents script running for one switch and on a thermostat and temperature sensor. i don't know where te begin, and i did read the wiki.

Code: Select all

--------------------------------------------------------------------------------------------------------------------------------
-- assumptions:
-- the setpoint is set by a dummy device (not a selector type)

local WP_SWITCH = 'Zolder' -- switch device
local TEMP_SETPOINT = 'Thermostaat zolder' -- selector dummy device
local TEMP_SENSOR_1 = 'Zolder temp'
local BAT_THRESHOLD = 30
local LOGGING = true

return
{
    on =
    {
        timer =
        {
            'every 10 minutes',
        },
    },

    --LOG levell: This is the log level you want for this script. Can be domoticz.LOG_INFO, domoticz.LOG_MODULE_EXEC_INFO, domoticz.LOG_DEBUG or domoticz.LOG_ERROR
    --marker: A string that is prefixed before each log message. That way you can easily create a filter in the Domoticz log to see just these messages.
    logging =
    {
        level = LOGGING and domoticz.LOG_DEBUG or domoticz.LOG_ERROR,
        --level = LOGGING and domoticz.LOG_ERROR,
        marker = 'dzVents Zolder',
    },

    execute = function(dz)

        -- collect all input data
        local verdeler_switch_state = dz.devices(WP_SWITCH).state
        local temp_1 = dz.devices(TEMP_SENSOR_1).temperature



        -- info only on log level = LOG_DEBUG
        dz.log('WP_SWITCH : ' .. verdeler_switch_state, dz.LOG_DEBUG)
        dz.log('Temp_1 : ' .. temp_1, dz.LOG_DEBUG)


        
        -- now determine what to do
        if (temp_1 >= setpointValue) then
            dz.devices(WP_SWITCH).switchOff().afterMin(2).silent()
            dz.log('Target temperature reached, group off')
        elseif (temp_1 <= (setpointValue -0.3)) then
            dz.log('Average temperature more than 0.3 degree below setpointValue, switchOn')
            dz.devices(WP_SWITCH).switchOn().afterMin(2).silent()

           
        end

    end
}
long time domiticz user, running 8 domoticz installations on vm's nuc i3. And espeay hosting more than 30.
willemd
Posts: 621
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: multi switches off another switch on

Post by willemd »

Probably simplest way is to define a variable called NrOfSwitchesOn and then test every switch (with an if statement) one by one.
If the switch is On you add 1 to the variable NrOfSwitchesOn.

After testing all switches, you check the variable.
If the variable is 0 then switch the virtual switch off.
If the variable >3, then switch the virtual switch on.
(what to do if variable is 1,2, or 3? Do nothing? Leave switch as-is?)
anno
Posts: 36
Joined: Wednesday 01 March 2017 13:00
Target OS: Windows
Domoticz version: all
Contact:

Re: multi switches off another switch on

Post by anno »

Code: Select all

local waarde = 'Hoeveel_groepen_open'
local device0 = 'test 0'
local device1 = 'test 1'
local device2 = 'test 2'
local device3 = 'test 3'


return {

	on = {

		devices = {
			'device1' or 'device2' or 'device3' 
		},


		variables = {
			'waarde'
		},

	},


	-- custom logging level for this script
	logging = {
        level = domoticz.LOG_DEBUG,
        marker = "test"
    },

	-- actual event code
	-- the second parameter is depending on the trigger
	-- when it is a device change, the second parameter is the device object
	-- similar for variables, scenes and groups and httpResponses
	-- inspect the type like: triggeredItem.isDevice
	execute = function(domoticz, triggeredItem, info)
		--[[

		The domoticz object holds all information about your Domoticz system. E.g.:

		local myDevice = domoticz.devices('myDevice')
		local myVariable = domoticz.variables('myUserVariable')
		local myGroup = domoticz.groups('myGroup')
		local myScene = domoticz.scenes('myScene')

		The device object is the device that was triggered due to the device in the 'on' section above.
		]] --

		-- example

		if (device1.On) then -- state == 'On'
			dz.variables('waarde').set('+1')
		elseif (device2.On) then -- state == 'On'
			dz.variables(setupvalue +1)
		elseif (device3.On) then -- state == 'On'
			dz.variables(setupvalue +1)
		elseif (device1.Off) then -- state == 'On'
			dz.variables(setupvalue -1)
		elseif (device2.Off) then -- state == 'On'
			dz.variables(setupvalue -1)
		elseif (device3.Off) then -- state == 'On'
			dz.variables(setupvalue -1)
		end
	end
}
I can't get the + to work, get errors, and in the wiki can't find any info about how to change a (user) variable value
long time domiticz user, running 8 domoticz installations on vm's nuc i3. And espeay hosting more than 30.
willemd
Posts: 621
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: multi switches off another switch on

Post by willemd »

Try this:

Code: Select all

local device0 = 'test 0'
local device1 = 'test 1'
local device2 = 'test 2'
local device3 = 'test 3'
        
return {

	on = {

		devices = {
			device0,
			device1,
			device2,
			device3 
		},

	},


	-- custom logging level for this script
	logging = {
        level = domoticz.LOG_INFO,
        marker = "test"
    },

	
	execute = function(dz, device)
		
        local resultDevice = 'resultdevice'
		local waarde=0
		
		dz.log('device0 is '.. dz.devices(device0).state,dz.LOG_INFO)
		dz.log('device1 is '.. dz.devices(device1).state,dz.LOG_INFO)
		dz.log('device2 is '.. dz.devices(device2).state,dz.LOG_INFO)
		dz.log('device3 is '.. dz.devices(device3).state,dz.LOG_INFO)
		
		if (dz.devices(device0).state == 'On') then
			waarde=waarde+1
		end	
		if (dz.devices(device1).state == 'On') then
			waarde=waarde+1
		end	
		if (dz.devices(device2).state == 'On') then
			waarde=waarde+1
		end	
		if (dz.devices(device3).state == 'On') then
			waarde=waarde+1
		end	
		dz.log('waarde is '.. waarde,dz.LOG_INFO)
		dz.log('result switch was '.. dz.devices(resultDevice).state,dz.LOG_INFO)
		if waarde>3 then
		    dz.log('waarde > 3',dz.LOG_INFO)
		    dz.devices(resultDevice).switchOff()
		else
		    if waarde==0 then
		        dz.log('waarde == 0',dz.LOG_INFO)
		        dz.devices(resultDevice).switchOn()
		    else
		        dz.log('0<waarde<=3',dz.LOG_INFO)
		    end
	    end   
	    
	end
}
anno
Posts: 36
Joined: Wednesday 01 March 2017 13:00
Target OS: Windows
Domoticz version: all
Contact:

Re: multi switches off another switch on

Post by anno »

Great this works, now i am going to implement it in the real situation. It is a whole lot different than i could think of.
long time domiticz user, running 8 domoticz installations on vm's nuc i3. And espeay hosting more than 30.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest