Hello, I am trying to setup a client project script (let me know if gateway would be better). To reset nightly tags. It’s a decent amount of tags so I’d like to loop it.
Ideally, I loop all the the tags under a folder, and write 0 to all of them at midnight using some sort of if time = midnight checked hourly.
I can loop using below code:
tags = system.tag.browseTags(parentPath="System1/Efficiency", recursive=True)
for tag in tags:
print tag.path
But my issue is it prints the folder names see below: Should I exclude them/how can I?
@lrose thank you! I want tag path because i want to reset them all all at midnight with something like the full code below:
# Grabs current Date and Time
getDateTime = system.date.now()
# Returns current hour in a 0-23 format
getHour = system.date.format(today, "H")
# Creat array to store tag paths
resetPaths = []
# Checks if time is Midnight
if getHour == 0:
# Gets all tags in folder sub structure and writes to an array
tags = system.tag.browseTags(parentPath="System1/Efficiency", recursive = True)
for tag in tags:
if not tag.isFolder():
# Wrties tag path to array
resetPaths.append (tag.path)
# Writes a 0 to clear all tags in array
system.tag.writeAll(resetPaths,0)
And I’ll run it in a timer script to check once an hour. Just not sure if client or gateway timer script would be better?
Definitely gateway timer script. A client timer script will 1. only work if there’s at least one client running, and 2. be run by each client you have open.
In your script the system.tag.writeAll() should be outside of the for loop. You only want to call it once. You will also need to provide it with a list of values, that matches list of paths.
# Grabs current Date and Time
getDateTime = system.date.now()
# Returns current hour in a 0-23 format
getHour = system.date.format(today, "H")
# Creat array to store tag paths
resetPaths = []
# Checks if time is Midnight
if getHour == 0:
# Gets all tags in folder sub structure and writes to an array
tags = system.tag.browseTags(parentPath="System1/Efficiency", recursive = True)
for tag in tags:
if not tag.isFolder():
# Wrties tag path to array
resetPaths.append (tag.path)
# Writes a 0 to clear all tags in array
system.tag.writeAll(resetPaths,[0] * len(resetPaths))
You could potentially use None if that is what you mean. I suppose it just depends on the types of the tags you are writing. For integers None is essentially 0.
Do you have any logging in the script? Do you see any errors in the Wrapper Logs? What are the types of the tags you are resetting and are they all the same type?
0 is not the same as "" is not the same as null/None - you probably have to write the correct initial value type for the tag’s type. You could read the data type and have a simple dictionary of tag types : initial tag values.
# Grabs current Date and Time
getDateTime = system.date.now()
# Returns current hour in a 0-23 format
getHour = system.date.format(getDateTime, "H")
# Creates array to store tag paths
intPaths = []
stringPaths = []
# Checks if time is Midnight
if getHour == 7:
# Gets all tags in folder sub structure and writes to an array
tags = system.tag.browseTags(parentPath="System1/Efficiency", recursive = True)
for tag in tags:
if not tag.isFolder():
if tag.dataType == Int4:
# Wrties tag path to array
intPaths.append (tag.path)
elif tag.dataType == String:
stringPaths.append (tag.path)
# Creates array for reset values
resetInt = [None] * len(intPaths)
resetStr = [""] * len(stringPaths)
# Writes a 0 to clear all tags in array
system.tag.writeAll(intPaths,resetInt)
system.tag.writeAll(stringPaths,resetStr)
The console log is not the same as the wrapper log.
Here is a small example of using a logger.
This:
if tag.dataType == Int4:
Doesn't do what you think it does. Int4 is a type, tag.dataTypereturns a string value returns a com.inductiveautomation.ignition.common.sqltags.model.types.DataType. The two can never be equivalent.
I would also use 0 for the default value for Integers. Also, instead of calling writeAll twice, combine the lists first and then call writeAll
```
paths = intPaths + stringPaths
values = resetInt + resetStr
system.tag.writeAll(paths,values)
```
from com.inductiveautomation.ignition.common import TypeUtilities
# Grabs current Date and Time
getDateTime = system.date.now()
# Returns current hour in a 0-23 format
getHour = system.date.getHour24(getDateTime)
# Creates list to store tag paths
paths = []
# Checks if time is Midnight
if getHour == 7:
# Gets all tags in folder sub structure and writes to an array
tags = system.tag.browseTags(parentPath="folder", recursive = True)
for tag in tags:
if not tag.isFolder():
paths.append((tag.path, TypeUtilities.getInitValueForClass(tag.dataType.javaType)))
paths, values = list(zip(*paths))
system.tag.writeAll(paths, values)
@PGriffith that worked like a charm! Now if I can just learn from your code and understand what you did. lol. If you could explain a little more in depth how your code works I’d be very appreciative.
There are just a few tags I’ll need to work on including that im sure are somewhere else. maybe write that tag to another one in a subfolder so this works 100% for me. But that should be easy.
I’d just really like to do some googling and understand it so i’m capable of doing it in the future.