Two devices triggers + time  [Solved]

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

Moderator: leecollings

Post Reply
besix
Posts: 99
Joined: Friday 25 January 2019 11:33
Target OS: Linux
Domoticz version: beta
Location: Poland
Contact:

Two devices triggers + time

Post 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?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Two devices triggers + time

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
besix
Posts: 99
Joined: Friday 25 January 2019 11:33
Target OS: Linux
Domoticz version: beta
Location: Poland
Contact:

Re: Two devices triggers + time

Post 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?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Two devices triggers + time

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
besix
Posts: 99
Joined: Friday 25 January 2019 11:33
Target OS: Linux
Domoticz version: beta
Location: Poland
Contact:

Re: Two devices triggers + time

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Two devices triggers + time

Post 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
}


Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
besix
Posts: 99
Joined: Friday 25 January 2019 11:33
Target OS: Linux
Domoticz version: beta
Location: Poland
Contact:

Re: Two devices triggers + time  [Solved]

Post by besix »

@waaren

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

Who is online

Users browsing this forum: No registered users and 1 guest