Programmatically enabling / disabling the tag historian

Does anyone have ideas on how to programmatically enable and disable the tag historian? I have an HMI application that requires that the tags to be stored by the historian only while the HMI is running. The end user is not allowed access to the tag provider configuration or the designer.
There is no global option for this and I have tried doing it using the following script but it requires a gateway restart after execution to make it work properly. Its also a messy solution.

from time import sleep # Added 7-21-20 RP
import system

#There is no global way to programmatically enable or disable the tag historian for multiple tags so #each tag must have the Enable History property changed manually using a script like this
#Unfortunately this script has a bug where it doesn’t work properly. When the tags are re-enabled after #being disabled the historian breaks and doesn’t store all of the tags.
#The only solution is to stop and start the Gateway after running this script.
#Even if a solution is found to fix this script it is a messy solution and would be difficult to maintain #when adding or removing more tags to the historian.

#Set the ‘historyEnabled’ state
def Enable(tagPath, boolVal):
tagConfig = system.tag.getConfiguration(tagPath + “value”)
tagConfig[0][‘historyEnabled’] = boolVal
system.tag.configure(tagPath, tagConfig, “o”)

def tagHistoryEnable():
# Set each Tag ‘historyEnabled’ parameter individually to True
# Pressure
tagPath = “[default]Param/PSI/”
Enable(tagPath, True)
# ROP
tagPath = “[default]Param/ROP/”
Enable(tagPath, True)
# Weight
tagPath = “[default]Param/WT/”
Enable(tagPath, True)
# Motor 1 Temp
tagPath = “[default]Param/Motor1_Temp/”
Enable(tagPath, True)
# Motor 2 temp
tagPath = “[default]Param/Motor2_Temp/”
Enable(tagPath, True)
# Tension
tagPath = “[default]Param/Tension/”
Enable(tagPath, True)
# Traction
tagPath = “[default]Param/Traction/”
Enable(tagPath, True)
# Length
tagPath = “[default]Length”
tagConfig = system.tag.getConfiguration(tagPath)
tagConfig[0][‘historyEnabled’] = True
system.tag.configure("", tagConfig)

#I tried just restarting the historian but it doesn’t Work! 8-10-20
# Triggers the Tag Historian to restart the Tag Historian Module
restartVal = system.tag.read("[default]HistorianRestart_Tag").value
system.tag.write("[default]HistorianRestart_Tag",restartVal+1)
# Allow time for the Tag Historian to restart before proceeding 7-21-20 RP
#sleep(1)

return "True" # dont need this just for testing...

def tagHistoryDisable():
# Set each Tag ‘historyEnabled’ parameter individually to False
# Pressure
tagPath = “[default]Param/PSI/”
Enable(tagPath, False)
# ROP
tagPath = “[default]Param/ROP/”
Enable(tagPath, False)
# Weight
tagPath = “[default]Param/WT/”
Enable(tagPath, False)
# Motor 1 Temp
tagPath = “[default]Param/Motor1_Temp/”
Enable(tagPath, False)
# Motor 2 temp
tagPath = “[default]Param/Motor2_Temp/”
Enable(tagPath, False)
# Tension
tagPath = “[default]Param/Tension/”
Enable(tagPath, False)
# Traction
tagPath = “[default]Param/Traction/”
Enable(tagPath, False)
# Length
tagPath = “[default]Length”
tagConfig = system.tag.getConfiguration(tagPath)
tagConfig[0][‘historyEnabled’] = False
system.tag.configure("", tagConfig)

#Doesn’t work! 8-10-20
# Triggers the Tag Historian to restart the Tag Historian Module
restartVal = system.tag.read("[default]HistorianRestart_Tag").value
system.tag.write("[default]HistorianRestart_Tag",restartVal+1)
# Allow time for the Tag Historian to restart before proceeding 7-21-20 RP
#sleep(1)

return "False" # dont need this just for testing...
1 Like

Maybe you should use a transaction group to record values.

I think I tried that route but there was an issue. Let me look at it again…

My initial thought is a driven scan class for your historian scan class. You can look at the System/Gateway/Sessions/SessionCount tag with an expression tag, and use it as a driving tag based on being over 1. The issue I see is it counts all sessions so if you have a designer open it will count it. It also doesn’t see a difference between different projects so if you want it to be only when a specific project is open it won’t work. If your wanting to record values while running a process, I would assume you should be able to use a status from that process to trigger a driven scan class.

I tried a driven scan group but that didn’t work. I found that it only works as the tag update rate for the HMI not the rate at which the tag historian saves the data. Even with the non-driven rate set to 0 the tag would still write to the DB. I wish that it worked because that is exactly how I would like to handle it.

jpark,
I was able to configure a transaction group that works the way I want but now I recall what the problems were.

  1. I would have to purchase the SQL Bridge module and upgrade all of the existing licenses we sold.
  2. All of my reports were designed using the tag history query and I don’t know how big of an impact changing the data source would have. I don’t want to rewrite all of my reports and associated scripts.

I don’t know how many tags you need to store data for or how frequently, but could you script it using system.tag.storeTagHistory()?

bpreston,
I could do that and probably get it to work. I only have 8 tags right now and they need to be saved at a 60 second interval so the script would have to run on a timer.
The downside is that in the future the number of tags could change from 8 to 10, 20 or 50 tags. Also what if I want different intervals. I can already see that trying to maintain a script like that (or like the one I posted above) would become a support problem.
My original goal was to simply start and stop the historian as I started and exited the HMI. I was hoping for a simpler more elegant solution. :slightly_frowning_face:

I’m not sure your going to be able to stop and start it like you want to in an easy way. My other question would be why only store historian data while the HMI is open? If you store it all the time, you can still filter it for trending and reporting based on time ranges allowing you to easily ignore the data collected while it isn’t running. Assuming that the equipment isn’t running outside of the times the HMI is open, I would think you should also be able to make good use of deadbands to limit how much data you collect while the HMI is closed.

Our equipment is mobile and used in the field. I need to collect the data on a job basis while the job is active. The actual job may last a few days but the equipment could be running for weeks. I don’t want to collect all of that useless data. I was going to try to use the HMI client startup and shutdown to start and stop data collection.