Home Tasker Maths in Tasker

Maths in Tasker

Make integers great again!

One of the Tasker functions that aren’t often talked about is the ability to do Maths in Tasker. ‘Do Maths‘ box in Variable Set action turns Tasker into a little Einstein or Alan Turing (because I hang around Manchester too much). This write-up aims to shed some light, and go beyond the basic mathematics, should you ever need it.

Maths in Tasker

Apart from basic operators, the list of mathematical expressions is rather impressive and it’s unlikely you will ever need all of them. To make an operation simply use Variable Set action and select the ‘do Maths’. Now you can write the equation of your choice.

Let’s try something simple:

2+2*2

The Internet believes that solving this little task makes you a genius. I’m pretty sure I know how we all feel about all these people bragging about their “maths skills”. I’m using it to illustrate that Tasker will obey mathematical rules when writing your calculations. To get an 8 you have to do this instead:

(2+2)*2

Maths in Tasker allows you to use more complicated algebra and substitute the values with variables as well!

(%two*(%two+%two))*(%two-%one)

This time we get the 8. Let’s get to my favourite part…

Ceil, floor, nextup… making the integers great again!

The weather forecast in my Weather Notification Project comes as a floating-point number (number with a decimal space). This level of precision is not that important.  I doubt I would feel the difference between 15°C and 15.87633°C. Here is how to get rid of the decimal quickly:

Variable Set %floor: floor(%temp)
Variable Set %ceil: ceil(%temp)
Variable Set %nextup: nextup(%temp)

As you can see that was quick and painless.

  • ceil() rounds up
  • floor() does the opposite
  • nextup() gets rid of the long decimal number

But this is a single operation only. What if I have an entire array of data to process? Regardless of the calculation, you want to apply – you can do it with a FOR loop:

%Array(16.25,15.75,19.31)

FOR Loop
 A1: For [ Variable:%domaths Items:%Array() ] 
 A2: Variable Set [ Name:%result To:ceil(%domaths) 
     Recurse Variables:Off Do Maths:On Append:Off ] 
 A3: Array Push [ Variable Array:%new_array 
     Position:3 Value:%result Fill Spaces:On ] 
 A6: End For
 A7: Flash: %new_array

Each value from the %Array is iterated and the calculation (A2) is performed. The %result is assigned back to the %new_array in the same order.  It’s worth noting that you either have to ‘push‘ the values from the ‘bottom‘ of the array or use action Array Process – Reverse to restore the original order.

Other most used functions

Maths in Tasker has more functions, but I’m only going to cover most used by me. Let’s try modulus next. If you are new to programming it might be the first time you come across this:

Variable set: 16/7
Variable set: 16%7

Unlike divide, modulus gives your the reminder from the divide operation. You can ‘fit’ two sevens inside 16 and you get the reminder:2 It’s great to check if a number is even or odd (divide by 2 – if % = 0 it is an even number).

Absolute value gives you a positive value of the integer:

Variable Set: abs(-10)
Variable Set: abs(10)

Great for all the pesky situations where the value given is below 0.

More Maths in Tasker

A few more operations are available in Tasker. Variable Convert does number conversions for you. You can:

  • convert units,
  • time,
  • characters
  • deal with HTML tags
  • more…

Take a look at this list:

AutoTools Maths

The plugin AutoTools also contains action for mathematical calculations. These can be calculated in vanilla Tasker using more complicated equations but if you feel lazy feel free to use the plugin to:

  • calculate sum
  • calculate an average
  • find min/max number

AutoTools WebScreens Calculations

A new update to AutoTools introduced calculations to Web Screens. The option allows to assign multiple fields for inputs and outputs and perform a mathematical function on values. It’s a very fast and convenient way to create calculated fields and process the multiple fields at once (arrays).

Conclusion

I hope this opens up some new possibilities for you. Maths in Tasker is worth knowing, as it will save you from writing multiple actions to achieve the same result. It’s good to know the basics as it will surely make your life easier. If you have any other interesting maths tricks in Tasker, let me know in this Reddit thread.

PayPal

Nothing says "Thank you" better than keeping my coffee jar topped up!

Patreon

Support me on Patreon and get an early access to tutorial files and videos.

image/svg+xml

Bitcoin (BTC)

Use this QR to keep me caffeinated with BTC: 1FwFqqh71mUTENcRe9q4s9AWFgoc8BA9ZU

New to Tasker?

Tasker Quick Start – Getting started with Tasker

0
From newb to not so newbie in 10 min

Best Tasker Projects

How to use Raspberry PI as WOL (wake on lan) server

0
While you could wake up your PC from a mobile directly, having a dedicated server capable of doing so is the best solution. The reason is simple. You can hook up as many devices as you wish with a single endpoint. This is why Raspberry Pi is perfect for this.

7 awesome Bluetooth keyboard shortcuts for Android

0
7 unique Android shortcuts that you can add to any Bluetooth keyboard.

Smart overnight charging with Tasker

0
Still keeping your phone plugged in overnight? Try smarter overnight charging with this profile

Charge Test – Tasker charger performance tool

0
Turning Tasker into productive tool for monitoring chargers

One thing that Join app can’t do and how to fix it with Tasker

0
It's not possible to share the clipboard automatically between join accounts registered to 2 different emails. But you can fix this with tasker.

Essential Guides

Tasker: Seconds into DD:HH:MM:SS (dynamic)

0
It's time to.... ok it's a pun, but I will show you how to master time and convert seconds to DD:HH:MM:SS dynamically

4 ways to organise Tasker projects

0
Keep your Tasker tidy!

A better way to store Tasker credentials

0
The more clever way of managing credentials

Annoyed with dozens of AutoApps populating your app drawer? Here is a fix!

0
Clear your app drawer from the clutter in seconds

Putting AutoTools pie chart to a good use – SSID logger

0
Who wants a piece of the pie (chart)?