Set Light to On based on motion detection between time  [Solved]

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

Moderator: leecollings

Xavier82
Posts: 178
Joined: Tuesday 07 June 2016 22:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Netherlands
Contact:

Set Light to On based on motion detection between time

Post by Xavier82 »

Hi all,

I searched the forum but couldn't find what I was looking for....

Could any one help me with a dzvents script to activate the light on our frontdoor when the camera with motion detection has detected motion between 00.00 hours and sunrise?

I have the next setup:
Camera connected to Domoticz but also to Synology Surveillance Station.
In Surveillance station I have setup an action rule based on url to Domoticz to IDX of Camera.
The action rule in Surveillance Station is triggered by motion of the camera.

But now I'm stuck...

So what should the script do?
It would be great of the light goes on when motion is detected between 00:00 hours and sunrise.

I hope somebody can help me with this.

Thanks in advance

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

Re: Set Light to On based on motion detection between time

Post by waaren »

Xavier82 wrote: Monday 21 September 2020 16:44 In Surveillance station I have setup an action rule based on url to Domoticz to IDX of Camera.
How does this call to domoticz looks like and what is it supposed to do?
Is the domoticz version number in your profile correct and if so is there a reason why you have not upgraded to a more recent stable release?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Xavier82
Posts: 178
Joined: Tuesday 07 June 2016 22:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Netherlands
Contact:

Re: Set Light to On based on motion detection between time

Post by Xavier82 »

Hi Waaren,

updated my profile.
Version is 2020.2 stable.
The call from Surveillance station is an action based on a URL wich triggers the IDX of a switch in domoticz namen " Camera Motion Detection".

Normally this has as an action that when motion is detected I receive a notificaties in telegram with a snapshot of the camera.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Set Light to On based on motion detection between time

Post by waaren »

Xavier82 wrote: Monday 21 September 2020 19:40 Hi Waaren,

updated my profile.
Version is 2020.2 stable.
The call from Surveillance station is an action based on a URL wich triggers the IDX of a switch in domoticz namen " Camera Motion Detection".

Normally this has as an action that when motion is detected I receive a notificaties in telegram with a snapshot of the camera.
Can you try with this dzVents script?

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            ['Camera Motion Detection'] = { 'between 00:00 and sunrise' },
        },
    },
 
    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Camera',
    },

    execute = function(dz, item)
        local light = dz.devices('Name of your light')
        local offDelay = 300
        
        if item.active then -- state == 'On'
            light.cancelQueuedCommands()
            light.switchOn()
            light.switchOff().afterSec(offDelay)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Xavier82
Posts: 178
Joined: Tuesday 07 June 2016 22:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Netherlands
Contact:

Re: Set Light to On based on motion detection between time

Post by Xavier82 »

Thanks for the code but I get this error:
2020-09-21 23:25:09.101 Error: dzVents: Error: (3.0.2) error loading module 'Voordeur verlichting' from file '/home/pi/domoticz/scripts/dzVents/generated_scripts/Voordeur verlichting.lua':
2020-09-21 23:25:09.101 ...ripts/dzVents/generated_scripts/Voordeur verlichting.lua:11: <eof> expected near '='
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Set Light to On based on motion detection between time

Post by waaren »

Xavier82 wrote: Monday 21 September 2020 23:26 Thanks for the code but I get this error:
2020-09-21 23:25:09.101 Error: dzVents: Error: (3.0.2) error loading module 'Voordeur verlichting' from file '/home/pi/domoticz/scripts/dzVents/generated_scripts/Voordeur verlichting.lua':
2020-09-21 23:25:09.101 ...ripts/dzVents/generated_scripts/Voordeur verlichting.lua:11: <eof> expected near '='
There was an error in the script which is now corrected please try again but note that the script in its current form will only work after 00:00
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Xavier82
Posts: 178
Joined: Tuesday 07 June 2016 22:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Netherlands
Contact:

Re: Set Light to On based on motion detection between time

Post by Xavier82 »

waaren wrote: Monday 21 September 2020 23:32
Xavier82 wrote: Monday 21 September 2020 23:26 Thanks for the code but I get this error:
2020-09-21 23:25:09.101 Error: dzVents: Error: (3.0.2) error loading module 'Voordeur verlichting' from file '/home/pi/domoticz/scripts/dzVents/generated_scripts/Voordeur verlichting.lua':
2020-09-21 23:25:09.101 ...ripts/dzVents/generated_scripts/Voordeur verlichting.lua:11: <eof> expected near '='
There was an error in the script which is now corrected please try again but note that the script in its current form will only work after 00:00
Thanks, error is gone for now.

Will keep an eye on it ;)
Xavier82
Posts: 178
Joined: Tuesday 07 June 2016 22:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Netherlands
Contact:

Re: Set Light to On based on motion detection between time

Post by Xavier82 »

So I checked the logging this morning, but the light didn't go on. I had several motion detections (a cat walked by). I receive a notificaties in telegram on my Phone but it looks like the script hasn' t been triggered.

I have adjusted the name of the lamp in the script.

Any idea why not?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Set Light to On based on motion detection between time

Post by waaren »


Xavier82 wrote:
Any idea why not?
Can you share the exact URL that activates the device with the telegram action?



Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Xavier82
Posts: 178
Joined: Tuesday 07 June 2016 22:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Netherlands
Contact:

Re: Set Light to On based on motion detection between time

Post by Xavier82 »

waaren wrote: Tuesday 22 September 2020 9:41
Xavier82 wrote:
Any idea why not?
Can you share the exact URL that activates the device with the telegram action?
Sure, the URL in Synology Surveillance Station which is triggered on motion is:
http://<IP_ADRES_Domoticz>:<port domoticz>/json.htm?type=command&param=switchlight&idx=26&switchcmd=On
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Set Light to On based on motion detection between time

Post by waaren »

Xavier82 wrote: Tuesday 22 September 2020 8:06 Any idea why not?
Please try again after changing cancelQueuedCommand to cancelQueuedCommands
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Xavier82
Posts: 178
Joined: Tuesday 07 June 2016 22:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Netherlands
Contact:

Re: Set Light to On based on motion detection between time

Post by Xavier82 »

Waaren,

I made the adjustment.
Will keep you posted about this.

Greetz,
Xavier
Melotron
Posts: 62
Joined: Tuesday 22 November 2016 21:04
Target OS: Linux
Domoticz version: 2020.2
Location: Gothenburg
Contact:

Re: Set Light to On based on motion detection between time

Post by Melotron »

Hi.
I've been trying to get a similar script running for myself for a few motion sensors.
I'am getting the following error :

Code: Select all

2020-09-22 18:52:11.225 ...ticz/scripts/dzVents/generated_scripts/dz timer test.lua:4: '}' expected near '='
The script that I'am using are this:

Code: Select all

return 
{
    on = 
    { devices =  { 118 = { 'between sunset and sunrise' }, }, },

    execute = function(domoticz)
        
    local myTextDevice  = 82

        if domoticz.devices(118).state  == 'On' then 
           myTextDevice.updateText('Motion')
           domoticz.devices(42).switchOn()
           domoticz.devices(42).switchOff().afterSec(40) 
            
        end
    
end }
It would be wonderful to get a scrip like this working even for setting a warmer kelvin on the lamps after sunset too =)

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

Re: Set Light to On based on motion detection between time

Post by waaren »

Melotron wrote: Tuesday 22 September 2020 19:05 Hi.
I've been trying to get a similar script running for myself for a few motion sensors.
I'am getting the following error :

Code: Select all

2020-09-22 18:52:11.225 ...ticz/scripts/dzVents/generated_scripts/dz timer test.lua:4: '}' expected near '='
Syntax to use is

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            [118] = { 'between sunset and sunrise' },
        },
    },

    execute = function(domoticz)

        local myTextDevice  = 82

        if domoticz.devices(118).state  == 'On' then 
            myTextDevice.updateText('Motion')
            domoticz.devices(42).switchOn()
            domoticz.devices(42).switchOff().afterSec(40) 
        end

    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Melotron
Posts: 62
Joined: Tuesday 22 November 2016 21:04
Target OS: Linux
Domoticz version: 2020.2
Location: Gothenburg
Contact:

Re: Set Light to On based on motion detection between time

Post by Melotron »

waaren wrote: Tuesday 22 September 2020 21:10
Melotron wrote: Tuesday 22 September 2020 19:05 Hi.
I've been trying to get a similar script running for myself for a few motion sensors.
I'am getting the following error :

Code: Select all

2020-09-22 18:52:11.225 ...ticz/scripts/dzVents/generated_scripts/dz timer test.lua:4: '}' expected near '='
Syntax to use is

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            [118] = { 'between sunset and sunrise' },
        },
    },

    execute = function(domoticz)

        local myTextDevice  = 82

        if domoticz.devices(118).state  == 'On' then 
            myTextDevice.updateText('Motion')
            domoticz.devices(42).switchOn()
            domoticz.devices(42).switchOff().afterSec(40) 
        end

    end
}
Thanks alot =)
Its finally working.

Are there any way to put the time at the devices like :
domoticz.devices(42).switchOn().at(between sunset and sunrise)

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

Re: Set Light to On based on motion detection between time

Post by waaren »

Melotron wrote: Tuesday 22 September 2020 21:56 Are there any way to put the time at the devices like :
domoticz.devices(42).switchOn().at(between sunset and sunrise)
Sorry but I don't understand what you try to achieve. switchOn() can be delayed until a moment in future but not between aaa and bbb.
Can you please explain ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Melotron
Posts: 62
Joined: Tuesday 22 November 2016 21:04
Target OS: Linux
Domoticz version: 2020.2
Location: Gothenburg
Contact:

Re: Set Light to On based on motion detection between time

Post by Melotron »

I want to have a low Kelvin during daytime and a higher Kelvin after the sun goes down.
I can achieve that with 2 scripts, but I could shrink my scripts down by setting times when a specific lamp should be on in a script.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Set Light to On based on motion detection between time

Post by waaren »

Melotron wrote: Wednesday 23 September 2020 6:51 I want to have a low Kelvin during daytime and a higher Kelvin after the sun goes down.
I can achieve that with 2 scripts, but I could shrink my scripts down by setting times when a specific lamp should be on in a script.
Like this?

Code: Select all

return
{
    on =
    {
        timer =
        {
            'at sunset',
            'at sunrise',
        },
    },

    logging =  
    {
        level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when tested and OK
        marker = 'lights at night',
    },

    execute = function(dz)

        local light = dz.devices(42)
        
        if dz.time.matchesRule('at sunrise') then 
            light.switchOff()
        else
            light.switchOn()
        end

    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Xavier82
Posts: 178
Joined: Tuesday 07 June 2016 22:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Netherlands
Contact:

Re: Set Light to On based on motion detection between time

Post by Xavier82 »

Waaren,

Tested the enhancement but no result.
For testing now I made a second actionrule in Synology Surveillance Station which is:
http://<IP_ADRES_Domoticz>:<port domoticz>/json.htm?type=command&param=switchlight&idx=186&switchcmd=On

IDX 186 is the IDX of my frontdoor light which I want to activate.
When testing the light goes on indeed, but how can I be sure the light only goes on between the entered time and goes off after 300seconds?

This is the code for now:

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            ['Voordeurlamp'] = { 'between 00:00 and sunrise' },
        },
    },
    
    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Camera',
    },

    execute = function(dz, item)
        local light = dz.devices('Voordeurlamp')
        local offDelay = 300
        
        if item.active then -- state == 'On'
            light.cancelQueuedCommands()
            light.switchOn()
            light.switchOff().afterSec(offDelay)
        end
    end
}
Xavier82
Posts: 178
Joined: Tuesday 07 June 2016 22:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Netherlands
Contact:

Re: Set Light to On based on motion detection between time

Post by Xavier82 »

mmmzzz, when rethinking about this....

What actually should happen is that the lamp on the frontdoor goes on WHEN "Camera motion dection" between 00:00 and sunrise FOR 5 minutes.
In other words I would like to re-use already existing scripts in Surveillance Station and Domoticz.
The trigger of this script should be a switch (IDX 26) in Domoticz.

Can this be done?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest