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 in style with crypto-currency

New to Tasker?

Tasker Quick Start – Getting started with Tasker

0
From newb to not so newbie in 10 min

Best Tasker Projects

Tasker PC control – #Tutorial 3 (Apps menu)

0
Launch apps and websites from the mobile!

Tasker PC control – #Tutorial 2 (Volume Menu)

0
If you missed the part one, I strongly recommend you to check it out. It tells you all about the framework used, and you...

How to control your PC like a boss – Tasker Project

0
Control PC, and other devices from a custom notification system.

Bluetooth battery monitor – monitor any Bluetooth battery with Tasker

0
Measure the battery use of Bluetooth devices

The Minimal & Elegant giveaway WINNERS

0
Picking the winners live! Livestream and making a Tasker Winners project!

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)?