switch and sensor combination  [Solved]

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

Moderator: leecollings

robbert1977
Posts: 15
Joined: Saturday 18 April 2020 22:12
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: switch and sensor combination

Post by robbert1977 »

Final code with Lux and based on motion keep light on.
In the code above if motion kept activated till end of timer the switch off command took place.
Solved in this one by starting the counter after motion sensor goes in OFF state, if reactivated on motion counter reset till next OFF state

No problems :) just wanted to share this.

Code: Select all

return {
  active = true,
  on = {
    devices = {
      "Lamp Gang",
      "PIR Gang"
    }
  },
  data =
    {
        sensorTurnedOnLights = { initial = false },
    },

  execute = function(dz, item)
        local Lux        = dz.devices('PIR Test Lux').lux
        local maxLux     = 150
        local maxSeconds = 300
        local motion = dz.devices('PIR Gang')
        local switch = dz.devices('Lamp Gang')

        if item == motion and Lux < maxLux then -- motion active
          if motion.state == 'On' and switch.state == 'Off' then -- motion was the trigger
            dz.data.sensorTurnedOnLights = true
            switch.cancelQueuedCommands()
            switch.switchOn().checkFirst()
          elseif motion.state == 'On' and switch.state == 'On' and dz.data.sensorTurnedOnLights == true then -- new motion detection
            switch.cancelQueuedCommands()
          elseif motion.state == 'Off' and dz.data.sensorTurnedOnLights == true then -- switched on by sensor now motion in off state
            switch.switchOff().afterSec(maxSeconds).checkFirst()
          end
        elseif item == switch and item.state == 'On' and motion.state == 'Off' then -- switch activated
          dz.data.sensorTurnedOnLights = false
        end
    end
}
Raspberry Pi 4
domoticzVersion: 2020.2
dzVentsVersion: 3.0.2
OpenZWave USB: 1.6-1033-g3661f170
RFXCOM - RFXtrx433 USB: Pro XL1/1043, Noise: 109 dB
Homebridge - Siri integration
User avatar
Sjonnie2017
Posts: 361
Joined: Wednesday 02 August 2017 19:43
Target OS: Linux
Domoticz version: Latest ß
Location: The Netherlands
Contact:

Re: switch and sensor combination

Post by Sjonnie2017 »

Reviving an old thread here hoping anyone can shed some light on my problem. I have modified the script to remove the lux part and added a timer. The result of my modification is that the light switches on the first time I enter the room after I saved it independent of the time of day. If I enter the room a minute later the switch is activated again. I have no clue what causes this behaviour.

The script looks like this:

Code: Select all

local motionName = 'Sensor Keuken'
local switchName = 'Plafond Keuken'

return
{
    active = true,

    on =
    {
      devices =
          {
          motionName,
          switchName,
          },
      timer =
          { 
          'between 15 minutes after sunset and 3 minutes before sunrise'
          },
    },
    
    data =
    {
        sensorTurnedOnLights = { initial = false },
    },

    logging =
    {
        level =  domoticz.LOG_DEBUG,
        marker = 'SensorKeuken'
    },

    execute = function(dz, item)

--     local Lux        = dz.devices('PIR Gang Lux').lux <= I am not using this as the sensor has no lux capabilities
--     local maxLux     = 150
--     local Lights     = dz.devices('Plafond Keuken')

        local maxSeconds = 10
        local motion = dz.devices(motionName)
        local switch = dz.devices(switchName)

        --DEBUG
        dz.log('state of switch:   ' .. switch.state,dz.LOG_DEBUG)
        dz.log('state of motion:   ' .. motion.state,dz.LOG_DEBUG)
        dz.log('name of item:   ' .. item.name,dz.LOG_DEBUG)
        dz.log('sensorTurnedOnLights value: ' .. tostring(dz.data.sensorTurnedOnLights),dz.LOG_DEBUG)

        if item == motion then
            dz.log('if item == motion',dz.LOG_DEBUG)
            if switch.state == 'On' and item.state == 'On' and dz.data.sensorTurnedOnLights then
                dz.log("switch.state == 'On' and item.state == 'On' and dz.data.sensorTurnedOnLights",dz.LOG_DEBUG)
                switch.cancelQueuedCommands()
                switch.switchOff().afterSec(maxSeconds).checkFirst().silent()
            elseif item.state == 'On' and switch.state == 'Off' then
                dz.log("item.state == 'On' and switch.state == 'Off'",dz.LOG_DEBUG)
                dz.data.sensorTurnedOnLights = true
                switch.cancelQueuedCommands()
                switch.switchOn().checkFirst().silent()
                switch.switchOff().afterSec(maxSeconds).silent()
            end
        elseif item.state == 'On' and  motion.state == 'Off' then
            dz.log("item.state == 'On' and  motion.state == 'Off'",dz.LOG_DEBUG)
            dz.data.sensorTurnedOnLights = false
        end
    end
}

Any help or tips greatly appreciated!
ConBee II - TRÅDFRI lights + switches, loads of ChingLing dimmers and switches, Heiman and Xiaomi sensors
SolarEdge SE4000H (with active modbus_tcp)
YouLess Energy meter
Shelly 2.5 in roller shutter mode
besix
Posts: 99
Joined: Friday 25 January 2019 11:33
Target OS: Linux
Domoticz version: beta
Location: Poland
Contact:

Re: switch and sensor combination

Post by besix »

Change

Code: Select all

if item == motion then
to this form

Code: Select all

if item == motion and dz.time.matchesRule('between 15 minutes after sunset and 3 minutes before sunrise') then
User avatar
Sjonnie2017
Posts: 361
Joined: Wednesday 02 August 2017 19:43
Target OS: Linux
Domoticz version: Latest ß
Location: The Netherlands
Contact:

Re: switch and sensor combination

Post by Sjonnie2017 »

Tx! That works. I experimented a little bit by introducing a schedule which seems to work as well. We'll see tonight if the results are as expected.

This is the final code I used:

Code: Select all

-- Script based on this thread: https://www.domoticz.com/forum/viewtopic.php?f=59&t=29834&start=20
-- Purpose: switch on light when motion is detected between sunset and sunrise (more or less ;-))

local motionName = 'Sensor Keuken'
local switchName = 'Plafond Keuken'
local schedule1 = 'between 15 minutes after sunset and 3 minutes before sunrise on mon, tue, wed, thu'
local schedule2 = 'between 15 minutes after sunset and 08:30 on fri, sat, sun'


return
{
    active = true,

    on =
    {
      devices =
          {
          motionName,
          switchName,
          },
    },
    
    data =
    {
        sensorTurnedOnLights = { initial = false },
    },

    logging =
    {
        level =  domoticz.LOG_DEBUG,
        marker = 'SensorKeuken'
    },

    execute = function(dz, item)

--     local Lux        = dz.devices('PIR Gang Lux').lux
--     local maxLux     = 150
--     local Lights     = dz.devices('Lamp Gang')

        local maxSeconds = 10
        local motion = dz.devices(motionName)
        local switch = dz.devices(switchName)

        --DEBUG
        dz.log('state of switch:   ' .. switch.state,dz.LOG_DEBUG)
        dz.log('state of motion:   ' .. motion.state,dz.LOG_DEBUG)
        dz.log('name of item:   ' .. item.name,dz.LOG_DEBUG)
        dz.log('sensorTurnedOnLights value: ' .. tostring(dz.data.sensorTurnedOnLights),dz.LOG_DEBUG)

        if item == motion and dz.time.matchesRule(schedule1) or dz.time.matchesRule(schedule2) then
            dz.log('if item == motion',dz.LOG_DEBUG)
            if switch.state == 'On' and item.state == 'On' and dz.data.sensorTurnedOnLights then
                dz.log("switch.state == 'On' and item.state == 'On' and dz.data.sensorTurnedOnLights",dz.LOG_DEBUG)
                switch.cancelQueuedCommands()
                switch.switchOff().afterSec(maxSeconds).checkFirst().silent()
            elseif item.state == 'On' and switch.state == 'Off' then
                dz.log("item.state == 'On' and switch.state == 'Off'",dz.LOG_DEBUG)
                dz.data.sensorTurnedOnLights = true
                switch.cancelQueuedCommands()
                switch.switchOn().checkFirst().silent()
                switch.switchOff().afterSec(maxSeconds).silent()
            end
        elseif item.state == 'On' and  motion.state == 'Off' then
            dz.log("item.state == 'On' and  motion.state == 'Off'",dz.LOG_DEBUG)
            dz.data.sensorTurnedOnLights = false
        end
    end
}
Again... tx for your support!
ConBee II - TRÅDFRI lights + switches, loads of ChingLing dimmers and switches, Heiman and Xiaomi sensors
SolarEdge SE4000H (with active modbus_tcp)
YouLess Energy meter
Shelly 2.5 in roller shutter mode
Post Reply

Who is online

Users browsing this forum: euhen and 1 guest