Loading...
 
Features / Usability

Features / Usability


mathematical calculation tracker field

posts: 228 Ukraine

Having difficulties with this one - one time worked, another no luck. Most likely my very limited knowledge of mathematical formulas . This could be a 'documentation' forum issue (for mathematically challenged people like me) as not that many examples in either this specific topic or Advanced Rating (language) page.

tiki 12.2
farming topic - trying to calculate difference (so 'subtract', but will use all eventually, i.e. -,+,/,*) for tractor engine working hours:

tracker Id24
field Id338 (numerical field), name: moto_start (sample number 500)
field Id339 (numerical field), name: moto_end (sample number 509)
field Id340 (math.calculation), name: total (total should = 9)

I got field names to work on another tracker once, but seems field id's would be more consistent, i.e.:
(sub f_339 f_338) or something like this.

I tried may other ways, most of the time gave me an error about not able to find the name of field, i.e. "moto", but also gives: Expecting "(" near ""

so also tried:
(sub (f_339)(f_338))
(sub (f_339 f_338))
(sub moto_end moto_start)
(sub (moto_end moto_start)) (sub (moto_end)(moto_start))

thanks,
Mike

posts: 228 Ukraine

apologies to all, will experiment for min. 5 days before posting (3 days is not enough).

changed two numeric field names to: 'moto start' & 'moto end'
what worked: (sub motoEnd motoStart)

documention doesn't mention anything about:
- no spacing
- capital first letter of next word in title

use of field numbers (i.e. f_338 and a brief example under the formula entry location) might be more consistent with those used in other types of tracker options and increase user friendliness for those experimenting and growing their Tiki knowledge smile

Mike


posts: 228 Ukraine

To close this subject, it was recommended to me I double check the use of the field name: it is very important in mathematical calculations to use the 'Permanent Name' of the field (Edit Field > Adanced > Permanent Name), then calculations work perfect!

- permanent names will almost always differ at least slightly,
i.e. 'Name' may = 'name',
- if the name has two words, it will almost always differ
i.e. 'First Name' may = 'firstName',
- if not English (i.e. latin alphabet) Tiki will always change the permanent name of the field name,
i.e. 'f_356' (which resolves all language issues!)


posts: 38 United States

Another thing that the Mathematical Calculation Tracker Field does not make clear is the context in which the parenthesis are supposed to occur.  For example, I tried putting them exactly as shown inside the "TRACKERMATH" plug-in (which for some reason I can't find documented anywhere) but get an error:

{TRACKERMATH(trackerId=33)} " - Unexpected "=", expected one of: "","" , ")"

 

Then after reading a bit more, it appears this is for a "FITNESS" (Fitnesse?) plug-in which is for testing purposes, which is *not* what I want.

I am also referenced to a "ratings" page to get more info on the allowed operators, but I am very confused as to what ratings have to do with calculations.  Apparently ratings use calculations too somehow, but since I am not interested in ratings or what they are, it is a bit-offputting to send me to an area I am not interested and make me scrounge around looking for what I am after.

Still haven't figure out how to get mathematical calculations to actually work.  Is there some other plug-in I'm supposed to embed them in?  It doesn't say.

For reference, I've got a template which defines the layout of the page, and then another page which references the template and which tracker it is a template for, so I don't know if that convolutes things any further.  I am trying to get a sum of various values on the page, and if it could do that both on the form entry page and when viewing the page through the template, that would be great, but if not, just when viewing the entered data through the template would be alright.

posts: 228 Ukraine

In order to create a mathematical calculation, you need to create two 'numeric' type fields + a mathematical calculation field for the mathematical calculation to work, here is an example -

Field For Math.formula  

(assumtion - tracker is already created)

Create fields:

  • Go into that tracker by chosing 'edit fields' > 'add field' > type 'numeric field' (from the drop down menu)

Permanent Name:

  • this is very important to note, as the 'permanent name' will almost always be slightly different from the name you gave the field.
  • in the case of non-English language it almost alwasy changes it to the letter 'f' + the number of the field, i.e. 'f_254'
  • in the screenshot below you can see the permament name for field #95, 'Hectares_Start' was changed to 'hectaresStart'

Permanent Name  

Mathematical Calculation:

  • finally, you create your 'mathematical formula' type field, give it a name, open up the second option, 'Options for Mathematical Calculations', and place your formula in the box 'Calculation'

Place To Add Math.formula  

  • always put it in parenthesis
  • always start with what you want to do
    • add is 'add', subtract is 'sub', divide is 'div', multiply is 'mul'
    • I'm still learning the advanced 'if', 'and', 'or', etc.
  • in my example I need to calculate how many hectares the farming combine harvested of corn (hectares is square kilometers, i.e. square miles) so I created a formula that subtracted the ending amount from the starting amount.  Like when you drive in a car and started at 46555 km  (say, for example the permanent field name is 'start') and ended at 46560 km (field name 'end'), or you drove 5km, you would need the formula to subtract 46560 km - 46550 km = 5km.  Mathematical formula would look like this: (sub end start)
  • rounding:  next problem you will discover (mostly with multiply or divide formulas) is too many numbers after the decimal place, i.e. 4.58690485.  To solve this add another set of parenthesis with two parts, the word 'round' at the beginning, and a number at the end for how many digits you want after the decimal place (two in this example):
    • i.e. (round(sub end start)2)

As you noted, all the formulas are on the Advanced Ratings page.

I don't use templates, but just Tracker List on a tiki page and enter all the parameters I want to display (fields to display, filters, sorting, etc.)

Hope this helps a little,

Mike

posts: 38 United States

Hi Mike,

Thanks for the reply!  It looks like the formula itself is going inside an option for a tracker field of type "mathematical formula", and so is part of the tracker itself, not an external value.  That is good to understand.

Apparently this has to be enabled (trackerfield_math) in the admin pages (i searched for "calcuation" and it found it under "trackers") .  This is why I wasn't finding it before.

 


posts: 228 Ukraine

Why start a new topic when you can re-use a topic you started 6 years ago biggrin

I could use some advice again, this time for calculating time between two dates -

I'm trying to calculate the difference between the current date and one in the future.  I have a Date and Time Picker field for the future date (let's call it 'futureDate').  So in the mathematical calculation field I already tried a number of variations:

Copy to clipboard
(date(sub futureDate now)) (date(sub futureDate current)) (date(sub futureDate today)) (date(sub futureDate now()))

I checked on the php page https://www.php.net/manual/en/function.date.php for these variables, but none worked.

Also, how would I format it in hours/min.? or days?

Any thoughts?

Thanks,
Mike

posts: 126886 United Kingdom

Hi Mike

I think you just need the "function" date on its own. That gives you the time now as the number of seconds since 1970, the reference you want is http://doc.tiki.org/Calculations#date not really the PHP date function.

So probably something like: (untested)

Copy to clipboard
(date (str H:i) (sub futureDate date))


The str H:i part gives you the format, and those you can look up on https://www.php.net/manual/en/datetime.format.php


posts: 228 Ukraine

Hi Jonny,

Thanks for helping with the calculation. I tried it out, unfortunately it only returns:

  • Variable not found "date"


I see the logic now, so tried a number of other variations, but than just other types of errors (due to incorrectly formulating the calculation).

I tried copying and pasting in the basic example from the docs:

Copy to clipboard
(date (str Y-m-d H:i:s))

and it worked correctly, so does find the 'date' variable in this case.

Maybe there are some additional Admin parameters I need to enable?

br,
Mike

posts: 126886 United Kingdom

Maybe more brackets would do it?

(date (str H:i) (sub futureDate (date)))

posts: 228 Ukraine
Jonny Bradley wrote:

Maybe more brackets would do it?

(date (str H:i) (sub futureDate (date)))


I did see there might be an issue with brackets, but I guess all the variations I tried were incorrect. Your version works better, does not produce any errors, and, also provides a time.

However, it does not seem to be calculating correctly. For example, if the 'future date' is this Friday, August 21st (so 3 - 4 days away, depending on time), it shows only 16 hours (and minutes, of course) or 13 hours (when I just checked again:

  • future date: 21-08-2020 08:00
  • calculation: 13:50

I thought maybe it was calculating only up to 24 hours in advance (ignoring the date) but even the time does not add up. I would assume Tiki would be using the system time zone so most likely no issues there? But in any case, it is not calculating correctly as it should be something like 80 hours at least.

Just to test, I tried a 'past' future date:

  • today is August 17
  • future date entered: 15-08-2020 00:00
  • calculation: 05:32

Some how I just can't seem to draw any conclusions as to where it is getting the calculated time.

Also to test I tried adding two more mathematical calculation fields:

1)
Copy to clipboard
(date (str Y-m-d H:i:s))
so it would just list the current date/time 2)
Copy to clipboard
(date (str H:i)(sub futuredate CurrentDateTime))

this one shows zero / null with the 'back' date.

  • current time: 2020-08-17 21:39:44
  • calculation: 11:00

here the only thing I see is that it is just simply listing the 'time' part of the 'date/time' 'future time' (21-08-2020 11:00)

Maybe there is a bug with time/date calculations? (I'm testing on Tiki 18.7)
thanks,
Mike

posts: 228 Ukraine

I decided to try a different approach - using 'seconds' of UNIX time.

Copy to clipboard
(round(div(div(date(sub futuredate (date)))60)60)1)

In the Tracker it displays correctly (in hours)
In PluginLIST it's best not to use the 'date/time' formatting but a simple 'number':

Copy to clipboard
{FORMAT(name="Time to Date")}{display name="tracker_field_futuredate" default=" " format=number }{FORMAT}


This is for a 'simple' use case, as it will also calculate hours in reverse (which, may or may not be useful).


posts: 228 Ukraine

Hi Bernard,

I agree on an updated name for the page, maybe something like 'Calculations' wink biggrin would work?

br,
Mike