Page 1 of 1

[SOLVED]Problem with script to activate a scene with "complicated" timing requirements

Posted: Monday 29 May 2023 14:43
by Sjonnie2017
Hi All,

I am not very proficient in programming but I try and sometimes it works. This time it doesn't :(

I have a scene consisting of three roller shutters that open on sunrise. I would like the scene to open at sunrise unless sunrise is earlier that 6 am in the morning. The scenes' timers does not have that option so I tried to create a dzVents script to make it happen. Note that I have disabled the timers in the scene so it would not interrupt the script.

The first roller shutter opens (not sure when because I generally sleep before 6 am ;) ) the other two screens defined in the scene do not respond.

Here is the code I have been able to create after some reading.

Code: Select all

-- Script to activate "scene" "Luiken Water Open" so they won't open before 6 am on workdays
-- Scene ¨Luiken Water open" = idx 9
-- The scene consists of three roller shutters. One opens, the other two don't.

return
{
    active = true,
    on =
    {
        scenes = {9},
        timer = {'2 minutes before sunset on mon, tue, wed, thu except between 04:00 and 06:05', '2 minutes before sunset except between 04:00 and 08:00 on fri, sat, sun'},
    },
    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'LuikenWaterOpen',
    },

    execute = function(dz, item)
    dz.scenes(9).switchOn()
    end
}
If anybody is willing to assist, be aware that this will be a "slow chat" because I have to wait till next morning to test it ;)

Any help appreciated!

Re: Problem with script to activate a scene with "complicated" timing requirements

Posted: Monday 29 May 2023 22:50
by waltervl
Scripts seems ok, so it is the scene that is not reacting correctly.
You can check the device logging to see if they got the switch on command.

If you switch on the scene manually all 3 blinds are opening?

Alternatively you can try to switch the 3 blinds directly from the script instead of using a scene.

Re: Problem with script to activate a scene with "complicated" timing requirements

Posted: Tuesday 30 May 2023 6:06
by Sjonnie2017
Thanks for your reply and suggestion!

The scene functions as expected when activated manually.

I will replace the scene with the actual devices I want to control in this script.

Will report back (in a day or so :lol: )

Re: Problem with script to activate a scene with "complicated" timing requirements

Posted: Tuesday 30 May 2023 6:19
by Kedi
Did you try:

Code: Select all

        timer = {'2 minutes before sunset on mon, tue, wed, thu except at 04:00-06:05', '2 minutes before sunset on fri, sat, sun except at 04:00-08:00'},

Re: Problem with script to activate a scene with "complicated" timing requirements

Posted: Tuesday 30 May 2023 9:24
by waltervl
It could indeed also be that your script is not running at all and something else (script, timer) is controlling that 1 moving blind.
Perhaps put a log statement after switchOn() so you can check.

Re: Problem with script to activate a scene with "complicated" timing requirements

Posted: Tuesday 30 May 2023 10:05
by hartwich
Don't you have to use sunrise instead of sunset in the timer?

Re: Problem with script to activate a scene with "complicated" timing requirements

Posted: Tuesday 30 May 2023 14:44
by Kedi
You have a trigger on Scenes(9) and you are also switching Scene(9). That is not OK.

Re: Problem with script to activate a scene with "complicated" timing requirements

Posted: Tuesday 30 May 2023 18:18
by Sjonnie2017
Thank you all for your support and ideas. Much appreciated. I modified the script by changing the timer to this:

Code: Select all

timer = {'2 minutes before sunrise on mon, tue, wed, thu except between 04:00-06:05', '2 minutes before sunrise except between 04:00-08:00 on fri, sat, sun'},
I made indeed a silly mistake by using sunset instead of sunrise (thanks @hartwich!) and changed the word "and" to a hyphen.

I don't quite understand the remark of @Kedi. Maybe you could explain?

I am sorry that I don't any real coding skills to speak of but I am trying and willing to learn ;)

For now I left the scene in the script to check what happens tomorrow. If the changes I made do not provide the desired effect, I will change the script to use the devices contained in the scene.

Re: Problem with script to activate a scene with "complicated" timing requirements

Posted: Tuesday 30 May 2023 19:03
by waltervl
Kedi wrote: Tuesday 30 May 2023 14:44 You have a trigger on Scenes(9) and you are also switching Scene(9). That is not OK.
@Sjonnie2017 If you have questions of this remark?
So just above timer= .... you l have scenes =
That means that the script is triggered when the scene is activated. That could lead to a race condition:
Timer triggered the script which switches the scene.
Scene is activated so script is triggered so scene is activated (again..)
Scene is activated so script is triggered so scene is activated again and again.

Conclusion: remove the scenes = (9) from the on= trigger section

Re: Problem with script to activate a scene with "complicated" timing requirements

Posted: Tuesday 30 May 2023 19:48
by Sjonnie2017
Hi Walter,

Tx for your thorough explanation. I removed the line in the on= section.

Let's see what happens! :)

Re: Problem with script to activate a scene with "complicated" timing requirements

Posted: Tuesday 30 May 2023 22:38
by Kedi
Look exactly at my line for the timer, it is different from yours.
Just test it.

Re: Problem with script to activate a scene with "complicated" timing requirements

Posted: Wednesday 31 May 2023 1:08
by boum
If it's too complicated, you can move stuff inside the execute function. There you can also add log output to debug the behavior of the script

Code: Select all

return
{
    active = true,
    on =
    {
        timer = {'2 minutes before sunrise'},
    },
    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'LuikenWaterOpen',
    },

    execute = function(dz, item)
    	if dz.time.matchesRule('at 04:00-06:05 on mon, tue, wed, thu')
    	  or dz.time.matchesRule('at 04:00-08:00 on fri, sat, sun') then
    	    dz.log('Too early, not activating', dz.LOG_DEBUG)
    	else
	    dz.scenes(9).switchOn()
	end
    end
}

Re: Problem with script to activate a scene with "complicated" timing requirements

Posted: Wednesday 31 May 2023 6:15
by Sjonnie2017
Hi all,

Thanks again!

The blinds opened too early probably due to the fact that I didn't copy the exact line from @Kedi. I changed the line now. Will see how this works out tomorrow. If that does not work then I will try the solution by @boum.

Keep you posted!

Re: Problem with script to activate a scene with "complicated" timing requirements

Posted: Wednesday 31 May 2023 9:06
by waltervl
You can also add another timer trigger for a daytime action just to test if the script is working.... be aware that the time is more than 2 minutes in the future to get Domoticz read the triggers

eg

Code: Select all

timer = {
'2 minutes before sunset on mon, tue, wed, thu except at 04:00-06:05', 
'2 minutes before sunset on fri, sat, sun except at 04:00-08:00',
'at 13:45'
},

Re: Problem with script to activate a scene with "complicated" timing requirements

Posted: Thursday 01 June 2023 7:59
by Sjonnie2017
Hi all,

The script works thanks to all your tips and support! :mrgreen:

The blinds don't open when sunrise is between 04:00 and 06:05 (or 08:00 for that matter). In fact they obviously don't open after 06:05 as well which was expected. I now have the timer in the scene set to open on 06:06. All working as I want now.

Tx again!