Page 1 of 1

Two devices triggers + time

Posted: Saturday 30 November 2019 22:26
by besix
Hello
I have two small scripts they make light in the kitchen in the morning.
PIR1 - motion sensor
Light 1 - dummy switch

Code: Select all

return {
   on = {
      devices = { ['PIR 1'] = {'between 06:30 and 08:00'}}
    },
   execute = function(dz, item)
       if (item.state == 'On') then
	     dz.devices('Light led').switchOn()
	     dz.devices('Radio').switchOn().afterSec(10)
         dz.log ('Radio and LED = ON')
        end     
   end
}  
second script

Code: Select all

return {
   on = {
      devices = { ['Light 1'] = {'between 06:30 and 08:00'}}
    },
   execute = function(dz, item)
       if (item.state == 'On') then
	     dz.devices('Light led').switchOff()
	     dz.log ('Main light kitchen = ON')
        end     
   end
}  
How to combine it into one script?

Re: Two devices triggers + time

Posted: Sunday 01 December 2019 0:40
by waaren
besix wrote: Saturday 30 November 2019 22:26 I have two small scripts they make light in the kitchen in the morning.
How to combine it into one script?
Like this ? (not tested)

Code: Select all

local motion = 'PIR 1'
local light = 'Light 1'
local morning = 'at 06:35-08:00'

return 
{
    on = 
    {
        devices = 
        { 
            [motion] = { morning }, 
            [light] = { morning },
        },
    },

   execute = function(dz)
        local isMotion = dz.devices(motion).active
        local lightOn = dz.devices(light).active
        local ledLight = dz.devices('Light led')
        
        if isMotion then
            dz.log ('Motion detected')
            if not(lightOn) then ledLight.switchOn().checkFirst() end
            dz.devices('Radio').switchOn().afterSec(10)
        end    
        
        if lightOn then
            dz.log ('Main light kitchen is On')
            ledLight.switchOff().checkFirst()
        end
    end
}

Re: Two devices triggers + time

Posted: Sunday 01 December 2019 12:21
by besix
@waaren
This is very ok
Thank you very much. But I have a question thanks to this logic change, can I add a lux light sensor?

Re: Two devices triggers + time

Posted: Sunday 01 December 2019 12:39
by waaren
besix wrote: Sunday 01 December 2019 12:21 I have a question thanks to this logic change, can I add a lux light sensor?
Sure you can. The questions that need to be answered before implementing are
  • What should it control and when
  • Which controlling device take preference

Re: Two devices triggers + time

Posted: Sunday 01 December 2019 20:20
by besix
@waaren
I added the lux sensor as you can see, it works
but did I do it right?
And how to make lux not controlling Radio?

Code: Select all

local motion = 'PIR 1'
local light = 'Light 1'
local lux = 'External lux'
local morning = 'at 06:35-08:00'

return 
{
    on = 
    {
        devices = 
        { 
            [motion] = { morning }, 
            [light] = { morning },
        },
    },

   execute = function(dz)
        local isMotion = dz.devices(motion).active
        local lightOn = dz.devices(light).active
        local ledLight = dz.devices('Light led')
        local LUX = dz.devices(lux).lux
        if isMotion and LUX < 20 then
            dz.log ('Motion detected')
            if not(lightOn) then ledLight.switchOn().checkFirst() end
            dz.devices('Radio').switchOn().afterSec(10)
        end    
        
        if lightOn then
            dz.log ('Main light kitchen is On')
            ledLight.switchOff().checkFirst()
        end
    end
} 
I learn thanks to you

Re: Two devices triggers + time

Posted: Sunday 01 December 2019 22:05
by waaren
besix wrote: Sunday 01 December 2019 20:20 I added the lux sensor as you can see, it works
but did I do it right?
Is this what you want ?

Code: Select all

local motion = 'PIR 1'
local light = 'Light 1'
local lux = 'External lux'
local morning = 'at 06:35-08:00'

return 
{
    on = 
    {
        devices = 
        { 
            [motion] = { morning }, 
            [light] = { morning },
        },
    },

   execute = function(dz)
        local isMotion = dz.devices(motion).active
        local lightOn = dz.devices(light).active
        local ledLight = dz.devices('Light led')
        local isDark = dz.devices(lux).lux < 20

        if isMotion then 
            dz.log ('Motion detected')
            if isDark then
                dz.log ('It is dark')
                if not(lightOn) then ledLight.switchOn().checkFirst() end
            end
            dz.devices('Radio').switchOn().afterSec(10)
        end

        if lightOn then
            dz.log ('Main light kitchen is On')
            ledLight.switchOff().checkFirst()
        end
    end
}



Re: Two devices triggers + time  [Solved]

Posted: Monday 02 December 2019 15:35
by besix
@waaren

Yes it is perfect. I have to understand how it works dark, but it's great Thank you