Hi, I'm trying to understand how I can use local variables to make my code more efficient.
I've written an app that sets some parameters on my Inovelli switches/dimmers to set the LED notification RED if my garage door is open late. Often the garage door will open and close within 5 minutes, so I was looking for a way to NOT change the lights red if the door opened and then closed within 5 minutes, but if it's still open after 5 minutes, then make them red.
I was going to set a local variable true if the lights had been turned red, and then only change them back to blue if the variable is true, testing the variable in an ELSE IF statement. I think I was able to setup the variable "LightsRed" as Boolean and initialize it to False, but in the ELSE IF statement, it seemed to want to make me set the value, instead of check the value. I saw there was also Private Boolean, perhaps that could be used? Or do I need to make a virtual switch?
The code is working without the delay and local variable. This is what I was thinking of.
Trigger: GarageSensor changed or when time is sunset + 15 minutes
Delay 5 minutes
If (GarageSensor open and Time between sunset and sunrise) Then
Set various switch and dimmer parameters to RED (several lines of code)
Set LightsRed True
Else IF LightsRed True Then
Set various switch and dimmer parameters back to normal
End If
It sounds like you have the right idea and were just not sure about this step. I agree that it's not clear whether you're testing the value or setting it, but this is indeed how you'd set up a test of that value when making the conditional:
I should also note that if all you want to do is set the LED red if the door stays open for more than 5 minutes, I'm not sure you need the rule to be that complicated. (Perhaps you have other needs I'm not seeing here, and I didn't look at the actions much to verify the logic would flow as expected before I wrote any of the above.)
[EDIT: Nevermind, I forgot you want to change it back if you changed it to red, so you'll need to add more than I have below. But in your particular case, you'll want a "Cancel Delayed Actions" to cancel that 5-minute delay and also mark the delay as "Cancelable." But leaving the below anyway in case it's helpful. I could probably think of a more elegant way to do this that does what you want if I had more time...maybe later. Though if your Inovelli is Red Series, you could use the "notification" parameter instead of changing the "real" LED, then it will just go back to the either the "real" LED when the notification is cleared or another notification LED if you set one before that.]
This would set a light red if the door stays open for more than 5 minutes if the time is between sunset and sunrise:
Trigger:Door opens
Actions:
IF (NOT Time between Sunset and Sunrise) Exit Rule
Wait for event: Door closes --> timeout 0:05:00
IF (Door open) THEN
Set light Red (pseudo-code; use real action)
END-IF
However, that won't capture the case where the door is already open and then it gets late (i.e,. becomes time = sunset). Adding "Time is sunset" as a second trigger to the above should more or less address that issue, assuming you don't mind the fact that it would still take 5 minutes (after sunset) for the light to turn red and it would technically reset the 5-minute timer if the door was opened less than 5 minutes before sunset (so it might just take a few minutes longer but never more than 10 for it to turn red if it stays open during that particular transition).
I should note that you could also use a simple conditional (IF) instead of a "full" conditional (IF THEN ... END-IF) for that second case, but not if you have more than one action "inside," which it looks like you do (my example doesn't). I did use a simple conditional for the "Exit Rule" line, but again, either way would work as long as (it meets your needs and) it's properly formed.
Many Thanks guys. I was close on the local variable, so I'll give that a try first. Just getting into this, it's been about 30 years since I did any programming, and that was Fortran.