Page 1 of 1

Delayed switching

Posted: Monday 09 November 2020 11:49
by wahrens
I'm looking for a solution of the following idea:

Actually i'm using a door switch to shut my heating if the door is open and the outside temperatur is less than 15° C.

Code: Select all

return {
	on = {
		devices = {
			'Tuer Wohnzimmer',
			'Zigbee Terrassentuer'
		}
	},
	execute = function(domoticz, device)
	    local WZHeat = domoticz.devices('WZ Heat')
	    local WZThermostat = domoticz.devices('WZ Thermostat');
	    local OAT = domoticz.devices('THB Darksky'); 
 	    if OAT.temperature < 15 then
    		if device.state == 'Open' then
                domoticz.log(device.name .." is now Open; setting Mode to Off")  
                WZThermostat.updateMode('Off')
                -- WZHeat.updateSetPoint(16)
            elseif device.state == 'Closed' then
                domoticz.log(device.name .." is now Closed; setting Mode to Heat " )
                WZThermostat.updateMode('Heat')
            end
        end
  end
}
This works fine, but i now look for a solution to only shut the heating if the door is not closed within 30 seconds or is open more than 30 seconds which is the same, but i dont know how to fix the timing problem.
Thanks for any idea

Re: Delayed switching

Posted: Monday 09 November 2020 12:05
by Matthiasfr
In Lua you can request current time. As soon as the door opens you can store this time. Maybe you can use a while loop that constantly checks current time. As soon as 30s are passend, you want to go out of the loop and re evaluate if the door is still open.

Re: Delayed switching

Posted: Monday 09 November 2020 12:19
by waaren
wahrens wrote: Monday 09 November 2020 11:49 i now look for a solution to only shut the heating if the door is not closed within 30 seconds or is open more than 30 seconds which is the same, but i dont know how to fix the timing problem.
Can you try with below script?

Code: Select all

local scriptVar = 'delayedMode'

return
{
    on =
    {
        devices =
        {
            'Tuer Wohnzimmer',
            'Zigbee Terrassentuer',
        },
        customEvents =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- set to domoticz.LOG_ERROR when all OK
        marker = scriptVar,
    },

    execute = function(dz, item)
        local WZHeat = dz.devices('WZ Heat')
        local WZThermostat = dz.devices('WZ Thermostat')
        local OAT = dz.devices('THB Darksky')
        local wohnZimmerTuer = dz.devices('Tuer Wohnzimmer')
        local terrassenTuer = dz.devices('Zigbee Terrassentuer')
        local delay = 30

        local doorOpen = wohnZimmerTuer.state == 'Open' or terrassenTuer.state == 'Open'

        if OAT.temperature < 15 then
            if item.isDevice then
                if not(doorOpen) then
                    dz.log('All doors closed; setting Mode to Heat', dz.LOG_DEBUG )
                    WZThermostat.updateMode('Heat')
                else
                    dz.log('At least one door is open. I will check again in ' .. delay .. ' seconds', dz.LOG_DEBUG)
                    dz.emitEvent(scriptVar).afterSec(delay)
                end
            elseif doorOpen then
                dz.log('At least one door is still open; setting Mode to Off')
                WZThermostat.updateMode('Off')
                -- WZHeat.updateSetPoint(16)
            else
                dz.log('No action needed. (Door closed during delay)', dz.LOG_DEBUG )
            end
        else
            dz.log('No action needed. (temperature > 15)', dz.LOG_DEBUG )
        end
  end
}

Re: Delayed switching

Posted: Monday 09 November 2020 12:23
by Matthiasfr
Off course. You can simply use the delay functionality. :|

Re: Delayed switching

Posted: Monday 09 November 2020 12:27
by waaren
Matthiasfr wrote: Monday 09 November 2020 12:05 In Lua you can request current time. As soon as the door opens you can store this time. Maybe you can use a while loop that constantly checks current time. As soon as 30s are passend, you want to go out of the loop and re evaluate if the door is still open.
Please don't do this. It will block the domoticz eventSystem (classic Lua, dzVents and Blockly scripts) for the duration of the loop.

Re: Delayed switching  [Solved]

Posted: Monday 09 November 2020 12:43
by wahrens
Thanks Waaren and Matthias !

Waarens script ist working fine.

Where can i find any documentation about: dz.emitEvent

Re: Delayed switching

Posted: Monday 09 November 2020 14:00
by waaren
wahrens wrote: Monday 09 November 2020 12:43 Where can i find any documentation about: dz.emitEvent
This wiki page seems to be a good start..