Logic Machine Forum
Need Help with Lua Script for Group Object Control - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Need Help with Lua Script for Group Object Control (/showthread.php?tid=4875)



Need Help with Lua Script for Group Object Control - josdegroot - 16.07.2023

Hello everyone,

I've been working on a Lua script to control a group object in my system, but I'm having some trouble getting it to work as expected. I was hoping someone here might be able to help me figure out what's going wrong.

Here's the idea behind the script: I want to check a specific group object (with ID "0/0/21") and see if it has not been turned on for at least 5 minutes. If it hasn't, I want to turn it on for 3 minutes and then turn it off again.

Code:
-- Define the group object ID
local groupObjectID = "0/0/21"

-- Define the time duration in seconds
local fiveMinutesInSeconds = 5 * 60
local threeMinutesInSeconds = 3 * 60

-- Check the current status of the group object
local currentStatus = grp.getvalue(groupObjectID)

-- If the group object is off, start the timer
if currentStatus == 0 then
    -- Check if the timer has already been started
    if not offTime then
        -- Start the timer
        offTime = os.time()
    else
        -- Check if the group object has been off for at least 5 minutes
        if os.time() - offTime >= fiveMinutesInSeconds then
            -- Turn on the group object
            grp.setvalue(groupObjectID, true)

            -- Wait for 3 minutes
            os.execute("sleep " .. threeMinutesInSeconds)

            -- Turn off the group object
            grp.setvalue(groupObjectID, false)

            -- Reset the timer
            offTime = nil
        end
    end
else
    -- If the group object is on, reset the timer
    offTime = nil
end



I'm using the `grp.getvalue(alias)` function to check the last time the group object was turned on, and the `grp.setvalue(alias, value)` function to turn the group object on and off. However, the script doesn't seem to be working as expected.

Does anyone have any ideas about what might be going wrong? Any help would be greatly appreciated!

Thanks in advance.


RE: Need Help with Lua Script for Group Object Control - Daniel - 17.07.2023

It would be better to do it differently. Instead of making your own timer just use object update time and compare it with os.time to see how long the object was not updated.
Code:
myobject = grp.find('1/1/1')
log(myobject.updatetime)



RE: Need Help with Lua Script for Group Object Control - admin - 17.07.2023

Problems with your script:
1. currentStatus is most likely Boolean and comparing it with 0 won't work because it's true/false. You can simply write it as if currentStatus then instead.
2. Use os.sleep(seconds) instead of os.execute().
3. There's no grp.setvalue() function - it is called grp.write()


RE: Need Help with Lua Script for Group Object Control - josdegroot - 17.07.2023

Tanks for the tips... i tried to execute all the tips in the new script... but it's still not working Big Grin

Code:
-- Define the group object
local myobject = grp.find('0/0/21')

-- Define the time duration in seconds
local fiveMinutesInSeconds = 5 * 60
local threeMinutesInSeconds = 3 * 60

-- Check the last update time of the group object
local lastUpdateTime = myobject.updatetime

-- Check if the group object has not been updated for at least 5 minutes
if os.time() - lastUpdateTime >= fiveMinutesInSeconds then
    -- Turn on the group object
    grp.write(myobject, true)

    -- Wait for 3 minutes
    os.sleep(threeMinutesInSeconds)

    -- Turn off the group object
    grp.write(myobject, false)
end



RE: Need Help with Lua Script for Group Object Control - Daniel - 18.07.2023

This should be resident script which periodical checks for the values, not event with such delay.
use logs to debug it.


RE: Need Help with Lua Script for Group Object Control - admin - 18.07.2023

grp.write expects first argument to be a group address or a name, but you are passing a table from grp.find.
Use this instead:
Code:
myobject:write(true)



RE: Need Help with Lua Script for Group Object Control - josdegroot - 18.07.2023

Thanks for the reply!

This one is working

Code:
-- Define the group object
local myobject = grp.find('0/0/21')

-- Define the time duration in seconds
local fiveMinutesInSeconds = 5 * 60
local threeMinutesInSeconds = 3 * 60

-- Check the last update time of the group object
local lastUpdateTime = myobject.updatetime

-- Check if the group object has not been updated for at least 5 minutes
if os.time() - lastUpdateTime >= fiveMinutesInSeconds then
    -- Turn on the group object
    myobject:write(true)

    -- Wait for 3 minutes
    os.sleep(threeMinutesInSeconds)

    -- Turn off the group object
    myobject:write(false)
end