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

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

Moderator: leecollings

Post Reply
User avatar
Sjonnie2017
Posts: 364
Joined: Wednesday 02 August 2017 19:43
Target OS: Linux
Domoticz version: Latest ß
Location: The Netherlands
Contact:

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

Post 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!
Last edited by Sjonnie2017 on Thursday 01 June 2023 7:59, edited 1 time in total.
ConBee II - TRÅDFRI lights + switches, loads of ChingLing dimmers and switches, Heiman and Xiaomi sensors
SolarEdge SE4000H (with active modbus_tcp)
YouLess Energy meter
Shelly 2.5 in roller shutter mode
User avatar
waltervl
Posts: 5779
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

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

Post 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.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
Sjonnie2017
Posts: 364
Joined: Wednesday 02 August 2017 19:43
Target OS: Linux
Domoticz version: Latest ß
Location: The Netherlands
Contact:

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

Post 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: )
ConBee II - TRÅDFRI lights + switches, loads of ChingLing dimmers and switches, Heiman and Xiaomi sensors
SolarEdge SE4000H (with active modbus_tcp)
YouLess Energy meter
Shelly 2.5 in roller shutter mode
Kedi
Posts: 569
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

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

Post 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'},
Logic will get you from A to B. Imagination will take you everywhere.
User avatar
waltervl
Posts: 5779
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

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

Post 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.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
hartwich
Posts: 2
Joined: Sunday 15 July 2018 16:39
Target OS: -
Domoticz version:
Contact:

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

Post by hartwich »

Don't you have to use sunrise instead of sunset in the timer?
Kedi
Posts: 569
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

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

Post by Kedi »

You have a trigger on Scenes(9) and you are also switching Scene(9). That is not OK.
Logic will get you from A to B. Imagination will take you everywhere.
User avatar
Sjonnie2017
Posts: 364
Joined: Wednesday 02 August 2017 19:43
Target OS: Linux
Domoticz version: Latest ß
Location: The Netherlands
Contact:

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

Post 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.
ConBee II - TRÅDFRI lights + switches, loads of ChingLing dimmers and switches, Heiman and Xiaomi sensors
SolarEdge SE4000H (with active modbus_tcp)
YouLess Energy meter
Shelly 2.5 in roller shutter mode
User avatar
waltervl
Posts: 5779
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

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

Post 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
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
Sjonnie2017
Posts: 364
Joined: Wednesday 02 August 2017 19:43
Target OS: Linux
Domoticz version: Latest ß
Location: The Netherlands
Contact:

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

Post by Sjonnie2017 »

Hi Walter,

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

Let's see what happens! :)
ConBee II - TRÅDFRI lights + switches, loads of ChingLing dimmers and switches, Heiman and Xiaomi sensors
SolarEdge SE4000H (with active modbus_tcp)
YouLess Energy meter
Shelly 2.5 in roller shutter mode
Kedi
Posts: 569
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

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

Post by Kedi »

Look exactly at my line for the timer, it is different from yours.
Just test it.
Logic will get you from A to B. Imagination will take you everywhere.
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

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

Post 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
}
User avatar
Sjonnie2017
Posts: 364
Joined: Wednesday 02 August 2017 19:43
Target OS: Linux
Domoticz version: Latest ß
Location: The Netherlands
Contact:

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

Post 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!
ConBee II - TRÅDFRI lights + switches, loads of ChingLing dimmers and switches, Heiman and Xiaomi sensors
SolarEdge SE4000H (with active modbus_tcp)
YouLess Energy meter
Shelly 2.5 in roller shutter mode
User avatar
waltervl
Posts: 5779
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

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

Post 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'
},
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
Sjonnie2017
Posts: 364
Joined: Wednesday 02 August 2017 19:43
Target OS: Linux
Domoticz version: Latest ß
Location: The Netherlands
Contact:

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

Post 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!
ConBee II - TRÅDFRI lights + switches, loads of ChingLing dimmers and switches, Heiman and Xiaomi sensors
SolarEdge SE4000H (with active modbus_tcp)
YouLess Energy meter
Shelly 2.5 in roller shutter mode
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest