# Automatic Daily Export

**URL:** <https://forum.inductiveautomation.com/t/automatic-daily-export/102621>\
**Category:** Ignition\
**Tags:** database, scripting, perspective\
**Created:** [February 20, 2025, 2:17pm UTC](https://forum.inductiveautomation.com/t/automatic-daily-export/102621 "2025-02-20T14:17:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_MUSSO](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@Thomas\_MUSSO](https://forum.inductiveautomation.com/u/Thomas_MUSSO)\
**Post date:** [February 20, 2025, 2:17pm UTC](https://forum.inductiveautomation.com/t/automatic-daily-export/102621/1 "2025-02-20T14:17:13Z")

</div>

Hello,

I would like to create an automatic daily export of several variables.

So I coded this (on a simple button for the moment):

```python
import system 

def runAction(self, event):
    # Chemin du tag à exporter
    tag_path = "[default]Station Météo/Temperature"
    tag_name = "Temperature" # Nom du tag pour l'export
    
    # Récupérer l'historique des données sur 1 heure
    tag_history = system.tag.queryTagHistory(
        paths=[tag_path],
        startDate=system.date.addMinutes(system.date.now(), -60),
        endDate=system.date.now(),
        returnSize=10,
        aggregationMode=None, 
        perPoint=True,
        database="DB_Septèmes"
    )

    # Vérifier si des données sont récupérées
    if tag_history.rowCount == 0:
        system.util.getLogger("ExportFTP").info("Aucune donnée trouvée pour ce tag.")
        return

    # Construire les données à exporter
    csv_content = "Timestamp," + tag_name + " (Valeur)\n"
    for row in range(tag_history.rowCount):
        timestamp = system.date.format(tag_history.getValueAt(row, 0), "yyyy-MM-dd HH:mm:ss")
        value = tag_history.getValueAt(row, 1)
        csv_content += "{},{}\n".format(timestamp, value)

    # Définir le chemin du fichier CSV
    file_path = "C:\\Export_ODP\\historique_1h.csv"

    # Vérifier et créer le dossier si nécessaire
    if not system.file.fileExists("C:\\Export_ODP"):
        system.file.mkdir("C:\\Export_ODP")

    # Écrire dans le fichier CSV
    system.file.writeFile(file_path, csv_content.encode('utf-8'))

    system.util.getLogger("ExportFTP").info("Exportation CSV terminée. Fichier créé : " + file_path)

```

But here is the result:

```python
Timestamp,Temperature (Valeur)
2025-02-20 13:29:22,None
2025-02-20 13:35:22,None
2025-02-20 13:41:22,None
2025-02-20 13:47:22,None
2025-02-20 13:53:22,None
2025-02-20 13:59:22,None
2025-02-20 14:05:22,None
2025-02-20 14:11:22,None
2025-02-20 14:17:22,None
2025-02-20 14:23:22,None

```

I have the impression that it works, but that the data cannot be retrieved. ‘None’

The data is still available when I display it in a PowerGraph

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/3/532f03e8158c9edc58c87bb9af84936af3a726dd.png)

I try with this path` [default]Station Météo/Temperature` , but also the complete path that I use in my Powerchart ` histprov:DB_Septèmes:/drv:ignition-ign-srv-septemes:default:/tag:station météo/temperature`

Thank you in advance for your help. Have a nice day!

---

<div class="post-metadata">

**Author:** ![leonardo.godoi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/leonardo.godoi/32/27178_2.png) [@leonardo.godoi](https://forum.inductiveautomation.com/u/leonardo.godoi)\
**Post date:** [February 20, 2025, 2:27pm UTC](https://forum.inductiveautomation.com/t/automatic-daily-export/102621/2 "2025-02-20T14:27:19Z")

</div>

Maybe it's an issue caused because of the lack of Unicode encoding on your strings, like this:

```python
import system 

def runAction(self, event):
    # Chemin du tag à exporter
    tag_path = u"[default]Station Météo/Temperature"
    tag_name = u"Temperature" # Nom du tag pour l'export
    
    # Récupérer l'historique des données sur 1 heure
    tag_history = system.tag.queryTagHistory(
        paths=[tag_path],
        startDate=system.date.addMinutes(system.date.now(), -60),
        endDate=system.date.now(),
        returnSize=10,
        aggregationMode=None, 
        perPoint=True,
        database=u"DB_Septèmes"
    )

    # Vérifier si des données sont récupérées
    if tag_history.rowCount == 0:
        system.util.getLogger("ExportFTP").info(u"Aucune donnée trouvée pour ce tag.")
        return

    # Construire les données à exporter
    csv_content = u"Timestamp," + tag_name + u" (Valeur)\n"
    for row in range(tag_history.rowCount):
        timestamp = system.date.format(tag_history.getValueAt(row, 0), "yyyy-MM-dd HH:mm:ss")
        value = tag_history.getValueAt(row, 1)
        csv_content += "{},{}\n".format(timestamp, value)

    # Définir le chemin du fichier CSV
    file_path = "C:\\Export_ODP\\historique_1h.csv"

    # Vérifier et créer le dossier si nécessaire
    if not system.file.fileExists("C:\\Export_ODP"):
        system.file.mkdir("C:\\Export_ODP")

    # Écrire dans le fichier CSV
    system.file.writeFile(file_path, csv_content.encode('utf-8'))

    system.util.getLogger("ExportFTP").info(u"Exportation CSV terminée. Fichier créé : " + file_path)

```

My first project was completely in Portuguese so my first instinct was to use accents on the appropriate words but after running into some issues due to the unicode i started to avoid using the correct spelling.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [February 20, 2025, 4:26pm UTC](https://forum.inductiveautomation.com/t/automatic-daily-export/102621/3 "2025-02-20T16:26:46Z")

</div>

Separately from any other issues with your script, you've got a _fundamental_ issue.

An "automatic daily export" _does not belong in Perspective_.

You should be using either reporting or a gateway scheduled script. As is, your code is going to be running once _per Perspective session open running this action_, even though it's writing to a single file on the gateway's hard drive.

---

<div class="post-metadata">

**Author:** ![Thomas\_MUSSO](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@Thomas\_MUSSO](https://forum.inductiveautomation.com/u/Thomas_MUSSO)\
**Post date:** [February 21, 2025, 8:00am UTC](https://forum.inductiveautomation.com/t/automatic-daily-export/102621/4 "2025-02-21T08:00:47Z")

</div>

Thank you both for your answers. Leonardo, that was the problem!  
And PGrifith, now I can put this code on Gateway Scheduled Script.  
Have a good day, everyone!
