Where to use the script in label

perspective
I have created a new script to calculate the remaining time on duty, which runs normally. Then, I want to display the remaining time calculated by the script on the Label. However, I don't know where to call the script I wrote in the properties of the Label. Please give me some advice


Please post code, not screengrabs of code and especially not photographs of screens showing code. It means that we can copy and paste them to test or to edit in our answers. Post a screengrab as well if it helps give context to the code.

There isn't a direct script binding so the easiest way to do what you want is to use an expression binding to any nonsense value and then do a script transform.
return TimeRemaining.calculate_remaining_time()

Setup would be like this:

(My function is just returning system.data.now(). Yours will be returning the formatted time remaining.)

2 Likes

wow
thank you very much,it worked

Instead of a null expression + script transform, you could use runScript('TimeRemaining.calculate_remaning_time').
I'm not a fan of runScript, but to call a library function it does the job just fine.

2 Likes

class Shift:
def init(self, start_hour, end_hour):
self.start_hour = start_hour
self.end_hour = end_hour

shifts = [Shift(8, 20), Shift(20, 8)]
current_shift = 0 # 初始班次为第一个班次(早班)

def calculate_remaining_time():
global current_shift
now = datetime.datetime.now()
start_time = datetime.datetime(now.year, now.month, now.day, shifts[current_shift].start_hour, 0)
end_time = datetime.datetime(now.year, now.month, now.day, shifts[current_shift].end_hour, 0)
if now > end_time:
# 切换到下一个班次
current_shift = (current_shift + 1) % len(shifts)
start_time = datetime.datetime(now.year, now.month, now.day, shifts[current_shift].start_hour, 0)
remaining_minutes = (end_time - now).total_seconds() // 60
return remaining_minutes

print(calculate_remaining_time())Preformatted text

But there is another question that I need to consult with you. Sometimes, he doesn't update these calculated values himself. After I switch screens, he updates them himself

where should i use the runScript('TimeRemaining.calculate_remaning_time')

Your code is not formatted. There are no indents. Use the preview window to check your post before posting it.

Use now() with a millisecond value to retrigger the expression. As shown above the expression will recalculate every 5000 ms.

thanks very much

Or... use runScript. It takes a poll rate parameter to specify how often it should execute.
If you don't know where to use it, you should watch the inductive university videos and read the doc.
But I'll tell you anyway: It's an expression function, so... you can put it anywhere you'd put an expression.
in this case, you'd put it where @Transistor put the now(5000).

So, you could replace now(5000) and the transform with just runScript("TimeRemaining.calculate_remaning_time", 5000)

5 Likes

Concur. Substantially less overhead than a script transform. See the timing comments in my Integration Toolkit topic.

3 Likes

Just chiming in to say it's generally bad practice to use datetime you are much better off exclusively system.date.*functions and the java date objects it returns.

5 Likes

Since you only have two 12-hour shifts starting at 08:00 and 20:00 you could use an expression binding.

12 * 60						// 12 hours in minutes
- (
	(
		toMillis(now(1000)) // time now
		- 8 * 3600 * 1000	// 08:00 offset
	) / 1000 / 60			// convert to minutes since midnight
) % (12 * 60)				// modulo 12 hours in minutes gives minutes into shift
// Subtracting the minutes into shift from the length of the 
// shift gives the minutes remaining.

This could be more efficient than running the script.

1 Like

HI
If this script has its own parameters, how to use Runscript (). Because with "", the parameters inside are also abnormal

If this script has its own parameters, how to use Runscript (). Because with "", the parameters inside are also abnormal

It's in the doc:
https://docs.inductiveautomation.com/display/DOC81/runScript

image

Here are 2 links that you should bookmark:
https://docs.inductiveautomation.com/display/DOC81/Expression+Functions
https://docs.inductiveautomation.com/display/DOC81/System+Functions

I don't understand what you are saying here. Has @pascal.fragnoud's answer solved your problem?

Can you please go back to post #5 and fix the code formatting as explained in post #9. Edit the post, select the code and press the </> button. That will fix the indentation and apply syntax highlighting.

import time
import numpy as np


def calculate_time(end_time_second, star_time_second):
    minute = (end_time_second - star_time_second) / 60
    minute = np.round(minute, 2)
    return f'{minute}分钟'


class stop_time:
    def __init__(self, stop_sign_prior):
        self.stop_sign_prior = stop_sign_prior
        self.star_time = time.time()
        self.end_time = time.time()

    def downtime(self, stop_sign):
        output_time = 0
        t = time.localtime()
        if ((t.tm_hour == 8) or (t.tm_hour == 20)) and (t.tm_min == 0) and (t.tm_sec == 0):
            if stop_sign == 0:
                self.end_time = time.time()
                output_time = calculate_time(self.end_time, self.star_time)
                print(f'停机时间为:{output_time}')

            self.star_time = self.end_time = time.time()
            self.stop_sign_prior = stop_sign

        if self.stop_sign_prior == 0 and stop_sign == 1:
            self.end_time = time.time()
            output_time = calculate_time(self.end_time, self.star_time)
            print(f'停机时间为:{output_time}')

        if self.stop_sign_prior == 1 and stop_sign == 0:
            self.star_time = time.time()

        self.stop_sign_prior = stop_sign

        return output_time