toddlock Posted December 16, 2022 Share Posted December 16, 2022 I'm trying to write a State Variable program that counts down by one. When I use -= 1, it counts down by more than one. How do I have the program just count one digit at a time and stop? Thank you in advance for any help. Link to comment
MrBill Posted December 16, 2022 Share Posted December 16, 2022 17 minutes ago, toddlock said: I'm trying to write a State Variable program that counts down by one. When I use -= 1, it counts down by more than one. How do I have the program just count one digit at a time and stop? Thank you in advance for any help. post the program. Right click the program name in the tree, pick the bottom entry "copy to clipboard" then paste it in a forum message. this also sounds like an X-Y Problem, so tell us what your really trying to accomplish because they may be a better way. 1 1 Link to comment
toddlock Posted December 16, 2022 Author Share Posted December 16, 2022 I have two programs, one to count up when Rain is forecasted by more than one day (ie rain is forecasted for two days it would set the State Variable by 2).... and this one that I'm trying to count down by one each days so it would take two days to clear the variable so the sprinklers would be off for two days. Variable Rain Delay Clear - [ID 0016][Parent 0001] If Time is 7:00:00AM And $RainDelay >= 1 Then $RainDelay -= 1 Else - No Actions - (To add one, press 'Action') Link to comment
randyth Posted December 16, 2022 Share Posted December 16, 2022 I suspect that program always sets the RainDelay variable to 0 because every time the Then statement decrements the value it re-triggers the program (it starts back at the If). I think you are going to have to change RainDelay to a regular (Integer) variable to prevent the If block from re-triggering. 1 Link to comment
MrBill Posted December 16, 2022 Share Posted December 16, 2022 (edited) You must be using a state variable. with this limited view of how the variable is being used my initial reaction is to say switch to an integer variable. The IF is triggering twice and apparently in the same second, or at least the ISY is seeing it that way. Whats happaning is the value is > 1 and the time is 7am which triggers the program, the fact it's a state variable as soon as the value changes the program is triggered again--because the value of the state variable changed. As noted the limited view fix is to change the variable to Integer which won't trigger the IF statement on any change. The other fix is to use two programs: Variable Rain Delay Clear.0 - [ID 0016][Parent 0001] If Time is 7:00:00AM Then Run Program Variable Rain Delay Clear.1 (if) Else - No Actions - (To add one, press 'Action') --------------- Variable Rain Delay Clear.1 - DISABLED If $RainDelay >= 1 Then $RainDelay -= 1 Else - No Actions - (To add one, press 'Action') The second program MUST be Disabled. A disabled program still runs when specifically run by another program, as in my example here. Be careful: The second program is a good example of how to create an infinite loop that brings an ISY to it's knee's. If $RainDelay is a state variable as soon as the value of $RainDelay >= 1 changes the program runs again because $RainDelay is a state variable and its value changes. When you write a program that has that potential I would recommend adding a delay to the beginning of the THEN statement so that ISY isn't caught in an infite loop that causes the admin console to respond slowly.... such as: If $RainDelay >= 1 Then Wait 2 seconds $RainDelay -= 1 the wait must be before (not after) the "$RainDelay -= 1" to be effective. Once you've entered the realm of intentionally disabled programs that are subroutines it's also a good practice to keep updated a program that runs on start up to make sure those programs stay disabled. We've seen odd conditions that make disabled programs enabled (usually firmware upgrades). Disabled-Intentionally - [ID 013A][Parent 0001] [Run at Startup] If - No Conditions - (To add one, press 'Schedule' or 'Condition') Then Disable Program 'Variable Rain Delay Clear.1' { .... add any others you create in the future here.... } Else - No Actions - (To add one, press 'Action') Edited December 16, 2022 by MrBill 2 Link to comment
toddlock Posted December 16, 2022 Author Share Posted December 16, 2022 I was using State variable and now switch Integer and my initial checkout that worked. I won't know for sure if completely works till we get rain forecasted and in Southern California that may be awhile. Also, thanks for the detailed variable explanation which will helps me understand how these variables work. Thanks again MrBill, till the next problem Cheers 1 Link to comment
dbwarner5 Posted December 16, 2022 Share Posted December 16, 2022 (edited) 58 minutes ago, toddlock said: I was using State variable and now switch Integer and my initial checkout that worked. I won't know for sure if completely works till we get rain forecasted and in Southern California that may be awhile. Also, thanks for the detailed variable explanation which will helps me understand how these variables work. Thanks again MrBill, till the next problem Cheers That's why it's always important to include your programs in your posts so that the core issue can be identified quickly for you.. good luck! And By the way , if you need a state variable for a different program as a trigger, assume your switched the above variable to I_RainDelay, you can always add a line $RainDelay= I_RainDelay in the THEN portion. Lastly to protect yourself from power outages, I always include a line after a variable gets adjusted, to init (set the value upon restart) to itself as shown in this simple program below: Then $Dark_Mode = 1 $Dark_Mode Init To $Dark_Mode Set 'KP GR D DarkMode' On Cheers. Edited December 16, 2022 by dbwarner5 1 Link to comment
MrBill Posted December 16, 2022 Share Posted December 16, 2022 2 hours ago, toddlock said: I was using State variable and now switch Integer and my initial checkout that worked. I won't know for sure if completely works till we get rain forecasted and in Southern California that may be awhile. Also, thanks for the detailed variable explanation which will helps me understand how these variables work. Thanks again MrBill, till the next problem Cheers State variables have the magical property of being able to trigger an IF statement. Integer variables act as a filter in an IF statement, they must be coupled with an AND to something else that provides a trigger or the program must have been run from another program. 1 Link to comment
Recommended Posts