Accessing a points tag value via script?
-
Is it possible to access a data points tag value in a script?
"tags": { "breaker_size": "20" }
I did not see the tags listed when using print(point). Curious if it's possible and if so the required syntax.
Thank you.
-
Hi Wingnut2.0,
At the moment you'll have to do,
var tags = com.serotonin.m2m2.db.dao.DataPointTagsDao.instance.getTagsForDataPointId( p.getDataPointWrapper().getId() );
-
I have made the
p.getDataPointWrapper().getTags()
function load the tags if they're null. -
Thank you, Phillip. Exactly what I needed.
Using your example I can easily retrieve a specific tag -
tags.breaker_size;
Next question is can I write to a tag using this function and is the there any history associated with a given tags value?
-
You could set a points tags either by setting the running data point to have the new tags and saving them, like so,
var newTags = {"key":"value"}; CONTEXT_POINTS.p.getVO().setTags(newTags); com.serotonin.m2m2.db.dao.DataPointTagsDao.instance.saveDataPointTags(CONTEXT_POINTS.p.getVO());
or you could save the whole point, like so,
var newTags = {"key":"value"}; CONTEXT_POINTS.p.getVO().setTags(newTags); com.serotonin.m2m2.db.dao.DataPointDao.instance.saveDataPoint(CONTEXT_POINTS.p.getVO());
Currently the first option is not generating audit events for the tags, but the second option would. The downside of the second option is that it requires restarting the data point. Then you can see the tag history in the audit table. I have created this issue about the first option not generating audit events: https://github.com/infiniteautomation/ma-core-public/issues/1267
-
@phildunlap said in Accessing a points tag value via script?:
setTags
Works perfectly for creating new tags. Is there another approach to update the value of an existing tag?
Right now if I use this approach I need to re-write all tags & values or I will loose them. For example, if I have 4 tags for a given point and use this approach to set (update) only one of the tags value, the other tags get wiped out. -
Yes, that is true. So, you would want to update the tag in the existing tags map, then, like,
var tags = com.serotonin.m2m2.db.dao.DataPointTagsDao.instance.getTagsForDataPointId( p.getDataPointWrapper().getId() ); tags.key = "value"; CONTEXT_POINTS.p.getVO().setTags(tags); com.serotonin.m2m2.db.dao.DataPointDao.instance.saveDataPoint(CONTEXT_POINTS.p.getVO());
There are not methods to update a tag individually of the other tags. The DataPointTagsDao takes the whole DataPointVO as its argument.