Continuously updating values with radio buttons

Hey guys,

I'm trying to make a display that will take a counter from a SQLtag that is in seconds and convert and display it in either seconds, minutes or hours depending on which radio button is selected. I can get it to work by writing an event script on each radio button but the problem is it only displays once and will not update unless the radio button is reselected. I would like it to continuously update the value. I tried putting a while statement in there and that locked up the client as soon as the radio button was selected. I even tried putting a delay in there so it looks like this:

while event.source.selected:
event.source.parent.getComponent(‘Numeric Label 2’).value = event.source.parent.getComponent(‘Numeric Label’).value
import time
time.sleep(1)

But that still locks up the client and I have to end the task. I’m probably going about this all wrong but is there a way to constantly update a value off of an event like selecting a radio button? Thanks!

Yes, this is definitely not the way to go about this. Not your fault, but something we’re always trying to combat: the urge to script everything. Naturally it comes from using other packages, scripting a lot day to day, etc.

ANYHOW, using expressions and bindings in the way to go. Bindings are magical, in that they’re all linked up and get re-evaluated any time any reference in them changes. That way, you can build complex hierarchies and have everything just be “up to date”.

The best designs in Ignition use binding and dynamic properties a lot. In this case though, we’ll strip it down to the easiest way to accomplish this. I’m assuming three radio buttons, “Sec”, “Min”, “Hour” and a SQLTag called “Time”. With those, all you need to do is create a label and bind it’s text to an expression like:

if({Root Container.Sec.selected}, concat({Time}," sec"), if({Root Container.Min.selected}, concat({Time}/60," min"), concat({Time}/(60*60)," hours")))

There are many ways this expression could be written, and you can embelish it with formatting, rounding, etc., but I just want to show to main methodology.

The point here, is that each time “Time” changes, or any of the radio boxes’ “selected” properties, it will be re-evaluated and the label will be updated.

Hope this helps,

I did what you suggested and that worked great. The problem is I have no idea WHY it worked great. I guess I’m not used to this program format yet. Could you go through each statement in the code you included below and explain what it’s doing? That will help me understand what exactly you did there, no rush just curious so I don’t have to bother you next time :wink: Thanks for the help!

Here you bound the label’s text property to an expression. The expression follows Ignition’s expression language where you can use various operators, functions, properties and tags. You can find more information on the language here:

inductiveautomation.com/supp … inding.htmif({Root Container.Sec.selected}, concat({Time}," sec"), if({Root Container.Min.selected}, concat({Time}/60," min"), concat({Time}/(60*60)," hours")))This expression has an if statement inside of another if statement. An if statement checks some condition and has a true return and a false returnif(condition, trueReturn, falseReturn)The first if in the expression checks to see if the second radion button is selected by referencing the property. If so the expression returnsconcat({Time}, " sec")Otherwise it returns the false statement which happens to be another if statement in this case. The second if statement checks to see if the minute radio button is selected. If so it returnsconcat({Time}/60," min")Otherwise it returns the hoursconcat({Time}/(60*60)," hours")Only one radio button can be selected so if the hours radio button is selected it must validate against two if statements. If the second radio button is selected it will stop after the first if statement.

Since you are referencing properties or tags in the expression, if the tag or property changes the expression evaluates again. This is why it continues to update. Your previous script only occurred when you changed the radio button. Hope this helps.

We’re in the process of making new videos, but there are some old ones from FactoryPMI that still apply. The series on “Properties, Bindings and Expressions” might be useful for you to watch: FactoryPMI - PBE.

Regards,

Ahhh, I see now. I was thinking the two if statements were independent of one another. I’m used to Cicode and Opto22 programming where the syntax is much different but I think I’m starting to catch on. Thanks for taking the time to explain, much appreciated!

Expressions are a very cool thing! The big difference is that a script runs code once based on something (button click, timer, window opening, etc), whereas an expression “listens” to all of it’s dependents (inputs) and automatically updates whenever any of them change. The expression doesn’t have separate statements - the whole thing evaluates down to a single value.

As a rule of thumb, I would use expressions over script whenever you can. That gives you the ability to bind any properties to just about anything (Objects on the screen, PLC values via SQLTags, SQL database values, etc, etc)