How can I write to a file on a timed basis?
I'd like to write the tag name and value of all tags in a domain to a file on a regular interval.
Overview
By using the .TimerAt() function, you can trigger a method that will send all tags in a specific domain to a file. The attached code file saves the tag name and value to a text file. Each tag is put on a new line and separated by a comma. For example:
Tag1, 341
Tag2, 6
Tag3, 92.34
Running the Script:
Download the attached sample script. In the scripting section of DataHub, click "Add" then browse for the script you downloaded. Edit the script to use your domain and file name. Save the script and close the script editor. Check the checkbox next to the script and click "apply".
Sample Code:
//Trigger the time event
.TimerAt(nil,nil,nil,17,45,00, `(@self).writeToFile());
//Write to the file
if ((.fp = open(.filename, "w", nil)) != nil);
{
local tags = datahub_points(.domainName);
with tag in tags do
{
if(tag.value != nil)
{
line = format("%a,%a\n", tag.name, tag.value);
writec(.fp, line);
}
}
}
close(.fp);