Page 1 of 1

How to create a delay in a contact's On/Off sequence

Posted: Friday 23 May 2025 18:40
by Michel13
Hello,
I wrote a Lua script that allows me to control my pool's chlorinator. Without going into details that are irrelevant here, I need to cut off its power for 10 seconds to reset it. The device that controls the power is "On" when the power is off, and "Off" to power the chlorinator. I would like to do this:

Code: Select all

commandArray[stop_electrolyseur] = "On" -- power off
10 second delay
commandArray[stop_electrolyseur] = "Off" -- power on
But I fail to translate this sequence into a Lua script.
Could a kind soul tell me how to implement this sequence?

Re: How to create a delay in a contact's On/Off sequence

Posted: Friday 23 May 2025 18:50
by psubiaco
Try with this:
commandArray["stop_electrolyseur"] = "On FOR 10 seconds"

Re: How to create a delay in a contact's On/Off sequence

Posted: Friday 23 May 2025 18:58
by Michel13
Thank you psubiaco, so simple !
Just one question : if the contact is "On" before the sequence, is this command will do the same or is it necessary to turn it off before ?

Re: How to create a delay in a contact's On/Off sequence

Posted: Friday 23 May 2025 19:21
by HvdW
From the wiki. (Check that first)

Code: Select all

    -- switch on for 2 minutes after 10 seconds
    device.switchOn().afterSec(10).forMin(2)

    -- switch on at a specic time / day
    device.switchOn().at('09:00')                   -- earliest moment it will be 09:00 hr.
    device.switchOn().at('08:53:30 on fri')         -- earliest moment it will be Friday at 08:53:30
    device.switchOn().at('08:53:30 on sat, sun')    -- earliest moment it will be Saturday or Sunday at 08:53:30 (whatever comes first)

    -- switch on for 2 minutes after a randomized delay of 1-10 minutes
    device.switchOff().withinMin(10).forMin(2)
    device.close().forMin(15)
    device.open().afterSec(20)
    device.open().afterMin(2)

    -- switch on but do not trigger follow up events
    device.switchOn().silent()

    -- flash a light for 3 times
    device.switchOn().forSec(2).repeatAfterSec(1, 3)

    -- switch the device on but only if the current state isn't already on:
    device.switchOn().checkFirst()
    -- this is a short for:
    if (device.state == 'Off') then
        devices.switchOn()
    end

Re: How to create a delay in a contact's On/Off sequence

Posted: Friday 23 May 2025 20:11
by psubiaco
Michel13 wrote: Friday 23 May 2025 18:58 Thank you psubiaco, so simple !
Just one question : if the contact is "On" before the sequence, is this command will do the same or is it necessary to turn it off before ?
If it was on, you don't need to turn off than on for 10 seconds, but you may experience strange behaviours.

Re: How to create a delay in a contact's On/Off sequence

Posted: Friday 23 May 2025 21:10
by Michel13
psubiaco wrote: Friday 23 May 2025 20:11 you don't need to turn off than on for 10 seconds,
Ok, I understand.
At first, the power supply of the unit is Off (control device is On). I need to turn it Off to supply the chlorinator. At the next call (2 hours later), control device is Off and I need to turn it On for 10 sec to initiate and start a new cycle.
So here is my script :

Code: Select all

	if otherdevices[stop_electrolyseur] == "On" then
		commandArray[stop_electrolyseur] = "Off"
	else
		commandArray[stop_electrolyseur] = "On for 10 seconds" 
	end
Thanks for your help.

Re: How to create a delay in a contact's On/Off sequence

Posted: Friday 23 May 2025 21:19
by psubiaco
Your script can be called every minute, so you can use a variable to manage time to know what you have to do.

Please note you have also the "AFTER" keyword: I never tried, but maybe it works

Code: Select all

commandArray[device]="On for 10 seconds after 7200 seconds"
Check it, it may not work as I expected.

Re: How to create a delay in a contact's On/Off sequence

Posted: Friday 23 May 2025 22:13
by Michel13
Thanks psubiaco
My script is called every 15 minutes and time of operation of the chlorinator (2 hours) is managed properly elsewhere in the script. So it was only the question of supplying the unit on and off which was my only problem. Now it is solved thanks to you.