Auto-calculation of multiple observations

The documentation gives us an example of auto-calculation of BMI:

What if there are multiple observation values, not related to BMI, to be computed, e.g., a risk score?

Will all logic have to reside in the BahmniObsValueCalculator.groovy file?

Can there be multiple Groovy scripts in the obscalculator folder, each calculating its own observations?


Hi @dsurrao,

The calculation has to be done in the same BahmniObsValueCalculator. If you look at the Bahmni-Core code, BahmniObsValueCalculator is specifically loaded using its name. Hence this restriction.

The way we do it right now is to have multiple CalculateXXX() Methods and call them. Please have a look at the example.

Thanks @swathivarkala!

How do you debug Groovy scripts, e.g., write debugging statements to a log file?

I found a way: write debug statements to a File object in your OpenMRS application data directory, be sure to make the file writable. E.g.,

def file1 = new File(OpenmrsUtil.getApplicationDataDirectory() + "obscalculator/groovy_log.txt")
file1.write 'Debugging statement...'

If there is a better method, please let me know.


1 Like

I have always done it by copying the groovy file to bahmni-core omod and debugging it. But it’s been long so i don’t remember exact steps.

Hi @dsurrao and @swathivarkala

I realy need you help on auto-calculate from two date time.

i want to auto-calculate total surgical time spend from surgical time in and surgical time out and these fields are datetime. I tried to follow the same auto-calculate done in bahmni for BMI but i don’t know why it doesn’t show the auto-calculated value. i also tried to use this approach for debugging Debug groovy file but it doesn’t debug.

see my groovy file code here: my groovy file, Kindly help

Thank you.


Hi Mupagasi,

  1. Do you have the Concept ‘Surgical Total Time’ created under the same parent of Surgical Time In/Out?
  2. Looking at the groovy, i see that you are just setting value, but when you are saving Surgical Time In/Out observations for the first time, there won’t be any Surgical Total Time obs right. Hence you need to create an observation using ‘createObs’ method for it and set the value and set it as one of the children of your parent concept.

Hi @swathivarkala, Yes this concept is under same parent as surgical time out/in here is the screenshot.

You are right this total time obs should be created after saving surgical time in/out, let me try to create this and set the value as you suggested.

Thank you,


Hello @swathivarkala,

I have followed your advice but it seems there is something i’m messing up, i have created the obs using createObs as you suggested and set a static value to the surgical total time fields. But that static value is not showing up when i save the form data, even the debug code don’t write in the file. it seems this file is not executing my code.

Here is a design of the whole form

Here is Surgical Time In concept

Here is Surgical Time out concept

Here is Surgical Total Time concept

Here is my groovy file

Thank you


Hi @mupagasi, if you’re looking to display surgical total time on the form itself, I’m not sure if Bahmni supports that. You would need to display it on the patient dashboard.

I had asked a similar question: Is there a way to dynamically update an observation form field based on a change in another?


1 Like

Hi @Dominic,

Thanks for the information but for me it 's inevitably to have this kind of auto-calculated fields, can you guide me how i can display this auto-calculated field on patient dashboard, or my be share your configurations if you don’t mind.

Thank again,

Regards, Jean Paul

Hi @mupagasi, this document outlines how to do it:

@dsurrao thank you, i appreciate you help