Lua script not sensing state change Topic is solved

Moderator: leecollings

Post Reply
User avatar
AlanGpio
Posts: 15
Joined: Monday 24 February 2020 21:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Lua script not sensing state change

Post 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
Hobbyist developing home automation on a Rasp Pi 4 under Buster off SSD.
Custom electronics to link to alarm system, doors, lights, cameras and door bell.
Running Domoticz in a Docker Container along with containers for Moquitto and Portainer.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Lua script not sensing state change

Post 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 )
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
AlanGpio
Posts: 15
Joined: Monday 24 February 2020 21:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua script not sensing state change

Post 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
Hobbyist developing home automation on a Rasp Pi 4 under Buster off SSD.
Custom electronics to link to alarm system, doors, lights, cameras and door bell.
Running Domoticz in a Docker Container along with containers for Moquitto and Portainer.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests