Page 1 of 1

Pause/sleep inside a dzVents script: logic error ?

Posted: Sunday 21 March 2021 10:30
by fdemees
Hello dear Community,

I have acquired new shades and got them delivered with standard motors, one up and one down.
I have interfaced them with two standard 2-way NO/NC relays which work fine. (like Sonoff 4ch-Pro)
I have used Zigbee push-buttons configured as selectors : single push= open or close (toggle) ; double push= stop where it is.
I would like to insert a short pause of e.g. 200 ms on a single push in order to avoid brutal direction changes.
E.g. stop, sleep 200 ms, change direction, start

Devices:
98 is my selector push-button
74 is the relay that brings power or not
75 is the relay that drives one motor or the other one, so that they are never both powered-on at the same time.

Here is the script that does not work:

Code: Select all

function sleep(n)
    os.execute("sleep " .. tonumber(n/10))
end

return {

   on = { devices = { 98 }},        -- idx of button
               
   execute = function(dz, item )
           dz.log("state of " .. item.name .. " is " .. item.state)
           if item.state == "1 Click" then
            dz.devices(74).switchOff()    -- idx of power switch
            sleep(5)
            dz.devices(75).toggleSwitch()    -- idx of up/down switch
            dz.devices(74).switchOn()      -- idx of power switch
        elseif item.state == "2 Click" then
            dz.devices(74).switchOff()    -- idx of power switch
        end
   end
}
The actions are executed in the wrong order. The sleep statement works, but the switch-off is executed after the sleep.

What am I doing wrong ?

Thank you for your replies ;)
Frédéric

Re: Pause/sleep inside a dzVents script: logic error ?

Posted: Sunday 21 March 2021 12:10
by erem
encroaching on @waarens territory who is the resident lua guru, but you could try

Code: Select all

            dz.devices(74).switchOff()    -- idx of power switch
            dz.devices(75).toggleSwitch().afterSec(5)    -- idx of up/down switch

Re: Pause/sleep inside a dzVents script: logic error ?

Posted: Sunday 21 March 2021 13:07
by fdemees
Hello Rob,

I have brought some debug commands in order to log more:

Code: Select all

return {

   on = { devices = { 98 }},        -- idx of button
               
   execute = function(dz, item )
           dz.log("state of " .. item.name .. " is " .. item.state)
           if item.state == "1 Click" then
            dz.log("switching 74 off")   
            dz.devices(74).switchOff()    -- idx of power switch
            dz.log("sleeping")
            -- sleep(20)
            dz.log("toggling 75")
            dz.devices(75).toggleSwitch().afterSec(3)    -- idx of up/down switch
            dz.log("switching 74 on")
            dz.devices(74).switchOn()      -- idx of power switch
        elseif item.state == "2 Click" then
            dz.devices(74).switchOff()    -- idx of power switch
        end
   end
}

2021-03-21 12:54:52.544 Status: User: Admin initiated a switch command (98/Bouton Store 2 (B8)/Set Level)
2021-03-21 12:54:52.801 Status: dzVents: Info: Handling events for: "Bouton Store 2 (B8)", value: "1 Click"
2021-03-21 12:54:52.801 Status: dzVents: Info: ------ Start internal script: dz Store2 Toggle: Device: "Bouton Store 2 (B8) (Zigbee)", Index: 98
2021-03-21 12:54:52.801 Status: dzVents: Info: state of Bouton Store 2 (B8) is 1 Click
2021-03-21 12:54:52.802 Status: dzVents: Info: switching 74 off
2021-03-21 12:54:52.804 Status: dzVents: Info: sleeping
2021-03-21 12:54:52.804 Status: dzVents: Info: toggling 75
2021-03-21 12:54:52.806 Status: dzVents: Info: switching 74 on
2021-03-21 12:54:52.807 Status: dzVents: Info: ------ Finished dz Store2 Toggle
2021-03-21 12:54:52.809 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua

When I action the button, the switch 74 goes immediately on, and the 75 toggles after 3 seconds.
I understand that all actions are issued simultaneously (see the time stamps).

So, here is the final solution that I adopt:

Code: Select all

           if item.state == "1 Click" then
            dz.devices(74).switchOff()    -- idx of power switch
            dz.devices(75).toggleSwitch().afterSec(0.2)    -- idx of up/down switch
            dz.devices(74).switchOn().afterSec(0.4)      -- idx of power switch
Thank you for showing me the way ;)

Re: Pause/sleep inside a dzVents script: logic error ?  [Solved]

Posted: Sunday 21 March 2021 13:21
by waaren
fdemees wrote: Sunday 21 March 2021 10:30 I would like to insert a short pause of e.g. 200 ms on a single push in order to avoid brutal direction changes.
The actions are executed in the wrong order. The sleep statement works, but the switch-off is executed after the sleep.
Frédéric
The sleep does work but the commands switchOff(), switchOn() and toggeSwitch() (or any command passed back to domoticz device, scenes, vars, etc..) are send after the script finished using one table (the commandArray). That's why a pause, sleep or wait does not make sense in a script type like this.

A working version could look like.

Code: Select all

return
{
   on =
   {
        devices =
        {
            98,         -- idx of button
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'toggleBlinds',
    },

   execute = function(dz, item )
        local powerSwitch = dz.devices(74)
        local relay = dz.devices(75)

        dz.log("state of power switch " .. powerSwitch.state, dz.LOG_DEBUG )
        dz.log("state of " .. item.name .. " is " .. item.state, dz.LOG_DEBUG )

        if item.state == "1 Click" then
            powerSwitch.switchOff()    -- idx of power switch
            relay.toggleSwitch.afterSec(0.2)
            powerSwitch.switchOn().afterSec(0.5)
        elseif item.state == "2 Click" then
            powerSwitch.switchOff()
        end
   end
}

Re: Pause/sleep inside a dzVents script: logic error ?

Posted: Monday 22 March 2021 15:49
by fdemees
Thanks, it is exactly what I wanted to achieve, have a nice day. Erem and you put me on the right way.

Frédéric