# Script for adding tag alarm

**URL:** <https://forum.inductiveautomation.com/t/script-for-adding-tag-alarm/94639>\
**Category:** Ignition\
**Created:** [August 14, 2024, 11:40am UTC](https://forum.inductiveautomation.com/t/script-for-adding-tag-alarm/94639 "2024-08-14T11:40:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Filarm27](https://avatars.discourse-cdn.com/v4/letter/f/b9bd4f/32.png) [@Filarm27](https://forum.inductiveautomation.com/u/Filarm27)\
**Post date:** [August 14, 2024, 11:40am UTC](https://forum.inductiveautomation.com/t/script-for-adding-tag-alarm/94639/1 "2024-08-14T11:40:58Z")

</div>

Hello everyone.

Is there a way to add an alarm to an OPC boolean tag via script?

With adding an alarm I mean without manually pressing the button shown below in the image. But automatically created via a script that I would then run inside a button.

 ![Cattura](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/7/f709076bdaa99052d69ea7d31222e459ceb6549a.jpeg)

I know that there is the 'system.tag.configure' to modify the alarm configurations. But without adding the tag alarm I am not able to use it.

Thank you all.

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [August 14, 2024, 12:13pm UTC](https://forum.inductiveautomation.com/t/script-for-adding-tag-alarm/94639/2 "2024-08-14T12:13:48Z")

</div>

It is system.tag.configure. The docs show an example for alarms.

> **[system.tag.configure | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-tag/system-tag-configure)**
>
> Creates tags from a given list of Python dictionaries or from a JSON source string.

---

<div class="post-metadata">

**Author:** ![Filarm27](https://avatars.discourse-cdn.com/v4/letter/f/b9bd4f/32.png) [@Filarm27](https://forum.inductiveautomation.com/u/Filarm27)\
**Post date:** [August 14, 2024, 2:37pm UTC](https://forum.inductiveautomation.com/t/script-for-adding-tag-alarm/94639/3 "2024-08-14T14:37:20Z")

</div>

Yes I saw. And through the following script I get to create an OPC boolean tag from scratch.

```python
# The provider and folder the Tag will be placed at. 
baseTagPath = "[default]MyPath"
 
# Properties that will be configured on that Tag.
name = "BitAlarm_00_"
opcItemPath = "nsu=KEPServerEX;s=DR_1.CH_1.Allarmi[x].Allarmi[00].BitAlarm[x].BitAlarm[00]"
opcServer = "KEPServerEX"
valueSource = "opc"
dataType = "Boolean"
 
alarms = [
            {
                "name": "Alarm_0_1.",               
                "priority": "High",               
                "setpointA": 1,
                "label": "Example",
                "displayPath": "12345",
                "shelvingAllowed": False
                
            }
        ]
        
       
# Configure the tag.
tag = [
		{
            "name": name,         
            "opcItemPath" : opcItemPath,
            "opcServer": opcServer,
            "valueSource": valueSource,
            "dataType": dataType,
            "alarms": alarms
        }
    ]
 
 
# Set the collision policy to Abort. Thus, if a Tag already exists at the base path,
# we will not override the Tag. If you are overwriting an existing Tag, then set this to "o".
collisionPolicy = "o"
 
# Create the Tag.
system.tag.configure(baseTagPath, tag, collisionPolicy)

```

My situation however is the following: I import the tags directly by importing them from KepServerEx, then it creates the variable tree with folders and subfolders of the various alarm tags.  
From this situation I would like to alarm the various tags to be able to configure them. The problem is that I cannot add the alarm via script.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 14, 2024, 2:50pm UTC](https://forum.inductiveautomation.com/t/script-for-adding-tag-alarm/94639/4 "2024-08-14T14:50:30Z")

</div>

What do you mean ?  
That's exactly what this script does.  
The alarms in the `alarms` list are the alarms you're adding to the tag. Isn't that what you want ?

Use `system.tag.getConfiguration` to retrieve your tags configurations, add the alarms you want, then use `system.tag.configure`.

---

<div class="post-metadata">

**Author:** ![Filarm27](https://avatars.discourse-cdn.com/v4/letter/f/b9bd4f/32.png) [@Filarm27](https://forum.inductiveautomation.com/u/Filarm27)\
**Post date:** [August 16, 2024, 7:02am UTC](https://forum.inductiveautomation.com/t/script-for-adding-tag-alarm/94639/5 "2024-08-16T07:02:44Z")

</div>

Yes. I have tried to alarm an existing tag using this script:

```python
tagPath = "MyTagPath"

tagConfig = system.tag.getConfiguration(tagPath, returnDefaults=True)[0]

alarmConfig = {
    "name": "Alarm_0", 
    "enabled": True,  
    "priority": "High", 
    "label": "Test", 
}

tagConfig['alarms'] = [alarmConfig]

system.tag.configure(tagPath, tagConfig, "o")   

```

But the response is this:

**[Bad\_Unsupported("The target path 'MyTagPath' cannot accept children tags.")]**

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 16, 2024, 7:20am UTC](https://forum.inductiveautomation.com/t/script-for-adding-tag-alarm/94639/6 "2024-08-16T07:20:17Z")

</div>

Because that's not how `configure` works. You're trying to add a tag in itself.

```python
foo = system.tag.getConfiguration("foo")[0]

foo['alarms'] = [
	{
	    "name": "Alarm_0", 
	    "enabled": True,  
	    "priority": "High", 
	    "label": "Test", 
	}
]

system.tag.configure("", foo)

```

Note how I'm not passing the tag path, but its parent's path ?  
It's because `configure` is initially supposed to take a [list of tags](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-tag/system-tag-configure#syntax). Just like the `read*` and `write*` functions.  
If you pass a list of tags, you can't expect a single tag path to work.

Try to change the path you pass to `configure`.

---

<div class="post-metadata">

**Author:** ![Filarm27](https://avatars.discourse-cdn.com/v4/letter/f/b9bd4f/32.png) [@Filarm27](https://forum.inductiveautomation.com/u/Filarm27)\
**Post date:** [August 20, 2024, 10:46am UTC](https://forum.inductiveautomation.com/t/script-for-adding-tag-alarm/94639/7 "2024-08-20T10:46:25Z")

</div>

Using your script it creates a tag outside of each folder of the variable tree alarmed with the indicated properties.

while the tag I would like to alarm does not undergo any modification

```python
foo = system.tag.getConfiguration("MyTagPath")[0]

foo['alarms'] = [
	{
	    "name": "Alarm_0", 
	    "enabled": True,  
	    "priority": "High", 
	    "label": "Test", 
	}
]

system.tag.configure("", foo)

```

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 20, 2024, 1:02pm UTC](https://forum.inductiveautomation.com/t/script-for-adding-tag-alarm/94639/8 "2024-08-20T13:02:27Z")

</div>

Well you need to pass the path of the folder where you want to put the tag as a parameter to `configure`... I can't do that for you.

Let's make an example.  
Let's say your tag path is `[default]site_a/machine_1/foo`.  
Then it will look like this

```python
foo = system.tag.getConfiguration("[default]site_a/machine_1/foo")[0]
...
system.tag.configure("[default]site_a/machine_1", foo)

```

---

<div class="post-metadata">

**Author:** ![Filarm27](https://avatars.discourse-cdn.com/v4/letter/f/b9bd4f/32.png) [@Filarm27](https://forum.inductiveautomation.com/u/Filarm27)\
**Post date:** [August 21, 2024, 7:51am UTC](https://forum.inductiveautomation.com/t/script-for-adding-tag-alarm/94639/9 "2024-08-21T07:51:07Z")

</div>

It works. Thank you so much for your patience!! 🙏
