Using global variables in a trigger

(Greg) #1

So I am trying set up some global variables. Basically I am setting a global variable the holds the lights in in the bedroom.

If I put in a if statement that says if @bedroomLights do whatever, then the piston will never fire. Yet if I make a loval variable and use that instead of the global then the piston will fire.

The global variable will work farther down in the piston just not for the trigger. Am I missing something?


( I hate Mondays) #2

Do you have a sample?

(Greg) #3

Sorry about that. This is one for my garage door that has the same issue. The code is using the global variable for the if trigger. You will notice I have also set a local variable. If I switch to that the code runs fine.

The global and local are set to the same thing.

(Robin) #4

I’m not sure why the global won’t trigger the piston but when you get that part working try changing your message value to:

"WARNING - The garage door has been open for " {formatDuration(Age([@garageSensor : contact]), true, “s”, false)}

Gives a running duration in hours, minutes, seconds.

(Greg) #5

For now I will just use the local variable so it works and keep trying to figure out why the global isnt working.

But thanks for the warning statement. That will really help.

( I hate Mondays) #6

Global device variables do not subscribe to events by design - this is because a later update of said global variable would NOT cause all pistons to resubscribe to the newly contained devices, whereas a local variable would require the user to save the piston, hence resubscribing.

(Greg) #7

Got ya! So use the local variable for a trigger.