# Where to use the script in label

**URL:** <https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [November 21, 2023, 10:18am UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182 "2023-11-21T10:18:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![chen\_zhe](https://avatars.discourse-cdn.com/v4/letter/c/c0e974/32.png) [@chen\_zhe](https://forum.inductiveautomation.com/u/chen_zhe)\
**Post date:** [November 21, 2023, 10:18am UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/1 "2023-11-21T10:18:19Z")

</div>

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

 ![1](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/b/1b5dbfc095ae0d5f9ad2b367216caee0199cb3d7.jpeg)  
 ![2](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/0/7025bc4d2389d005198d7a5f5acdae0ca31890bc.jpeg)

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 21, 2023, 10:35am UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/2 "2023-11-21T10:35:02Z")

</div>

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.

> [@Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831):
>
> This thread is intended as a short help file for all those struggling to post formatted code on the forum. Posting formatted code on the forum (and most others) is as simple as selecting the code and pressing the \</\> button. It works the same as applying Bold or Italic styles to your document. It preserves indentation and applies syntax highlighting. [How to post code]

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 21, 2023, 10:43am UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/3 "2023-11-21T10:43:27Z")

</div>

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:

 ![Scriptransform on null](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/a/faf0578f9e308859801c7ae3ae33953ac7630f65.png)

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

---

<div class="post-metadata">

**Author:** ![chen\_zhe](https://avatars.discourse-cdn.com/v4/letter/c/c0e974/32.png) [@chen\_zhe](https://forum.inductiveautomation.com/u/chen_zhe)\
**Post date:** [November 21, 2023, 10:53am UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/4 "2023-11-21T10:53:37Z")

</div>

wow  
thank you very much,it worked

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [November 21, 2023, 11:11am UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/5 "2023-11-21T11:11:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chen\_zhe](https://avatars.discourse-cdn.com/v4/letter/c/c0e974/32.png) [@chen\_zhe](https://forum.inductiveautomation.com/u/chen_zhe)\
**Post date:** [November 21, 2023, 11:14am UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/6 "2023-11-21T11:14:13Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![chen\_zhe](https://avatars.discourse-cdn.com/v4/letter/c/c0e974/32.png) [@chen\_zhe](https://forum.inductiveautomation.com/u/chen_zhe)\
**Post date:** [November 21, 2023, 11:16am UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/7 "2023-11-21T11:16:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chen\_zhe](https://avatars.discourse-cdn.com/v4/letter/c/c0e974/32.png) [@chen\_zhe](https://forum.inductiveautomation.com/u/chen_zhe)\
**Post date:** [November 21, 2023, 11:23am UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/8 "2023-11-21T11:23:51Z")

</div>

> [@pascal.fragnoud](#):
>
> runScript('TimeRemaining.calculate\_remaning\_time')

where should i use the runScript('TimeRemaining.calculate\_remaning\_time')

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 21, 2023, 11:27am UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/9 "2023-11-21T11:27:10Z")

</div>

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

> [@Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831):
>
> This thread is intended as a short help file for all those struggling to post formatted code on the forum. Posting formatted code on the forum (and most others) is as simple as selecting the code and pressing the \</\> button. It works the same as applying Bold or Italic styles to your document. It preserves indentation and applies syntax highlighting. [How to post code]

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 21, 2023, 11:32am UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/10 "2023-11-21T11:32:40Z")

</div>

> [@chen\_zhe](#):
>
> Sometimes, he doesn't update these calculated values himself.

 ![Scriptransform on null 2](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/6/26f784b849a4d7c225156fe8140b6c3ac161ef9b.png)

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

---

<div class="post-metadata">

**Author:** ![chen\_zhe](https://avatars.discourse-cdn.com/v4/letter/c/c0e974/32.png) [@chen\_zhe](https://forum.inductiveautomation.com/u/chen_zhe)\
**Post date:** [November 21, 2023, 11:46am UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/11 "2023-11-21T11:46:28Z")

</div>

thanks very much

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [November 21, 2023, 1:05pm UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/12 "2023-11-21T13:05:50Z")

</div>

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)`

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 21, 2023, 1:07pm UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/13 "2023-11-21T13:07:57Z")

</div>

> [@pascal.fragnoud](#):
>
> use `runScript`.

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

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [November 21, 2023, 1:09pm UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/14 "2023-11-21T13:09:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 21, 2023, 2:45pm UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/15 "2023-11-21T14:45:41Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![chen\_zhe](https://avatars.discourse-cdn.com/v4/letter/c/c0e974/32.png) [@chen\_zhe](https://forum.inductiveautomation.com/u/chen_zhe)\
**Post date:** [December 5, 2023, 6:48am UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/16 "2023-12-05T06:48:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chen\_zhe](https://avatars.discourse-cdn.com/v4/letter/c/c0e974/32.png) [@chen\_zhe](https://forum.inductiveautomation.com/u/chen_zhe)\
**Post date:** [December 5, 2023, 6:48am UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/17 "2023-12-05T06:48:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [December 5, 2023, 7:50am UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/18 "2023-12-05T07:50:25Z")

</div>

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

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/9/d99c71fcb55b411ba12a279931e0e07404640394.png)

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

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [December 5, 2023, 9:16am UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/19 "2023-12-05T09:16:58Z")

</div>

> [@chen\_zhe](#):
>
> If this script has its own parameters, how to use Runscript (). Because with "", the parameters inside are also abnormal.

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.

---

<div class="post-metadata">

**Author:** ![chen\_zhe](https://avatars.discourse-cdn.com/v4/letter/c/c0e974/32.png) [@chen\_zhe](https://forum.inductiveautomation.com/u/chen_zhe)\
**Post date:** [December 7, 2023, 2:54pm UTC](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182/20 "2023-12-07T14:54:03Z")

</div>

```auto
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

```

[Next page](https://forum.inductiveautomation.com/t/where-to-use-the-script-in-label/82182.md?page=2)
