Page 1 of 1

Lua script not sensing state change

Posted: Wednesday 25 March 2020 16:11
by AlanGpio
Hi. First Lua script as functionality too complex for Blockly.

I'm running this on a Rasp Pi 4 under Buster and within a Docker container. Domoticz V4.10364 (c)2012-2019 GizMoCuz and Build Hash: 1cacdddf, Date: 2019-01-25 10:01:48. GPIO pins defined using sys/gpio exports.

The script is activated by push button (gpio6) going low (it is normally high). In the Switch definition the off state triggers this script (part shown).

The script needs to sense if the button is released (goes high) while the loop iterates for 10 seconds. The log (shown below) shows the code looping and shows the button going high but the script continues to loop. I tried lower case 'On' but no difference.

Code: Select all

if (otherdevices['ShutDownButton'] == 'Off') then
    local pressed = 1
    for i = 1, 10 do    -- check button is held down for 10 secs 
        print('in loop')
        print(otherdevices['ShutDownButton'])
        if (otherdevices['ShutDownButton'] == 'On') then   -- it's been released so quit
            pressed = 0
            print('break loop')
            break
        end
        os.execute("sleep " .. tonumber(1))
    end
    if (pressed == 1) then
        print('pressed is = 1')
	shutDown('ShutDownLED', 10, 0.5) -- blink 10 times with 0.5sec gap
    end
end
Log

Code: Select all

 2020-03-25 13:12:14.831 (Raspberry Pi 4 HMS) Lighting 2 (ShutDownButton)
2020-03-25 13:12:14.826 Status: Sysfs GPIO: gpio6 new state = off
2020-03-25 13:12:14.833 Status: LUA: in loop
2020-03-25 13:12:14.833 Status: LUA: Off
2020-03-25 13:12:15.309 (Raspberry Pi 4 HMS) Lighting 2 (ShutDownButton)
2020-03-25 13:12:15.304 Status: Sysfs GPIO: gpio6 new state = on
2020-03-25 13:12:15.847 Status: LUA: in loop
2020-03-25 13:12:15.847 Status: LUA: Off
2020-03-25 13:12:16.861 Status: LUA: in loop
2020-03-25 13:12:16.861 Status: LUA: Off
2020-03-25 13:12:17.875 Status: LUA: in loop 
+ 6 more loops
My first version of the script was activated by the state change and the if statement in the first line being

Code: Select all

if (devicechanged['ShutDownButton'] == 'Off') then
but this behaved the same.

Is Lua meant to pickup on state changes happening while the script is running? Is there another way to note a state change? or is there a error in my code?

Help appreciated. Thanks for reading.
Alan

Re: Lua script not sensing state change

Posted: Wednesday 25 March 2020 18:16
by waaren
AlanGpio wrote: Wednesday 25 March 2020 16:11 Is Lua meant to pickup on state changes happening while the script is running?
No it will receive the states when the script starts and state changes will not be updated in the variables inside the script.
Because the event-system is single threaded it is in general not a good idea to use loops in the scripts with a delay. This will block the entire event-system.
dzVents and classic Lua scripts can trigger on device state changes. (see the dzVents wiki for examples )

Re: Lua script not sensing state change

Posted: Wednesday 25 March 2020 22:13
by AlanGpio
Thanks again waaren :)

For my shutdown script it didn't matter that the loop impacted the event system, but your good advice is something to remember for more normal scripts.

I will look at dzVents but I'm moving to handling shutdown outside Domoticz using python. Over recent days I have written python scripts to send email/sms/mqtt/telegram so I can use these rather than Domoticz to send alerts. And later on I can link the python script to my UPS to handle prolonged power outages.

I've also read that linux shutdown command does allow apps to close down safely, hopefully that includes apps like Domoticz running in a Docker container.

Alan