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.