# Getting tag's value from 8 hours ago

**URL:** <https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103>\
**Category:** Ignition\
**Tags:** ignition80, scripting, reporting\
**Created:** [May 29, 2024, 7:43pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103 "2024-05-29T19:43:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Derrick\_Overfield](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@Derrick\_Overfield](https://forum.inductiveautomation.com/u/Derrick_Overfield)\
**Post date:** [May 29, 2024, 7:43pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/1 "2024-05-29T19:43:17Z")

</div>

I am wanting to get a tags value from 8 hours ago and store it as another tag. When writing the script in the script console, it works. But I need some help making this work when creating a new tag. I want this value to move exactly 8 hours behind the current value.

Here is the code-

```python
eight_hours_ago = system.date.addHours(system.date.now(), -8)

tag_path = "[default]Mill 2/XV2_5_RunHrsReal"
history = system.tag.queryTagHistory(paths=[tag_path],
                                      startDate=eight_hours_ago,
                                      returnSize=1)

if history:
      	 value = history.getValueAt(0, 1) 
print(value)

```

Thanks for any help.  
Derrick

---

<div class="post-metadata">

**Author:** ![Rick\_de\_Man](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/rick_de_man/32/75670_2.png) [@Rick\_de\_Man](https://forum.inductiveautomation.com/u/Rick_de_Man)\
**Post date:** [May 29, 2024, 7:54pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/2 "2024-05-29T19:54:00Z")

</div>

Have you tried implementing a Tag ValueChange event? Tags paths can be acces relative to the current yah path.

---

<div class="post-metadata">

**Author:** ![Derrick\_Overfield](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@Derrick\_Overfield](https://forum.inductiveautomation.com/u/Derrick_Overfield)\
**Post date:** [May 29, 2024, 8:00pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/4 "2024-05-29T20:00:12Z")

</div>

No, I am working on uptime report so I am wanting the run hours 8 hours ago and then compare to current run hours. This number constantly changes. I haven't spent alot of time scripting so I am new most things.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [May 30, 2024, 11:00am UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/6 "2024-05-30T11:00:48Z")

</div>

> [@Derrick\_Overfield](#):
>
> But I need some help making this work when creating a new tag

I don't understand what this means. Could you elaborate?

---

<div class="post-metadata">

**Author:** ![Derrick\_Overfield](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@Derrick\_Overfield](https://forum.inductiveautomation.com/u/Derrick_Overfield)\
**Post date:** [May 30, 2024, 1:15pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/7 "2024-05-30T13:15:15Z")

</div>

This code works in the script console, but not when creating a new Expression tag. I am trying to put the value from 8 hours ago into a tag and use that tag in a report.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [May 30, 2024, 1:18pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/8 "2024-05-30T13:18:16Z")

</div>

> [@Derrick\_Overfield](#):
>
> a new Expression tag

So you're trying to put the script into an expression tag for use in the reporting Module?

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [May 30, 2024, 1:20pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/9 "2024-05-30T13:20:35Z")

</div>

> [@Derrick\_Overfield](#):
>
> I am trying to put the value from 8 hours ago into a tag

You don't need a tag. Just use a Tag Historian Query in the report?

> **[Tag Historian Query | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/ignition-modules/reporting/report-data/tag-historian-query)**
>
> The Tag Historian Query provides a simple way to query data from Tag Historians. In the Tag Historian Query you can collect data from Historical Tags for specific date ranges, apply aggregates, and specify the sample size. It functions the same as a...

---

<div class="post-metadata">

**Author:** ![Derrick\_Overfield](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@Derrick\_Overfield](https://forum.inductiveautomation.com/u/Derrick_Overfield)\
**Post date:** [May 30, 2024, 1:30pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/10 "2024-05-30T13:30:46Z")

</div>

Yeah, I have it setup like this currently but if the value does not change then the range value will be the same as the current value. So let say the hour meter is at 900.1 hours 8 hours ago and 900.1 currently, the range should be 0, but it will be 900.1.. But if I get this value and use it as a tag, then I can use that tag in other places.

---

<div class="post-metadata">

**Author:** ![Derrick\_Overfield](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@Derrick\_Overfield](https://forum.inductiveautomation.com/u/Derrick_Overfield)\
**Post date:** [May 30, 2024, 1:31pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/11 "2024-05-30T13:31:33Z")

</div>

As an expression tag and then use it in a report. That way I can do more with it than the one report.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [May 30, 2024, 1:40pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/12 "2024-05-30T13:40:08Z")

</div>

Place the script in a project library.

In the report, you can call that script in a Script Datasource. Anywhere else that you may need that data, you can call that script.

There is _no_ need for an additional tag.

---

<div class="post-metadata">

**Author:** ![Derrick\_Overfield](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@Derrick\_Overfield](https://forum.inductiveautomation.com/u/Derrick_Overfield)\
**Post date:** [May 30, 2024, 1:48pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/13 "2024-05-30T13:48:37Z")

</div>

Ok. Tested this code again on script console and it gives the value I need. I added to script library, went to gateway settings- gateway scripting project, set that up. In project library created a new script "Mill1". I created and expression tag and in the expression just used runScript("Mill1") and if I set the data type of the tag to Float I get nothing. If I set to a string Data type, the value is "module Mill1 at 2"

---

<div class="post-metadata">

**Author:** ![Felipe\_CRM](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@Felipe\_CRM](https://forum.inductiveautomation.com/u/Felipe_CRM)\
**Post date:** [May 30, 2024, 1:56pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/14 "2024-05-30T13:56:52Z")

</div>

Assuming you're dead-set on making it a tag (may be redundant, I'm not sure if Ignition would cache the historian query anyway here):

You're trying to run a script module when you should be running a function in that module

Inside `Mill1` should be:

```python
def getOldValue(tag_path):

	eight_hours_ago = system.date.addHours(system.date.now(), -8)

	history = system.tag.queryTagHistory(paths=[tag_path],
                                      startDate=eight_hours_ago,
                                      returnSize=1)

	if history:
		return history.getValueAt(0, 1)
	else:
		return None #will probably cause an error but you'll know something's up

```

and then your `runScript` should be  
`runScript("Mill1.getOldValue", 1000, "[default]Mill 2/XV2_5_RunHrsReal")`

where 1000 is your refresh rate in milliseconds.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [May 30, 2024, 2:06pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/15 "2024-05-30T14:06:44Z")

</div>

> [@Derrick\_Overfield](#):
>
> added to script library,

Let's see the script library implementation

> [@Derrick\_Overfield](#):
>
> runScript("Mill1")

I don't believe runscript is the correct approach, but even if I'm wrong, I'm certain the library script is not being called correctly here. What is your function's name?

---

<div class="post-metadata">

**Author:** ![Derrick\_Overfield](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@Derrick\_Overfield](https://forum.inductiveautomation.com/u/Derrick_Overfield)\
**Post date:** [May 30, 2024, 2:08pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/16 "2024-05-30T14:08:23Z")

</div>

That works.

Thank you!

---

<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:** [May 30, 2024, 2:10pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/17 "2024-05-30T14:10:53Z")

</div>

Tip: 1000 ms is probably excessive. I would imagine that once a minute (60000 ms) would be more than adequate.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [May 30, 2024, 2:14pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/18 "2024-05-30T14:14:06Z")

</div>

> [@Derrick\_Overfield](#):
>
> I created and expression tag and in the expression just used runScript("Mill1")

\*\*\*\* Jumps on Soap Box \*\*\*\*

You just don't need a tag.

Sure, it's just one tag, not a huge deal, but 1 tag quickly turns into hundreds. Now you're adding excess load on your gateway and bloating your internal DB, this will eventually lead to performance issues.

You're duplicating data. Just call the library script directly from wherever you actually need the data (report, vision window, perspective session...etc).

Okay, I've said my piece, do as you will.

\*\*\*\* Steps down from Soap Box \*\*\*\*

---

<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:** [May 30, 2024, 2:30pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/19 "2024-05-30T14:30:38Z")

</div>

lrose, is Markdown missing `<rant>` and `</rant>` tags?

---

<div class="post-metadata">

**Author:** ![Derrick\_Overfield](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@Derrick\_Overfield](https://forum.inductiveautomation.com/u/Derrick_Overfield)\
**Post date:** [May 30, 2024, 2:51pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/20 "2024-05-30T14:51:51Z")

</div>

It works.

---

<div class="post-metadata">

**Author:** ![Felipe\_CRM](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@Felipe\_CRM](https://forum.inductiveautomation.com/u/Felipe_CRM)\
**Post date:** [May 30, 2024, 6:38pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/21 "2024-05-30T18:38:34Z")

</div>

It's more of a question of "best practice" vs "works". There's lots of ways to get something done in Ignition but some of them will make your (or your successor's) life harder down the line.

Think about how often you actually need the data to update, how many machines you'll be doing this with, et cetera.

---

<div class="post-metadata">

**Author:** ![Derrick\_Overfield](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@Derrick\_Overfield](https://forum.inductiveautomation.com/u/Derrick_Overfield)\
**Post date:** [May 30, 2024, 7:01pm UTC](https://forum.inductiveautomation.com/t/getting-tags-value-from-8-hours-ago/91103/22 "2024-05-30T19:01:21Z")

</div>

I'll keep that in mind. For this situation, it will be fine. I just needed a couple of tags and they're in their own folder and labeled. Thanks again.
