Automatic Daily Export

Hello,

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

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

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:

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

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!

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

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.

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.

2 Likes

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!

2 Likes