# Script: Tag Values exported to CSV every 1H, with data records for every 10 mins

**URL:** <https://forum.inductiveautomation.com/t/script-tag-values-exported-to-csv-every-1h-with-data-records-for-every-10-mins/59115>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [May 2, 2022, 12:22pm UTC](https://forum.inductiveautomation.com/t/script-tag-values-exported-to-csv-every-1h-with-data-records-for-every-10-mins/59115 "2022-05-02T12:22:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![edwinfreirec](https://avatars.discourse-cdn.com/v4/letter/e/8edcca/32.png) [@edwinfreirec](https://forum.inductiveautomation.com/u/edwinfreirec)\
**Post date:** [May 2, 2022, 12:22pm UTC](https://forum.inductiveautomation.com/t/script-tag-values-exported-to-csv-every-1h-with-data-records-for-every-10-mins/59115/1 "2022-05-02T12:22:58Z")

</div>

Hi there,

We need to grab / export data from a PLC, data is coming through OPC UA, to CSV file. Initially, we are interested in tagname, datatime, value and Quality. The exported file should have the records for the last 4 hours, which contains data for every 10 mins value. This file will be generated every hour. It means that each file will have 25 records every hour, i.e File202205022100 file for 2nd of May 21H00 will have records from 17H00, 17H10, 17H20 …21H00.

Please, anybody can suggest the better approach for this. Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![tim.schultz](https://avatars.discourse-cdn.com/v4/letter/t/71e660/32.png) [@tim.schultz](https://forum.inductiveautomation.com/u/tim.schultz)\
**Post date:** [May 2, 2022, 12:40pm UTC](https://forum.inductiveautomation.com/t/script-tag-values-exported-to-csv-every-1h-with-data-records-for-every-10-mins/59115/2 "2022-05-02T12:40:06Z")

</div>

Set up your tags to historize appropriately (as periodic with a sample rate & max time of 10 minutes I think)

To convert, I would suggest a scheduled gateway event. In the script, you’ll need to retrieve your tag history. Once you have your results, store them to CSV using standard functions. See docs here:  
[https://docs.inductiveautomation.com/display/DOC81/Export+Tag+Historian+to+CSV](https://docs.inductiveautomation.com/display/DOC81/Export+Tag+Historian+to+CSV)

To write the CSV to file:

```auto
import os
temp_filename = system.file.getTempFile('csv')
system.file.writeFile(temp_filename,yourHistoryData,0)
os.rename(temp_filename,your_filename_here)

```

---

<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 2, 2022, 12:57pm UTC](https://forum.inductiveautomation.com/t/script-tag-values-exported-to-csv-every-1h-with-data-records-for-every-10-mins/59115/3 "2022-05-02T12:57:14Z")

</div>

Tim, how would you schedule the tag historian to record on the hour and every ten minutes? Surely this is a job for a scheduled gateway event script set to 0/10 \* \* \* \* or else a transaction group with a 10 minute trigger starting on the hour?

---

<div class="post-metadata">

**Author:** ![tim.schultz](https://avatars.discourse-cdn.com/v4/letter/t/71e660/32.png) [@tim.schultz](https://forum.inductiveautomation.com/u/tim.schultz)\
**Post date:** [May 2, 2022, 1:16pm UTC](https://forum.inductiveautomation.com/t/script-tag-values-exported-to-csv-every-1h-with-data-records-for-every-10-mins/59115/4 "2022-05-02T13:16:56Z")

</div>

I think a transaction group would work well here also, good call. They’re never my first thought for some reason. Here’s what I think is the right setting:

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/7/67c0380d9697ae599a0bc8d7fdfd7ff46051f0f6.png)

---

<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 2, 2022, 2:36pm UTC](https://forum.inductiveautomation.com/t/script-tag-values-exported-to-csv-every-1h-with-data-records-for-every-10-mins/59115/5 "2022-05-02T14:36:37Z")

</div>

That’s the setting for the tag history. If the OP wants this to occur on the hour and every 10 minutes then a 10-minute tag group would need to be set up. (The OP has a choice to use a transacton log as described in my first post or this method in the tag historian.)

 ![10 minute tag group](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/a/2a4e5d174156e1a5b185ac6ecc0a44e0ee4a6920.png)

The driving expression should be set to  
`dateExtract(now(1000), "minute") % 10`  
The modulo arithmetic and the `One Shot: true` should cause this to fire once on the hour and every ten minutes after.  
You then set the tag to sample based on that tag group.

![Historical tag group](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/f/afdf692e519f364f6f98cad2370a13eca2bc1a0b.png)

---

<div class="post-metadata">

**Author:** ![edwinfreirec](https://avatars.discourse-cdn.com/v4/letter/e/8edcca/32.png) [@edwinfreirec](https://forum.inductiveautomation.com/u/edwinfreirec)\
**Post date:** [May 2, 2022, 3:59pm UTC](https://forum.inductiveautomation.com/t/script-tag-values-exported-to-csv-every-1h-with-data-records-for-every-10-mins/59115/6 "2022-05-02T15:59:04Z")

</div>

Thanks you both guys for your thoughts. It has been really detalied for tag historian. It is appreciated. I wonder whether the approach would be by transaction group, how it looks like. If I am not wrong, I assume that we can trigger the transaction group by schedule, where we match exactly every 10 mins, starting on HH00 time. is it correct? However, I am not sure how to pass data to CSV file. Perhaps it would be by the use of system.dataset.exportCSV function…any suggestion? thanks in advance.

---

<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 2, 2022, 4:08pm UTC](https://forum.inductiveautomation.com/t/script-tag-values-exported-to-csv-every-1h-with-data-records-for-every-10-mins/59115/7 "2022-05-02T16:08:43Z")

</div>

> [@edwinfreirec](#):
>
> system.dataset.exportCSV

That sounds appropriate. I've never tried it. Maybe someone else can respond. The manual gives an example.  
[https://docs.inductiveautomation.com/display/DOC81/system.dataset.exportCSV](https://docs.inductiveautomation.com/display/DOC81/system.dataset.exportCSV)

Watch one thing. The example uses the filename `data.csv` which means that you could have a conflict if multiple clients were triggered at the same time. The standard web technique to avoid this is to use a random filename for the task and then delete it when done. e.g.,

```auto
import random
filename = "data%s.csv" % (random.randint(0, 1000000))

```

(Check this in the script console. I haven't!
