This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Need Help with Lua Script for Group Object Control
#1
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.
Reply
#2
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)
------------------------------
Ctrl+F5
Reply
#3
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()
Reply
#4
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
Reply
#5
This should be resident script which periodical checks for the values, not event with such delay.
use logs to debug it.
------------------------------
Ctrl+F5
Reply
#6
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)
Reply
#7
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
Reply


Forum Jump: