# Component event script in inheritable project

**URL:** <https://forum.inductiveautomation.com/t/component-event-script-in-inheritable-project/89019>\
**Category:** Ignition\
**Tags:** scripting, vision, ignition81\
**Created:** [April 18, 2024, 5:39am UTC](https://forum.inductiveautomation.com/t/component-event-script-in-inheritable-project/89019 "2024-04-18T05:39:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [April 18, 2024, 5:39am UTC](https://forum.inductiveautomation.com/t/component-event-script-in-inheritable-project/89019/1 "2024-04-18T05:39:32Z")

</div>

Hello,

I have a property change script on a radio button of an inheratable project . It seems like the script is been executed multiple times (so in each child project). What can I do to avoid this? I thought of storing the script in another project that is not inheritable, but since it is a component event script, I don't know how to realise this.

Thank you for your suggestions

---

<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:** [April 18, 2024, 6:29am UTC](https://forum.inductiveautomation.com/t/component-event-script-in-inheritable-project/89019/2 "2024-04-18T06:29:16Z")

</div>

That shouldn't happen, there must be something else at play here.

Can you describe the setup more accurately ? Show the script, how it's called, etc.

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [April 18, 2024, 6:47am UTC](https://forum.inductiveautomation.com/t/component-event-script-in-inheritable-project/89019/3 "2024-04-18T06:47:56Z")

</div>

Thats what the script does: calculate the history and store it in a dataset

```auto
if event.propertyName=="actu" or event.propertyName=="selected" :
	if (event.source.selected==True and event.source.actu==True) or event.source.selected==True:
		
		start=event.source.parent.getComponent('CalStart').date
		ende=event.source.parent.getComponent('CalEnd').date
		folderPath = event.source.parent.dataPath 
		folderArchiv = folderPath + "/AlarmData/alarmArchiv"   
		modifiedHistoryDataset = system.dataset.toDataSet(["ID","Description", "Start", "End", "Duration"], [])
		tagPaths=system.tag.readBlocking(folderPath +"/AlarmData/alarmList")[0].value	
		startTimestamp=None
		for x in range(tagPaths.rowCount):
			identification = tagPaths.getValueAt(x,0) 
			description =tagPaths.getValueAt(x,1)
			tagPath = tagPaths.getValueAt(x,2) 
			history = system.tag.queryTagHistory([tagPath],start,ende, aggregationMode="LastValue")
			for i in range(history.rowCount):
				if history.getValueAt(i,1) == 1: # Check if the tag value is 1
					startTimestamp = history.getValueAt(i,0)
					endTimestamp=history.getValueAt(i + 1, 0) if i<history.rowCount-1 else system.date.now()
			        if startTimestamp is not None and endTimestamp is not None:
			        	formatted_duration = shared.Func_Dataset.calculate_duration(startTimestamp, endTimestamp)
			        	startFormatted = system.date.format(startTimestamp, "dd.MM.yyyy HH:mm:ss")
			        	endFormatted = system.date.format(endTimestamp, "dd.MM.yyyy HH:mm:ss")
			        	modifiedHistoryDataset = system.dataset.addRow(modifiedHistoryDataset, [identification, description, startFormatted, endFormatted, formatted_duration])
		modifiedHistoryDataset=system.dataset.sort(modifiedHistoryDataset, 2, False)		        
		system.tag.writeBlocking(folderArchiv, modifiedHistoryDataset)
		event.source.actu=False

```

I have tested the code in a script console and for the same time range I got with the script console 54 entries in the dataset but with the component event script there are more than 7250 entries and the entries are duplicated, sometimes x2 or x3 but sometimes even x10.

---

<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:** [April 18, 2024, 8:16am UTC](https://forum.inductiveautomation.com/t/component-event-script-in-inheritable-project/89019/4 "2024-04-18T08:16:25Z")

</div>

I can't tell you why it would execute multiple times, but frankly I'm not even sure that's what's actually happening. I can't see how multiple executions would result in duplicated data, since you're overwriting the dataset anyway.  
What I can tell you for sure is that this script needs some work:

- Try not to do multiple call `queryTagHistory` in a loop, it takes a list of paths to query all of them at once
- Don't store formatted dates. Dataset can stored actual date objects.

Can you tell us what you're trying to achieve with this ? What's the endgoal ?  
I kinda feel like you're going in the wrong direction.

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [April 18, 2024, 8:40am UTC](https://forum.inductiveautomation.com/t/component-event-script-in-inheritable-project/89019/5 "2024-04-18T08:40:28Z")

</div>

It works when I save the script in the property change of both calendar components instead.

```auto
if event.propertyName=="date":
	ende = event.source.date
	start = event.source.parent.getComponent('CalStart').date
	if event.source.parent.getComponent("radioButton").selected:          

```

I want to display for a given time range the start, end and duration when an alarm was on.  
No it doesn't help to call queryTagHistory just once. The tagPaths are store in a dataset, to store it in a list, I first have to iterate through the entire dataset and retrieve them. Then the result I get is a dataset with number columns= number tagPaths +1 and number rows= number of different timestamps. With this structure, in order to calculate the duration each time each tag was on, I have to go through all columns and rows and that takes much more time.
