When to use Global Variable vs Virtual Switch


#1

I’m still working on streamlining the pistons that control my location mode (away, home, etc).

I’ve set up a number of global variables (@IsBedtime, @IsAsleep, etc). I’m curious if I should be turning on/off virtual switches instead of setting global variables.

Are there pros/cons to either method?


#2
  • Simulated Switches will turn on or off nearly the instant that line of code is executed
  • Global variables will update after the very last line of code has been executed.

#3

Virtual switches are also easier to toggle in case the automation doesn’t work, or you run into a special case. You can always flip the switch on a dashboard.


#4

I believe a global variable does not undergo command optimization like a vswitch will.


#5

Thanks guys. I guess I’ll switch my variables over to switches for this use case. I also didn’t realize that variables weren’t updated until the last line of codes runs (which explains a few things).


#6

Global variables are great IF a variable needs to do 3-4 or more things depending on the situation.
ie : my rooms are using global variables because they have 3-4 different settings.
@movieroom = 1
@movieroom = 5
@movieroom = 7
@movieroom = 10

each number indicates different lights/motion sensors/media settings etc.
I could still do this with sim switches but didn’t want to TURN ON 1 switch and TURN OFF 3 on an on…
I simply just change the variable…


#7

Good point. Non-boolean global variables can’t be done easily with switches.


#8

I’m curious on how this functions. If movieroom = 1, what happens?


#9

Note : I didn’t check my piston to give actual numbers here because it doesn’t matter. (just checked actual numbers are: 1, 2, 4, 7)

@movieroom = 1 is regular light/motion sensor combination
Around sunset until we go to sleep (Sleep time is @movieroom= 7)
IF motion lights turn ON
If no motion 3 minutes lights turn OFF

@movieroom = 4 is lights ON and motion sensors are by passed so lights remain ON

stuff ike that…


#10

How did you determine the numbering scheme?


#11

Well the first part is the usual stuff:
1- Ipads on the walls with Action tiles (simulated switches determine the global variable number)
2- Alexa voice commands (same simulated switches turn ON / OFF)
3 - Timers
4 - A couple of zwave buttons around the house. (just in case if voice commands fail etc) plus my family sometimes don’t like talking to Alexa LOL

The second part is more like as if the house is “intuitive.”
let me explain how that works:
(I got this design idea from @WCmore while he was working on my house)
Every room is controlled with global variables as I mentioned earlier.
Each room acts according to how other rooms are.
lets say, movie room’s variable is 5. Which means we are watching a movie, sound is blasting, projection is on, lights are off etc.
While movie room changes to 5, halway turns 7. Which means lights are off but if there is motion they turn ON full brightness (bathroom break maybe?)
If movie room is 5 BUT Master bedroom changes to 1, it means one of us is going to bed. Master bedroom 1 turns all the lights off in the bedroom and bathroom and motion sensors are no longer in the picture. only a single sensor controlls a single light bulb in the master bathroom with 25% brightness (mid night bathroom need:)
Since someone went to bed and M. bedroom changed to 1, movie room changes to 6. (meaning lower volume) and halway turns 5, which means lights are still controlled by motion sensors but dimmed to 30% (because people are sleeping now)

Maybe sounds complicated but it’s really not.

in order to make this work properly, you need to take a couple of simple steps.
1 - Your house must be controlled ONLY through WebCore.
2 - You need to design what each room will do on the paper first and decide the variables. Print that out and keep it handy:)
3 - You need two pistons for each room. One determinds the variable, other one controlls stuff
update :

here are two examples i use for the bedroom, incase if anyone else reading this wants to see it.


#12

This is a very interesting concept and I like the approach. I live in a fairly small apartment, so I’m not sure that I can do much with this, unfortunately. I need to think some more on this maybe and attempt to implement this here.


#13

I probably still use the same approach even if i lived in a small place because I like that so called “intuitive” behaviour of the system…


#14

For sure, it makes sense. I don’t have any interior motion sensors or anything, so I mainly control lights/security with my automations - and with it simply being myself here, there’s not many places I think that would be useful. Definitely see the utility of doing that when multiple people live together.

Once I can streamline my away/home/etc modes, then I will be able to simplify some other pistons that rely on that particular status.


#15

This is very similiar on how I’m trying to setup my household.

I basically have every person in the house with a global variable if they’re awake or not. This allows me to setup different motions settings depending on the combination.

If you have motion sensing to do a lot of your automation dependant on the variables.

But I’m questioning if I should control a global variable instead of using a virtual switch and came across this thread which I found really helpful on explaining the difference. I have a mixture of motion sensing, wall switch controls, buttons and GHA commands.


#16

I usually go with a Simulated Switch IF:

  • I want the quickest reaction time … or
  • This piston might fire quickly, back to back … or
  • I want to easily toggle it (using Alexa / button etc) … and
  • The desired response is boolean (on/off, true/false)

Alternatively, I often use global variables IF::

  • The desired response is a string … or
  • There are more than two possible outcomes

Exception to the “Rule”

A Simulated Dimmer’s “level” can technically trigger “99” different ways (1-99)… but that method is far “outside-the-box”, and to be honest, a bit tricky to code & deal with, so it’s not often used.


#17

The main reason i use global variable is because of the possibilities.
Meaning, with a virtual switch you just have ON/OFF option. For different scenarios you have to start creating more and more virtual switches.
But let’s say for one of my rooms, @mainentrace, if the variable is 10 the house acts different, if it’s 20 different etc…
but I’m no coder so maybe it’s better to use switches???


#18

With “more than two possible outcomes”, I almost always use variables


#19

what I’ve been experiencing is that I have Virtual Switches and then I have Phsyical Button does something Virtual Switch on/off.

I’m finding that I have too many nested things going on, maybe I need to simplify.
Right now: Button > Virtual Switch > Variable
Simplfy: Button > Virtual Switch (remove Variable)

but I want to take advantage when I press a button, it brings me out ot sleep mode, but when I press it again, it puts me back into sleep mode but does some actions depending on the time. Only way for this to work for me is having all these nested controls.

Most of the actions I have done are in Webcore IF statements.


#20

I’ve seen you reference your house statuses (10 vs 20) a fair amount and was curious if there’s logic behind the numbering scheme?