How to stop a running script.  [Solved]

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

Moderator: leecollings

Post Reply
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

How to stop a running script.

Post by EddyG »

I have a Zigbee switch with multiple command.
I would like to have to script running at one command from the switch and stopped at a second command from the switch.
I tried to do that with setting an unsetting a data variable, but that does not work.
The initial script keeps on running until the lua 10 sec message in the Domoticz log.
This is my script.

Code: Select all

local socket = require 'socket'

return {
    active = true,
--  active = false,

    on = { devices = { 'SchakelaarEddy' } },

    data = { Running = {initial=0} },

    execute = function(dz, device)
        Status = device.state
        Level = dz.devices("Lamp Eddy").level
        print('Device ' .. device.name .. ' Level: '.. Level .. ' Status:' .. Status)
        if Status == 'On' then
            dz.devices("Lamp Eddy").switchOn()
            dz.devices("Lamp Eddy").dimTo(80)
        elseif Status == 'Off' then
            dz.devices("Lamp Eddy").switchOff()
            dz.devices("Lamp Eddy").dimTo(0)
        elseif Status == 'Up' then
            dz.data.Running = 1
            repeat
                socket.sleep(0.5)
                Level = Level + 10
                if Level > 100 then
                    Level = 100
                end
                dz.devices("Lamp Eddy").dimTo(Level)
            until dz.data.Running == 0
        elseif Status == 'Down' then
            dz.data.Running = 1
            repeat
                socket.sleep(0.5)
                Level = Level - 10
                if Level < 0 then
                    Level = 0
                end
                dz.devices("Lamp Eddy").dimTo(Level)
            until dz.data.Running == 0
        elseif Status == 'Stop' then
            dz.data.Running = 0
        end
    end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to stop a running script.

Post by waaren »

EddyG wrote: Wednesday 04 March 2020 11:47 I have a Zigbee switch with multiple command.
I would like to have to script running at one command from the switch and stopped at a second command from the switch.
This cannot work as the dzVents script receives the state of all switches, uservars, etc. at the beginning of the script (via the first parm in the execute function) Any changes in the domoticz environment during the execution of the script are not passed to it.

You should keep the execution time of any event based script (dzVents, Lua or Python) to a minimum because the domoticz event-system is single threaded (during execution of one script no other scripts can start). Effectively you blocked the entire event-system with the loop you coded in your script.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: How to stop a running script.

Post by EddyG »

Tnx for the answer. Could it work if I write/read a status file to the filesystem?
Or is there an other solution?
The script as above runs max. 10 * 0.5 = 5 seconds

edit:
Sorry I know the answer: single threaded.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to stop a running script.

Post by waaren »

EddyG wrote: Wednesday 04 March 2020 14:38 Or is there an other solution?
Yes, Try below script.

Code: Select all

return 
{
    active = true,

    on = 
    { 
        devices = 
        { 
            'SchakelaarEddy',
        },
    },

    logging =   
    {
        level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when tested and OK
        marker = 'dimmer', 
    },

    execute = function(dz, item)
        local myLamp = dz.devices('Lamp Eddy')
        local myLevel = myLamp.level

        local myState = item.state
        local myDirection = item.state -- use item.levelName if controlling device is a selector

        local delay = 0 

        dz.log('Device ' .. item.name .. ', Level: '.. myLevel .. ', Status: ' .. myState, dz.LOG_DEBUG)

        myLamp.cancelQueuedCommands()

        if myState == 'On' then
            myLamp.dimTo(80)
        elseif myState == 'Off' then
            myLamp.dimTo(1)
            myLamp.dimTo(0).afterSec(0.1)
        elseif myDirection == 'Up' then
            for newLevel = myLevel, 105, 5 do
                myLamp.dimTo(newLevel).afterSec(delay)
                delay = delay + 0.5
            end
        elseif myDirection == 'Down' then
            for newLevel = myLevel, -5, -5 do
                myLamp.dimTo(newLevel).afterSec(delay)
                delay = delay + 0.5
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: How to stop a running script.

Post by EddyG »

Tnx, for the script. I will experiment with it.
It is not exactly as I wanted it. I want to use it with increments of dimming until I release the button and the event Stop comes and the increase in dimming stops.
But due to the nature of dzVents that will not be possible.
In this case I will connect the switch directly to the bulb (both IKEA).
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to stop a running script.  [Solved]

Post by waaren »

EddyG wrote: Wednesday 04 March 2020 19:51 It is not exactly as I wanted it. I want to use it with increments of dimming until I release the button and the event Stop comes and the increase in dimming stops.
Using my proposed script, the dimming will stop as soon as the switch sends a "stop" because of the cancelQueuedCommand().
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest