Lights on when motion but not when manually switched off

Post Reply
slagterbas
Posts: 9
Joined: Wednesday 27 December 2017 16:12
Target OS: Raspberry Pi
Domoticz version:
Contact:

Lights on when motion but not when manually switched off

Post by slagterbas » Friday 18 May 2018 11:00

Hi there,

I just received my Fibaro Multi-Sensor a couple of days ago and started writing a timed (runs every minute) lua script. The goal is to get the lights on between 6:30 and 23:00 when there was motion for the last 15 minutes and the amount of lux was below 15. Also, when it is past 23:00 and there was no motion longer than 15 minutes, the lights should go off (See script below).

Now, this seems to work quite nicely (any approvements are welcome) but now when I switch off a light manually during the period that the script tells it that it should be on, it will get back on again (of course). So...is there an option to say something like: hey, switch on the lights if needed but don't do so for lights that where manually switched off in the meantime. The same goes for the other way around, when I switch a light on when the motion script tells it to be off.

Any help appreciated.

Code: Select all

commandArray = {};

function dateToTime (s)
   year = string.sub(s, 1, 4)
   month = string.sub(s, 6, 7)
   day = string.sub(s, 9, 10)
   hour = string.sub(s, 12, 13)
   minutes = string.sub(s, 15, 16)
   seconds = string.sub(s, 18, 19)

   return os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds};
end

function diffTime (t)
  currentTime = os.time();

  return os.difftime(currentTime, t);
end

motionTreshold = 600; -- 15 min
luxTreshold = 15;

currentDate = os.date('*t');
startTime = os.time{year=currentDate.year, month=currentDate.month, day=currentDate.day, hour='6', min='30', sec='0'};
endTime = os.time{year=currentDate.year, month=currentDate.month, day=currentDate.day, hour='23', min='0', sec='0'};

motionDiff = diffTime(dateToTime(otherdevices_lastupdate['MS Woonkamer']));
startDiff = diffTime(startTime);
endDiff = diffTime(endTime);
numberOfLux = tonumber(otherdevices_svalues['LX Woonkamer']);
isDayTime = (startDiff >= 0 and endDiff <= 0);
isNightTime = (startDiff > 0 and endDiff > 0);
isMotionWithinTreshold = (motionDiff <= motionTreshold);
isLuxWithinTreshold = numberOfLux <= luxTreshold;

print('Checking lights status. StartDiff: ' .. startDiff .. ', EndDiff: ' .. endDiff .. ', motionDiff: ' .. motionDiff .. ', Lux: ' .. numberOfLux);

-- Turning lights on
if
  isDayTime and
  isMotionWithinTreshold and
  isLuxWithinTreshold
then
   commandArray['Scene:Basis verlichting woonkamer'] = 'On';
   print('Turning on basis verlichting woonkamer.');
end

-- Turning lights off

if
  (isNightTime and not isMotionWithinTreshold) or
  not isLuxWithinTreshold
then
  commandArray['DS Alles uit'] = 'On';
  print('Turning off all lights.');
end

return commandArray;

User avatar
waaren
Posts: 1078
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Lights on when motion but not when manually switched off

Post by waaren » Friday 18 May 2018 13:49

What I understand from your description is that a manual switch should override an automatic switch based on (a combination of) time, motion, lux.
Should this override be undefinite ? So if you manually switch off, it should stay off until manually switched on again and vv or do you have a condition to reset to automatic switching.

Would a dzVents Lua script be an option for you and if so what domoticz version are you on ?
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

slagterbas
Posts: 9
Joined: Wednesday 27 December 2017 16:12
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Lights on when motion but not when manually switched off

Post by slagterbas » Friday 18 May 2018 14:01

No, I don't think it should be indefinite but instead should stay off untill the first 'on' event of the next day. This also goes for switching on within the 'off' period. It should stay on for that 'off' period but the next day it should go off again when it was automatically turned on. Does that make sense? Maybe, it is easier for now to say it's indeed stays off (or on) indefinitely.

An DzVents Lua script will certainly be an option. I'm on version V3.8153 of Domoticz. I'm kind of new on this though so not sure what DzVents does.

Thanks again

User avatar
waaren
Posts: 1078
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Lights on when motion but not when manually switched off

Post by waaren » Friday 18 May 2018 19:04

slagterbas wrote:
Friday 18 May 2018 14:01
....
An DzVents Lua script will certainly be an option. I'm on version V3.8153 of Domoticz. I'm kind of new on this though so not sure what DzVents does.

find below a dzVents script tested on V3.8153 ( dzVents = short for Domoticz Easy Events ) Read wiki here that should get you started. If you have any questions or comments please feel free..

Spoiler: show

Code: Select all

 
--[[ conditionalMotion.lua

 The goal is to get the lights on between 6:30 and 23:00 when there was motion for the last 15 minutes and the amount of lux was below 15. Also, when it is past 23:00 and there was no motion longer than 15 minutes, the lights should go off (See script below).

Now, this seems to work quite nicely (any approvements are welcome) but now when I switch off a light manually during the period that the script tells it that it should be on, it will get back on again (of course). So...is there an option to say something like: hey, switch on the lights if needed but don"t do so for lights that where manually switched off in the meantime. The same goes for the other way around, when I switch a light on when the motion script tells it to be off.

 ]]--
 
return {
    on      =       {   timer = { "every minute" } , 
                        devices = { "Motion","Switch"  }  },                -- Name of your motion detector , Name of the switch you use to manually turn off the light (not the light itself)
      
    logging =       {   level   = domoticz.LOG_DEBUG,                       -- or LOG_INFO, LOG_ERROR
                        marker  = "conditionalMotion" },  
                    
    data    =       {   state                   = { initial = "Off" },
                        manual                  = { initial = false },
            },

    execute = function(dz,trigger,triggerInfo)                              -- still need triggerInfo when on version 3.8153
    
        local motionDetectionState =    dz.devices("Motion").state                          -- Change to the actual names in your environment
        local luxLevel             =    dz.devices("Lux").lux                               -- Change to the actual names in your environment
        local switchState          =    dz.devices("Switch").state                          -- Change to the actual names in your environment
        local sceneOn              =    dz.scenes("Scene:Basis verlichting woonkamer")
        local sceneOff             =    dz.scenes("DS Alles uit")
        local timeSlot             =    "at 06:30-23:00"
        local luxFence             =    15
        local lightningTime        =    15
        
        
        local function switchMyScene(action)
            if action ~= nil and action == "On" then
                sceneOn.switchOn()
                dz.log("Turning on basis verlichting woonkamer.",dz.LOG_DEBUG)
                dz.data.state = "On"
            else 
               sceneOff.switchOn()
               dz.data.state = "Off"
               dz.log("Turning off basis verlichting woonkamer.",dz.LOG_DEBUG)
            end    
        end
        
        if dz.data.manual then 
            dz.data.manual = dz.time.matchesRule("at 00:02-23:59")      -- Switch back to auto at midnight
            if triggerInfo.type == dz.EVENT_TYPE_DEVICE and trigger.name == "Switch"  then
                if switchState == "On" then switchMyScene("On") else switchMyScene() end
            end    
        else
            if triggerInfo.type == dz.EVENT_TYPE_DEVICE and trigger.name == "Switch" then 
                  dz.data.manual = true
                  if trigger.state == "On" then  switchMyScene("On") else switchMyScene() end
            elseif dz.data.state == "Off" and
                triggerInfo.type == dz.EVENT_TYPE_DEVICE and 
                motionDetectionState == "On" and 
                luxLevel < luxFence and 
                dz.time.matchesRule(timeSlot) then switchMyScene("On")
            elseif dz.data.state == "On" and motionDetectionState == "Off" and
                  ( luxLevel >= luxFence or 
                    not(dz.time.matchesRule(timeSlot) ) or 
                    sceneOn.lastUpdate.minutesAgo > lightningTime ) then switchMyScene()
            end
        end
    end
}
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

slagterbas
Posts: 9
Joined: Wednesday 27 December 2017 16:12
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Lights on when motion but not when manually switched off

Post by slagterbas » Friday 18 May 2018 20:35

That seems to be a lot more flexible. Will give it a shot. Thanks!

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest