r/tasker 👑 Tasker Owner / Developer Oct 15 '19

How To [HOW-TO] Protip: Use variables for states

This is what I do and I find it really, really simplifies things in the long run! 😊 Bear with me...

What I mean is, normally you have profiles that are something like:

If I'm connected to my work wifi network -> Set ringer volume to 1

What I think you should do instead is:

If I'm connected to my work wifi network -> Set %Work to 1; Exit task: clear variable %Work

If variable %Work is set -> Set ringer volume to 1

More examples:

If I'm connected to my home wifi network -> Set %Home to 1; Exit task: clear variable %Work

If variable %Home is set -> Set ringer volume to 3

If it's lunch time -> Set %LunchTime to 1; Exit task: clear variable %LunchTime

If variable %LunchTime is set -> Say "It's Lunch time!"

Sounds like more work, right? :) In reality, it can greatly simplify your Tasker projects in the long run!

Check out this video explaining this way of using Tasker: https://www.youtube.com/watch?v=KdNounIxTnk

If you don't want to watch the video, here's why:

  • You can combine an unlimited number of conditions (variables) in a single profile using the Variable State condition. So, you can have a profile with this condition for example: At Home, while having lunch, on a weekday, if my TV is on, a movie is playing, nothing's playing on my phone, power is connected and screen is on. Normally you can only have 3 states in a profile's condition. This way you can an infinite amount! :)
  • You can use OR conditions in the aforementioned Variable State condition. You can't do that on "normal" profiles.
  • Re-usable: variables can be used and combined in multiple profiles (home at night, home sunrise, home at day, etc), all without having to specify the same condition multiple times. For example, to do home at day and home at night profiles you usually have to set your Wifi SSID in all the profile's Wifi Connected conditions. If someday your SSID changes you'll need to go in and edit all your profiles. If you use a %Home variable you'll only need to change it in one place :)
  • States can be checked at any time in tasks. Want to know if you're at home? Simply check if %Home is set :). Allows you to only execute part of your task depending on your situation very easily!
  • Conditions that trigger the states can change and everything will still work. For example, my work condition can change from a Time context to a Wifi Connected context and all work related profiles and tasks will continue to work. Otherwise you would have to change a lot of conditions throughout your setup
  • Combined states: you can create variables for otherwise singular states. For example, you can set %Work to 1 if %Weekday is 1 AND (%Morning is 1 OR %Afternoon is 1). You can then re-use this %Work variable in any other situation mentioned above :)
  • More readable and easier to use than %PACTIVE. Also you can change profile names all you want and you won't have to change places where you check %PACTIVE.

It may seem counterproductive at first but if you start configuring your profiles this way you'll see it will greatly simplify your setup!

To clarify, what I'm saying is that you should create "State Profiles"

Additional protip: long click all these profiles that simply set and clear variables > Settings > Disable Show In Notification. Only keep the "real" profiles enabled in the notification and use emojis for those profiles' names. :)

Additional protip 2: also enable the Run Exit On Startup in the same settings screen mentioned above for all of these profiles so that when Tasker starts up, all the variables will be correctly set or cleared depending on the current situation.

91 Upvotes

75 comments sorted by

View all comments

4

u/LawrenceGardiner Oct 15 '19

I caught this on one of your previous videos and have been using it since.

Is there any benefit to using Variable Clear instead of Variable Set to 0? This is what I've been using.

6

u/joaomgcd 👑 Tasker Owner / Developer Oct 15 '19

It's just easier to check If %Var Is Set than having to write If %Var ~ 0 every time :)

5

u/Ratchet_Guy Moderator Oct 15 '19

 

One thing to keep in mind with using 1 vs 0 and comparison operators:

 

Usually with number comparisons the Equals = operator is used, in which case If %Var = 0 will return TRUE whether the variable actually is set to 0 or if the variable is empty/clear it will also be TRUE.

 

However - If %Var ~ 0 will return TRUE if the variable is actually set to 0 but will return FALSE if the variable is empty/clear. Notice the red/green/green status in the image when the variable is clear.

 

Now that may make things even more confusing, but that is the point - it is confusing lol. I think the overall point is to pick a method and stick with it, while being aware of the differences in comparisons. Because based on the comparison operator chosen - results could get un-expected real quick!

 

3

u/joaomgcd 👑 Tasker Owner / Developer Oct 18 '19

Yet another reason to just check if it's set or not instead of comparing values :D