Page 1 of 1

How to reset an timer

Posted: Wednesday 01 June 2022 13:14
by Fredom
Dear members,
I need some help
The purpose of this script is that when the sensor turns on, it turns on the lamp.
The lamp may only go out after 10 minutes if the sensor is also off.
So as long as the sensor is on, the lamp must also remain on.
But if the sensor switches off and on again within 10 min, the 10 min must be restarted.
Now the lamp always switches off after 10 minutes and then immediately on again.
Does anyone have the solution for me.
Or is this not possible

Code: Select all

return
{
    on =
    {
        devices = { 
                     'Lamp',
                     'Sensor',
                  },

    },
   execute = function(dz)
        local  sensor = dz.devices('Sensor')
        local     lamp = dz.devices('Lamp') 

    if sensor.state == 'On' then
        lamp.switchOn().silent()
    end
    
    if sensor.state == 'Off' then
        lamp.switchOff().afterMin(10)
    end
end

Re: How to reset an timer

Posted: Wednesday 01 June 2022 15:46
by plugge
Could you try this (tested with dummy devices):

Code: Select all

local lightOnMin = 10 -- How long you want the light to burn after the sensor turned off (maybe try 2 minutes first)

return
{
    on =
    {
        customEvents = { 'checkSensor',
                       },
        timer = { 'every minute'
                },
        devices = { 
                     'Lamp',
                     'Sensor',
                  },

    },
   execute = function(dz, item)
        local  sensor = dz.devices('Sensor')
        local    lamp = dz.devices('Lamp') 

    if item.isDevice and
       item.name == 'TestSensor' and
       sensor.state == 'On' 
    then
        lamp.switchOn()
        dz.emitEvent('checkSensor')
    end
    
    if item.isCustomEvent and 
       sensor.state == 'Off' and
       sensor.lastUpdate.minutesAgo == lightOnMin
    then
        lamp.switchOff()
    else
        dz.emitEvent('checkSensor')
    end
end
}

Re: How to reset an timer

Posted: Wednesday 01 June 2022 17:36
by Fredom
plugge wrote: Wednesday 01 June 2022 15:46 Could you try this (tested with dummy devices):

Code: Select all

local lightOnMin = 10 -- How long you want the light to burn after the sensor turned off (maybe try 2 minutes first)

return
{
    on =
    {
        customEvents = { 'checkSensor',
                       },
        timer = { 'every minute'
                },
        devices = { 
                     'Lamp',
                     'Sensor',
                  },

    },
   execute = function(dz, item)
        local  sensor = dz.devices('Sensor')
        local    lamp = dz.devices('Lamp') 

    if item.isDevice and
       item.name == 'TestSensor' and
       sensor.state == 'On' 
    then
        lamp.switchOn()
        dz.emitEvent('checkSensor')
    end
    
    if item.isCustomEvent and 
       sensor.state == 'Off' and
       sensor.lastUpdate.minutesAgo == lightOnMin
    then
        lamp.switchOff()
    else
        dz.emitEvent('checkSensor')
    end
end
}
Hi plugge,
Thanks for your quick response.
It almost works well.
I made your proposal and set the timer to 2 min and also tested with 2 dummys.
If I turn the sensor off and on once within 2 minutes, it works fine.
If I switch the sensor off and on several times, the lamp switches off immediately when the sensor also switches off

Re: How to reset an timer

Posted: Wednesday 01 June 2022 17:48
by plugge
Hm. In my case the behavior is different, but the script generates far too many events, so that's not good.
Better disable it and throw away :-/
Maybe I find a better solution, later.

Re: How to reset an timer

Posted: Wednesday 01 June 2022 18:49
by Fredom
Okay,
Thanks for your help for now.
I hope you or someone else has a solution for this

Re: How to reset an timer

Posted: Wednesday 01 June 2022 20:52
by plugge
Okey, one more try. A bit simpler, albeit less accurate as to how long the light remains on 10 min -> 11-12 min.
But that should not be a problem, if I understood correctly what you're trying to achieve.
Can you try this:

Code: Select all

local lightOnMin = 2  --(the light will stay on for at least 3 min)

--[[
Minimum = 2 minutes (it takes at least 2x 1 minute events to check the script twice
So, when you specfy the light to stay on for 10 minutes after the sensor turned off,
the light will stay on for 11 minutes, depending on how busy the system is with other events.
Everytime you turn the sensor on and then off, the timing starts from 0.
--]]

return
{
    on =
    {
        timer = { 'every minute'
                },
        devices = { 
                     'Lamp',
                     'Sensor',
                  },

    },
   execute = function(dz, item)
        local  sensor = dz.devices('Sensor')
        local     lamp = dz.devices('Lamp') 

    if item.isDevice and
       item.name == 'Sensor' and
       sensor.state == 'On' 
    then
        lamp.switchOn().checkFirst()
    end
    
    if item.isTimer and 
       sensor.state == 'Off' and
       sensor.lastUpdate.minutesAgo >= lightOnMin
    then
       lamp.switchOff()
    end
end
}
If this fails, I throw in the towel...

Re: How to reset an timer

Posted: Wednesday 01 June 2022 21:41
by Fredom
plugge wrote: Wednesday 01 June 2022 20:52 Okey, one more try. A bit simpler, albeit less accurate as to how long the light remains on 10 min -> 11-12 min.
But that should not be a problem, if I understood correctly what you're trying to achieve.
Can you try this:

Code: Select all

local lightOnMin = 2  --(the light will stay on for at least 3 min)

--[[
Minimum = 2 minutes (it takes at least 2x 1 minute events to check the script twice
So, when you specfy the light to stay on for 10 minutes after the sensor turned off,
the light will stay on for 11 minutes, depending on how busy the system is with other events.
Everytime you turn the sensor on and then off, the timing starts from 0.
--]]

return
{
    on =
    {
        timer = { 'every minute'
                },
        devices = { 
                     'Lamp',
                     'Sensor',
                  },

    },
   execute = function(dz, item)
        local  sensor = dz.devices('Sensor')
        local     lamp = dz.devices('Lamp') 

    if item.isDevice and
       item.name == 'Sensor' and
       sensor.state == 'On' 
    then
        lamp.switchOn().checkFirst()
    end
    
    if item.isTimer and 
       sensor.state == 'Off' and
       sensor.lastUpdate.minutesAgo >= lightOnMin
    then
       lamp.switchOff()
    end
end
}
If this fails, I throw in the towel...
Hi plugge,
It works perfectly now.
thank you for your effort