AndroidApps

User creates great state switching system in Tasker

tasker logo - for some reason we don't have an alt tag hereMany people who use Tasker end up creating different states with profiles that are active under certain circumstances, like at home, at work, in the car, etc. Making those profiles work together can be a pain, especially if they have the ability to overlap. I’ve talked a bit about this in my beginner’s guide to Tasker, and I myself use a system of such states which required a bit of tinkering to make work. 

A user from the Tasker Google Group forum has come up with a rather brilliant system for handling states. It’s based around a system of current, previous, and next state variables, where it’s possible to trigger profiles based on a series of state changes instead of just individual changes. This allows you to for instance separate between cases where you’re in your car on the way to work and when you’re in your car on the way back from work, simply by creating scenarios that require a certain current state/previous state combination (current: in car + last: home, for instance).

The way this works is that you create profiles for different states, like being in the car – which could be Bluetooth connected. This then sets a variable like %%NEWSTATE to “Car”. You then have a profile that monitors any change in %%NEWSTATE, and runs through its own task. The creator’s version of this task currently looks like this:

  • Stop if %STATELOCK is set (allow tasks to inhibit transitions temporarily)
  • Stop if %NEWSTATE ~ %STATE (non-transitions are no-ops)
  • Set %LSTIME To %TIMES – %TSTART (record the time spent in the previous state)
  • If %NEWSTATE ~ %LASTSTATE -> If %LSTIME < 3 -> stop (detect thrashing between states too quickly)
  • Set %LASTSTATE to %STATE
  • Set %STATE to %NEWSTATE
  • Log %TIME, %LASTSTATE, %STATE, %LSTIME to the state change file
  • Clear %NEWSTATE
  • Set %TSTART to %TIMES

This task includes a bunch of checks that are part of why this system is so powerful. There’s a state lock system that simply stops any state changes if enabled (using a variable). It also stops it if there’s no actual change in the state, which could happen if you reboot your phone, turn Tasker on and off, or simply leave and then reenter the state without another one being active in between.

There’s also a system to check if a state changes too quickly, which is possible because the task records the time that states changed. This is also logged to a file.

Like I said, this is the creator’s version of the state change task, but it shows what’s possible using this system. It adds a layer of intelligence to the whole thing, a layer you don’t have with less sophisticated state change methods (like the one I use myself).

The idea here is that, assuming that it’s a legit state change, %NEWSTATE is transferred into %STATE, and the current %STATE is moved to %LASTSTATE. %STATE can then be used to trigger the profiles that contain the actual settings and actions for each situation, either in itself or combined with %LASTSTATE or other contexts.

It’s really a brilliant system, and one I might have to start using myself. I have to think about whether there are any situations where this would be beneficial, which I’m sure there are. The creator did an awesome job coming up with this, and it’s so brilliant I wish I thought of it myself.

Pocketables does not accept targeted advertising, phony guest posts, paid reviews, etc. Help us keep this way with support on Patreon!
Become a patron at Patreon!

Andreas Ødegård

Andreas Ødegård is more interested in aftermarket (and user created) software and hardware than chasing the latest gadgets. His day job as a teacher keeps him interested in education tech and takes up most of his time.

Avatar of Andreas Ødegård

12 thoughts on “User creates great state switching system in Tasker

  • Avatar of D. Stroya

    Ooh, this seems complicated but may be exactly what I need to stop waking up to notification at night when my profiles conflict after not following my typical pattern. I see a new weekend project in the future. Thanks for the heads up.

    Reply
  • Avatar of Tuang D.

    I tried it, it’s working great!

    Reply
  • Avatar of James D

    How would one go about implementing this method?

    Reply
    • Avatar of Andreas Ødegård

      This is definitely a more advanced method, so it does require that you know how to create it from just seeing what’s in this article. It would have to be built directly into your own system, which is different from anyone else’s, so a “plug and play” method simply isn’t an option.

      Reply
      • No doubt. I understand the concept. But does that mean for every task that I use for state changing profiles I will need to create the same 9 tasks to initiate the State Change.

        I just hoped that I could easy reuse a task. Especially as copy and paste in tasker is diabolical.

        Reply
        • Avatar of Andreas Ødegård

          As explained in the article, the nine actions listed would run on its own upon detecting a change in the variable

          Reply
  • I’ve attempted to implement this and it works, mostly. But after the three minute conditional (I think) at the same location/profile it seems to %STATE send to get overwritten with the literal string “%NEWSTATE”.

    I have several profiles which launch a corresponding Set State task which then set %NEWSTATE based on location and then use the Perform Task action to run a Manage States task which contains the logic outlined in the article.

    Any suggestions on where I went wrong?

    Thanks, great article, great site.

    Reply
    • I should add… I know this because I’m displaying %STATE in a Minimalistic Text widget.

      Reply
    • Avatar of Andreas Ødegård

      no clue. i dont even use this system myself

      Reply
  • I finally managed to build this system into my own Tasker… and it’s brilliant !!
    Things never clash…
    I have one state called ‘Outside’. Whenever I remove the statelock… Or I should say, whever a state locked profile exits, the phone autmatically switches to Outside and the corresponding conditions.
    Whenever I get near a new set of state conditions ‘Outside’ gets set to ‘%LASTSTATE’… the %NEWSTATE gets passed on to %STATE and the %STATELOCK is engaged.

    Brilliant!

    Max. K,… the steps above is just the logic behind it all. It isn’t the entire profile.
    It took me like two weeks of fiddling in bed (WITH MY PHONE!!) before I started to realize how it was supposed to function.

    Reply
    • Avatar of Andreas Ødegård

      Indeed, it’s just the logic.

      Reply
      • OW and Max… Just as a tip….

        All my “Home” state does on the enter task is set %NEWSTATE to Home.

        On exit it clears %STATELOCK

        The rest ia automated magic.

        Andreas, I want to thank you for your unique way of teaching us stuff. It’s so much cooler to achieve a eureka moment after wasting blood sweat and tears on a profile than to get a step by step explanation.

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *