Expression tag to convert minutes to hh:mm:ss

Could someone help me with using and expression tag to convert something like:

91.50 minutes into 01:31:30

Is it even possible??

Not pretty, but it seems to work:

numberFormat(floor({GenericSim/Writeable/WriteableFloat1} / 60),"#0")+":"+numberFormat(60*(({GenericSim/Writeable/WriteableFloat1} / 60)-floor({GenericSim/Writeable/WriteableFloat1} / 60)),“00”)+":" + numberFormat(60*({GenericSim/Writeable/WriteableFloat1} - floor({GenericSim/Writeable/WriteableFloat1})),“00”)

Let me know if you want the parts broken down and explained.

I have a script for it:

def convert(type, input): import datetime if type=='min': return str(datetime.timedelta(minutes=input)) if type=='sec': return str(datetime.timedelta(seconds=input))
You can either import the attached file into app, or shared scripts if you have 7.7.

Your expression tag becomes

[code]runscript(app.hms.convert(‘min’, value_to_convert))

-OR-

runscript(shared.hms.convert(‘min’, value_to_convert))[/code]

It can also convert seconds:

[code]runscript(app.hms.convert(‘min’, value_to_convert))

-OR-

runscript(shared.hms.convert(‘min’, value_to_convert))[/code]
hms.py (181 Bytes)

Thanks Kathy and Jordan, I will give both of them a try.

Hi,
I tested and worked fine this one: runscript(“shared.hms.convert(‘sec’, 120)”)

BUT when I try to run using a property value like this:
runscript(“shared.hms.convert(‘sec’, {Pozo_ICH_v3.TiempoOFF.intValue})”)
I got the following error message:

Exception: Error executing expression binding on
Pozo_ICH_v3.Pozo_ICH_v3.Label 24.text
caused by ExpressionException: Error parsing script for runScript() expression: SyntaxError: (“mismatched input ‘}’ expecting COLON”, (‘expression:runScript’, 1, 68, “__RESULT = shared.hms.convert(‘sec’, {Pozo_ICH_v3.TiempoOFF.intValue})\n”))

caused by SyntaxError: ("mismatched input '}' expecting COLON", ('<expression:runScript>', 1, 68, "__RESULT = shared.hms.convert('sec', {Pozo_ICH_v3.TiempoOFF.intValue})\n"))

Ignition v7.9.9 (b2018081621)
Java: Oracle Corporation 1.8.0_144

{Pozo_ICH_v3.TiempoOFF.intValue} ends up as a literal string. You’ll need to concatenate the value into it. Try:

runscript(concat("shared.hms.convert('sec', ", toString({Pozo_ICH_v3.TiempoOFF.intValue}), ")"))

Thank you… works perfect!

1 Like

You can also use the newer runScript syntax to avoid the tricky/brittle concatentation:
runScript("shared.hms.convert", 0, "sec", {Pozo_ICH_v3.TiempoOFF.intValue})

4 Likes

Better idea… thanks!

IMO, it can just as easily be done without script.

  • start from a date (midnight)
  • add minutes or seconds like you want (needs to be integers, if you want high precision, you can add milliseconds as integers)
  • format as “HH:mm:ss”

This doesn’t work if the hours value can reach more than 24 hours, but apart from that, it’s quite concise and easy to understand IMO.

dateFormat(
	addSeconds(
		getDate(1970,1,1),
		toInt({Root Container.value} * 60)
	),
	"HH:mm:ss"
)
5 Likes

When I wrote it four years ago, not all of those expression functions existed. :slight_smile:

2 Likes

We just assumed you could see into the future. :wink:

1 Like