PIR + light: how to go to previous state

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

Moderator: leecollings

Post Reply
narel
Posts: 4
Joined: Sunday 04 February 2018 14:27
Target OS: Linux
Domoticz version: beta
Location: Poland
Contact:

PIR + light: how to go to previous state

Post by narel »

Hi,

I have a PIR sensor which should trigger the light. But the light can also be set manually (this is the main issue). It is the RGBW switch (it's the second).

The most common answer on the forum for forMin issues:

Code: Select all

light.switchOff().checkFirst().afterMin(2)
light.switchOn().checkFirst().forMin(2)
won't work for me, because I lost the main feature of forMin - returning to the previous state after time.
afterMin isn't a similar function to forMin, so in my opinion this is not a solution and I'm afraid that in most cases forMin is useless as the same can be achieved with:

Code: Select all

light.switchOn()
light.switchOff().afterMin(2)
which is more intuitive and user-proof than forMin.

Please correct me, if I'm wrong, but I understand that it should work the following way:
1. RGBW is set to 50% red colour
2. PIR event is triggered and the brightness is set to 100% and colour to white with forMin(2)
3. After 2 minutes light is restored to previous state (50% red)

But when another PIR event is triggered then command:

Code: Select all

light.switchOff().afterMin(2)
will discard forMin (so the state it holds also) and the light will be turn off after 2 minues after last PIR event.

So the questions are:
1) is there any way to reset the forMin timer after motion is detected (it would be the perfect solution which I think would address 99% of issues with forMin)
2) is there any way to read and then store RGBW device status (for simple switch it's easy: light.state, but how can I read rgbw settings) - with it the solution will be to store with local variable the state before first action and the restore it after some time).
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: PIR + light: how to go to previous state

Post by waaren »

narel wrote: Friday 24 August 2018 16:32
questions are:
1) is there any way to reset the forMin timer after motion is detected (it would be the perfect solution which I think would address 99% of issues with forMin)
2) is there any way to read and then store RGBW device status (for simple switch it's easy: light.state, but how can I read rgbw settings) - with it the solution will be to store with local variable the state before first action and the restore it after some time).
1) does cancelQueuedCommands() work for you ?
2) maybe this post will get you going on reading / setting RGBW
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
narel
Posts: 4
Joined: Sunday 04 February 2018 14:27
Target OS: Linux
Domoticz version: beta
Location: Poland
Contact:

Re: PIR + light: how to go to previous state

Post by narel »

I don't think that cancelQueuedCommands can help me. If i discard the queue for device I will lost the previous state.

Thanks for the link, I will try to do it.
However don't you think that resetting the forMin timer (to count from zero once again) will the best solution?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: PIR + light: how to go to previous state

Post by waaren »

narel wrote: Friday 24 August 2018 18:27 I don't think that cancelQueuedCommands can help me. If i discard the queue for device I will lost the previous state.

Thanks for the link, I will try to do it.
However don't you think that resetting the forMin timer (to count from zero once again) will the best solution?
Using dzVents persistent data you can save current state and use that whenever you need it.
It is my understanding that it is not possible to reset the forMin timer other than with the cancelQueuedCommands option.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
narel
Posts: 4
Joined: Sunday 04 February 2018 14:27
Target OS: Linux
Domoticz version: beta
Location: Poland
Contact:

Re: PIR + light: how to go to previous state

Post by narel »

Thanks waaren!
But still I cannot make it.
I tried do it in this way:
1. create trigger for RGBW light device which saves the state to a global variable domoticz.globalData.rgbwLastState[device.idx]
2. create a PIR trigger:

Code: Select all

prev_state = domoticz.globalData.rgbwLastState[light1.idx]
light1.cancelQueuedCommands()
light1.setRGB(255,0,0).silent()
light1.setRGB(prev_state.red, prev_state.green, prev_state.blue).afterMin(5).silent()
But unfortunately silent() doesn't work. At least doesn't work with setRGB (and it looks like it doens't work with switchOff().afterMin(1).silent() also). So in the end my PIR script updates rgbwLastState and I still cannot come back to the state before the first PIR event...

I don't believe that I'm the only one who need such scenario...
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: PIR + light: how to go to previous state

Post by waaren »

narel wrote: Saturday 25 August 2018 18:09 unfortunately silent() doesn't work. At least doesn't work with setRGB (and it looks like it doens't work with switchOff().afterMin(1).silent() also). So in the end my PIR script updates rgbwLastState and I still cannot come back to the state before the first PIR event...
If I understand your requirement correctly the script below should do what you want to achieve. On the first activation of the PIR the color, level and state of the light is stored in persistent data and only if no further movements within the defined delay period the light is restored to the previous setting.


Code: Select all

-- intelliPIR.lua
 
local yourLight         = "??????????"            -- Enter name of your RGBW light
local yourPIR           = "??????????"            -- Enter name of your PIR
local responseString    = "intelliPIR"

return {
    on = { devices        = {yourPIR},
           httpResponses  = {responseString}},
    
    logging =   {       level   =   domoticz.LOG_DEBUG,                  -- switch to LOG_INFO when results are OK 
                        marker  =   "intelliPIR" },                                           

    data    =   {      dimLevel         = { initial = 50    }, 
                       color            = { initial = {}    },
                       lightState       = { initial = "Off" },
                       PIRstate         = { initial = "Off" },
                       PIRmodTime       = { initial = "1970-1-1 1:1:1.1"     }}, 
                       
    execute = function(dz, trigger)
        local PIRdelay          = 120                                -- delay in seconds
        local maxBrightness     = 100
        local light             = dz.devices(yourLight)
        local PIR               = dz.devices(yourPIR)
        local Time              = require('Time')
        local white             = { b=255,r=255,g=255 }
        
        local function offTrigger(delay)
            local  url = dz.settings['Domoticz url'] .. "/json.htm?type=command&param=addlogmessage&message=I%20am%20back"
            dz.openURL  ({ url = url, method   = "GET", callback = responseString }).afterSec(delay)   -- to come back after delay seconds 
        end
        
        local function setLight(light, dimLevel,color, state)
            if state ~= nil and state == "Off" then 
                light.switchOff().silent()
            else
                light.setRGB( color.r, color.g, color.b )  
                light.dimTo(dimLevel)
            end
        end

        local function motionDetected()
            dz.data.PIRmodTime = dz.time.raw                            -- store last time that PIR was set to "On"
            offTrigger(PIRdelay)                                        -- start (kind of) timer
            if dz.data.PIRstate == "Off" then                           -- Only store values on first call to motionDetected
                dz.data.lightState  = light.state
                dz.data.PIRstate    = "On"
                dz.data.color       = dz.utils.fromJSON(light.color)
                dz.data.dimLevel    = light.level
                setLight(light,maxBrightness,white)
            end
        end

        local function delayPassed()
            if (Time(dz.time.raw).compare(Time(dz.data.PIRmodTime)).secs) > (PIRdelay - 1) then  -- More seconds past since last PIR On state than PIRdelay 
                dz.log("time passed" ,dz.LOG_DEBUG)
                dz.data.PIRstate = "Off"
                setLight(light,dz.data.dimLevel,dz.data.color,dz.data.lightState)       -- Restore light to old value
            end
        end

        if trigger.isDevice and PIR.state == "On"  then       -- script is triggered by PIR 
            motionDetected()
        elseif trigger.isHTTPResponse then                    -- script is triggered by HTTPResponse    
            delayPassed()
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
narel
Posts: 4
Joined: Sunday 04 February 2018 14:27
Target OS: Linux
Domoticz version: beta
Location: Poland
Contact:

Re: PIR + light: how to go to previous state

Post by narel »

Thanks!!! I will try it tomorrow.

Today I tried a different approach, but faced an issue with cancelQueuedCommands. How should it work? I though that when I call

Code: Select all

device.cancelQueuedCommands()
Then the whole queue for device "device" will be cleaned. But unfortunately it doesn't work this way. Is it a bug or my misunderstanding of a device queue?

I also implemented device.setColBrightness function (in dzVents/runtime/device-adapters/rgbw_device.lua) which allows to call setcolbrightnessvalue from JSON API.
It allows to set and restore the whole state of a RGB, RGBW, RGBWW and similar devices with one command.
Do you think I can do a pull request with it? I'm new to Domoticz (and don't know Lua to be honest) but would like to help other people.

Thanks!
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: PIR + light: how to go to previous state

Post by waaren »

narel wrote: Sunday 26 August 2018 22:20 Thanks!!! I will try it tomorrow.

Today I tried a different approach, but faced an issue with cancelQueuedCommands. How should it work? I though that when I call

Code: Select all

device.cancelQueuedCommands()
Then the whole queue for device "device" will be cleaned. But unfortunately it doesn't work this way. Is it a bug or my misunderstanding of a device queue?

I also implemented device.setColBrightness function (in dzVents/runtime/device-adapters/rgbw_device.lua) which allows to call setcolbrightnessvalue from JSON API.
It allows to set and restore the whole state of a RGB, RGBW, RGBWW and similar devices with one command.
Do you think I can do a pull request with it? I'm new to Domoticz (and don't know Lua to be honest) but would like to help other people.

Thanks!
I suggest you send a PM to @dannybloe. Danny is the developer of the Lua part of dzVents.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: PIR + light: how to go to previous state

Post by waaren »

narel wrote: Sunday 26 August 2018 22:20 Thanks!!! I will try it tomorrow.

Today I tried a different approach, but faced an issue with cancelQueuedCommands. How should it work? I though that when I call

Code: Select all

device.cancelQueuedCommands()
Then the whole queue for device "device" will be cleaned. But unfortunately it doesn't work this way. Is it a bug or my misunderstanding of a device queue?

I also implemented device.setColBrightness function (in dzVents/runtime/device-adapters/rgbw_device.lua) which allows to call setcolbrightnessvalue from JSON API.
It allows to set and restore the whole state of a RGB, RGBW, RGBWW and similar devices with one command.
Do you think I can do a pull request with it? I'm new to Domoticz (and don't know Lua to be honest) but would like to help other people.

Thanks!
I suggest you send a PM to @dannybloe. Danny is the developer of the Lua part of dzVents.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: PIR + light: how to go to previous state

Post by dannybloe »

narel wrote: Sunday 26 August 2018 22:20 Thanks!!! I will try it tomorrow.

Today I tried a different approach, but faced an issue with cancelQueuedCommands. How should it work? I though that when I call

Code: Select all

device.cancelQueuedCommands()
Then the whole queue for device "device" will be cleaned. But unfortunately it doesn't work this way. Is it a bug or my misunderstanding of a device queue?

I also implemented device.setColBrightness function (in dzVents/runtime/device-adapters/rgbw_device.lua) which allows to call setcolbrightnessvalue from JSON API.
It allows to set and restore the whole state of a RGB, RGBW, RGBWW and similar devices with one command.
Do you think I can do a pull request with it? I'm new to Domoticz (and don't know Lua to be honest) but would like to help other people.

Thanks!
cancelQueuedCommands() should definitely remove all commands for that specific device. So if you think it doesn't work well try to isolate it and make sure no other script is creating new commands for that device. Turn on debug logging and see which commands are sent back to Domoticz when you issue that command.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest