Garden lights and motion sensor  [Solved]

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

Moderator: leecollings

Post Reply
fvdp80
Posts: 69
Joined: Tuesday 14 August 2018 8:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10303
Contact:

Garden lights and motion sensor

Post by fvdp80 »

I've set up a garden light routine, but not all of it seems to work.

* Garden lights on at sunset (works)
* Gardenlightdoor turns on when motion detected at nighttime (works)
* Garden lights turn off around 23:30 (works)
* Gardenlights turn on at motion detected at nighttime, but after they are turned off after 23:30-ish (not working)

What am I doing wrong in the following script?

Code: Select all

local gardenMotion = 157 --motion sensor tuin

return {
	on = 
	{
		devices = 
		{
		    [gardenMotion] = { 'at nighttime' },
		},
		
		timer = 
		{
		    'at sunset',
            'at 23:30',
        },
		
		variables = {},
		scenes = {},
		groups = {},
		security = {},
		httpResponses = {},
		shellCommandResponses = {},
		customEvents = {},
		system = {},
	},
	
	data = {},
	
	logging = 
	{   level = domoticz.LOG_DEBUG,
        marker = "Hue light tuin",
    },
	
	execute = function(dz, item)

local   ped1 = dz.devices(142) --hue tuinlamp 1
        garden = dz.devices().filter({142, 143, 144, 145, 146}) --hue tuinlampen
        gardenlightdoor = dz.devices(174) --hue lamp naast keukendeur
        gardenMotion = dz.devices(157)
    
    --motionsensor tuin, lamp naast keukendeur aan op beweging
    if item == gardenMotion and gardenMotion.state == 'On' then
        if (gardenlightdoor.active) then
            gardenlightdoor.dimTo(0).afterMin(3)
        else
            gardenlightdoor.dimTo(100).forMin(3)
        end
    
    --motionsensor tuin, lampen in tuin aan op beweging
    elseif item == gardenMotion and gardenMotion.state == 'On' and dz.time.matchesRule('after 23:30') then
        if ped1.active then
            dz.log('Lampen waren aan en gaan nu uit')
            garden.forEach(function(device)
            device.switchOff().afterMin(3)
            end)
        else
            dz.log('Lampen gaan aan door beweging')
            dz.openURL({
                    url = 'http://10.0.1.27/api/eoKsH5jcG2zz5zt2Q2Qzk-WdZRdEpeXQHgkXOE22/groups/4/action',
                    method = 'PUT',
                    postData = {scene = "2k1uz6x3yPrWSZd"} --scene 'energie'
                    })
            garden.forEach(function(device)
            device.switchOff().afterMin(3)
            end)
        end
    
    --lampen tuin aan bij zonsondergang en uit rond 23:30uur
    elseif item.isTimer then
        if (dz.time.matchesRule('at sunset')) then 
            dz.openURL({
                    url = 'http://10.0.1.27/api/xxxxxxxxxxxxxxxxxxxx/groups/4/action',
                    method = 'PUT',
                    postData = {scene = "-PHj0N3ZEsw37b2", transitiontime = 9000} --scene 'ontspannen' met een transitietijd van 15 minuten
                    })
        else
            dz.openURL({
                    url = 'http://10.0.1.27/api/xxxxxxxxxxxxxxxxxxxx/groups/4/action',
                    method = 'PUT',
                    postData = {on = false} --uitschakelen alle lampen
                    }).withinMin(20)
        end
    end
 
end
}

StephaneM60
Posts: 12
Joined: Saturday 10 November 2018 12:47
Target OS: Windows
Domoticz version:
Contact:

Re: Garden lights and motion sensor

Post by StephaneM60 »

Hi,

For me without testing your code your problem is the "elseif".

if item == gardenMotion and gardenMotion.state == 'On' then
.../...
elseif item == gardenMotion and gardenMotion.state == 'On' and dz.time.matchesRule('after 23:30') then
.../...
end


change it to

if item == gardenMotion and gardenMotion.state == 'On' then
.../...
end

if item == gardenMotion and gardenMotion.state == 'On' and dz.time.matchesRule('after 23:30') then
.../...
end
StephaneM60
Posts: 12
Joined: Saturday 10 November 2018 12:47
Target OS: Windows
Domoticz version:
Contact:

Re: Garden lights and motion sensor

Post by StephaneM60 »

You can do the same for elseif item.isTimer then and replace it with a conditionnal block without elseif. Because you don't need to prevent the evaluation of the condition item.isTimer because this condition is always false when the item is not a timer....
fvdp80
Posts: 69
Joined: Tuesday 14 August 2018 8:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10303
Contact:

Re: Garden lights and motion sensor

Post by fvdp80 »

Good suggestion, will try it!
fvdp80
Posts: 69
Joined: Tuesday 14 August 2018 8:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10303
Contact:

Re: Garden lights and motion sensor  [Solved]

Post by fvdp80 »

The problem was in the timer logic in the matchesrule part. It seems that Domoticz doesn't understand "after 23:30".
This code is working now:

Code: Select all

local gardenMotion = 157 --motion sensor tuin

return 
{
	on = 
	{
		devices = 
		{
		    [gardenMotion] = { 'at nighttime' },
		},
		
		timer = 
		{
		    'at sunset',
            'at 23:30',
        },
		
		variables = {},
		scenes = {},
		groups = {},
		security = {},
		httpResponses = {},
		shellCommandResponses = {},
		customEvents = {},
		system = {},
	},
	
	data = {},
	
	logging = 
	{   level = domoticz.LOG_INFO,
        marker = "Hue light tuin",
    },
	
	execute = function(dz, item)

local   ped1 = dz.devices(142) --hue tuinlamp 1
        garden = dz.devices().filter({142, 143, 144, 145, 146}) --hue tuinlampen
        gardenlightdoor = dz.devices(174) --hue lamp naast keukendeur
        gardenMotion = dz.devices(157)
    
    --motionsensor tuin, lamp naast keukendeur aan op beweging
    if item == gardenMotion and gardenMotion.state == 'On' then
        if (gardenlightdoor.active) then
            gardenlightdoor.dimTo(0).afterMin(3)
        else
            gardenlightdoor.dimTo(100).forMin(3)
        end
    end
    
    --motionsensor tuin, lampen in tuin aan op beweging
    if item == gardenMotion and gardenMotion.state == 'On' and (dz.time.matchesRule('between 23:30 and sunrise')) then
        if ped1.active then
            dz.log('Lampen waren aan en gaan nu uit')
            garden.forEach(function(device)
            device.switchOff().afterMin(3)
            end)
        else
            dz.log('Lampen gaan aan door beweging')
            dz.openURL({
                    url = 'http://10.0.1.27/api/xxxxxxxxxxxxxxxxxxxx/groups/4/action',
                    method = 'PUT',
                    postData = {scene = "2k1uz6x3yPrWSZd"} --scene 'energie'
                    })
            garden.forEach(function(device)
            device.switchOff().afterMin(3)
            end)
        end
    end

    --lampen tuin aan bij zonsondergang en uit rond 23:30uur
    if item.isTimer then
        if (dz.time.matchesRule('at sunset')) then 
            dz.openURL({
                    url = 'http://10.0.1.27/api/xxxxxxxxxxxxxxxxxxxxxxxxx/groups/4/action',
                    method = 'PUT',
                    postData = {scene = "-PHj0N3ZEsw37b2", transitiontime = 9000} --scene 'ontspannen' met een transitietijd van 15 minuten
                    })
        else
            dz.openURL({
                    url = 'http://10.0.1.27/api/xxxxxxxxxxxxxxxxxxxxxxxxxxx/groups/4/action',
                    method = 'PUT',
                    postData = {on = false} --uitschakelen alle lampen
                    }).withinMin(20)
        end
    end
 
end
}
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest