Prevent loop with screen

Moderator: leecollings

Post Reply
reneklomp
Posts: 30
Joined: Monday 20 February 2017 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Prevent loop with screen

Post by reneklomp »

Guys,
Being a rather newbie to Domoticz/LUA I am risking being flamed with RTFM but I have searched but still not find a simple answer...

I have a screen (blind) that I would like to automate. However, the screen does not stop by itself - there is no brake/block on it.
So I need to activate the 'On' switch to start closing the screen and then after 30 seconds I need to switch 'On' again and it will stop.

Now I tried with simple LUA script script_device_screen.lua (with time set to 5 seconds for testing) but this ends in an infinite loop where the screen starts closing, then after 5 seconds it stops (fine!). However, after yet another 5 seconds it starts further closing again and 5 seconds later it stops again... and so on and so on. It obviously keeps generating events which is logical now...

The initial state of the screen is 'Open'.
When I push the 'On' switch the state will change to 'Closed'.
However, when I push the 'On' switch again after 30 seconds the state will remain 'Closed'.
So simple testing if the state changed to 'Closed' is not enough and will result in looping.

I assume I need to test and compare to the previous state the screen was in so I only need to send 'On' again when it changed from 'Open' to 'Closed', not when it 'changed' from 'Closed' to 'Closed'...

Reading to some of the posts it looks to me I need to use uservariables to keep the old state but I am not sure if there are even simpler solutions that I might be missing here...
I am assuming this is a common pattern and I am looking for some guidance/example here.

Thanks, Rene.
reneklomp
Posts: 30
Joined: Monday 20 February 2017 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Prevent loop with screen

Post by reneklomp »

I have tried with uservariables now but it not working...
I have declared a user variable through the UI that seems be getting updated alright.
Also looking at the logs, it is going through the right logic.
It is just not sending the 2nd 'On' statement so it is not stopping after 5 seconds.

Any help appreciated?!

Code: Select all

commandArray = {}

-- loop through all the changed devices
for deviceName,deviceValue in pairs(devicechanged) do
--    print ("Device based event fired on '"..deviceName.."', value '"..tostring(deviceValue).."'");
    if (deviceName=="Screen 2") then
        print ("Screen 2")
        if (deviceValue == "Closed" and uservariables['screen2_oldstatus'] == "Open") then
            print ("Open -> Closed")
            commandArray['Variable:screen2_oldstatus'] = "Closed"
            commandArray['Screen 2'] = 'On After 5'
        end

        if (deviceValue == "Open" and uservariables['screen2_oldstatus'] == "Closed") then
            print ("Closed -> Open")
            commandArray['Variable:screen2_oldstatus'] = "Open"
            commandArray['Screen 2'] = 'Off After 5'
        end
    end
end

return commandArray
zak45
Posts: 953
Joined: Sunday 22 January 2017 11:37
Target OS: Windows
Domoticz version: V2024.4
Contact:

Re: Prevent loop with screen

Post by zak45 »

hi,
maybe this:
commandArray['Screen 2'] = 'On AFTER 5 SECONDS'
reneklomp
Posts: 30
Joined: Monday 20 February 2017 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Prevent loop with screen

Post by reneklomp »

It is working now! It seems like 'After' and 'AFTER' makes a difference!?

Rene.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest