# BasicAlarmEvent to PyAlarmEvent

**URL:** <https://forum.inductiveautomation.com/t/basicalarmevent-to-pyalarmevent/32949>\
**Category:** Ignition\
**Created:** [March 9, 2020, 9:59pm UTC](https://forum.inductiveautomation.com/t/basicalarmevent-to-pyalarmevent/32949 "2020-03-09T21:59:00Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![jramirez3](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jramirez3](https://forum.inductiveautomation.com/u/jramirez3)\
**Post date:** [March 9, 2020, 9:59pm UTC](https://forum.inductiveautomation.com/t/basicalarmevent-to-pyalarmevent/32949/1 "2020-03-09T21:59:00Z")

</div>

Is there a way to get a PyAlarmEvent from the BasicAlarmEvent returned from system.alarm.queryStatus?

That function returns the BasicAlarmEvent which has several built-in methods to get alarm information but cannot get associated data.

A PyAlarmEvent can access associated data values. How can I get the BasicAlarmEvent to a PyAlarmEvent or how can I get the PyAlarmEvent to begin with?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 9, 2020, 10:35pm UTC](https://forum.inductiveautomation.com/t/basicalarmevent-to-pyalarmevent/32949/2 "2020-03-09T22:35:45Z")

</div>

PyAlarmEvent is basically just retrieving the entire list of properties, then checking if the string you provided matches the string name of a property. You could do this yourself; something like this (untested):

```auto
class WrappedAlarmEvent:
	def __init__ (self, basicEvent):
		self.event = basicEvent
		self.properties = {prop.name: event.getOrDefault(prop) for prop in event.properties}

	def get(prop):
		return self.properties.get(prop)

### Usage:

alarms = [WrappedAlarmEvent(alarm) for alarm in system.alarm.queryStatus()]

if alarms:
	print alarms[0].get("associatedData")

```

---

<div class="post-metadata">

**Author:** ![jramirez3](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jramirez3](https://forum.inductiveautomation.com/u/jramirez3)\
**Post date:** [March 9, 2020, 11:30pm UTC](https://forum.inductiveautomation.com/t/basicalarmevent-to-pyalarmevent/32949/3 "2020-03-09T23:30:41Z")

</div>

It errors out saying prop is not defined. I think it’s the prop.name but not for sure. I changed it to `for prop in event.properties` not in the dictionary and print prop, it says `'java.beans.PropertyChangeEvent' object has no attribute 'properties'`

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 9, 2020, 11:31pm UTC](https://forum.inductiveautomation.com/t/basicalarmevent-to-pyalarmevent/32949/4 "2020-03-09T23:31:28Z")

</div>

Oh, my bad - typo in the code. Change `self.event = event` to `self.event = basicEvent`.

---

<div class="post-metadata">

**Author:** ![jramirez3](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jramirez3](https://forum.inductiveautomation.com/u/jramirez3)\
**Post date:** [March 9, 2020, 11:38pm UTC](https://forum.inductiveautomation.com/t/basicalarmevent-to-pyalarmevent/32949/5 "2020-03-09T23:38:50Z")

</div>

> [@paul-griffith](#):
>
> self.event = basicEvent

```auto
class WrappedAlarmEvent:
	def __init__ (self, basicEvent):
		self.event = basicEvent
		
		self.properties = {prop.name: (event.getOrDefault(prop) for prop in event.properties)}

	def get(prop):
		return self.properties.get(prop)

if event.propertyName == "selectedAlarms":
	alarmDatasetInfo = system.dataset.toPyDataSet(event.newValue)
	if alarmDatasetInfo.getRowCount() >= 1:
		for row in alarmDatasetInfo:
			sourcePath = str(row["Source"])
			alarms = [WrappedAlarmEvent(alarm) for alarm in system.alarm.queryStatus(source=[sourcePath])]
			
			if alarms:
				print alarms[0].get("keyPosition")

```

same error  
File "event:propertyChange", line 15, in

File "event:propertyChange", line 5, in **init**

NameError: global name 'prop' is not defined

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 10, 2020, 12:13am UTC](https://forum.inductiveautomation.com/t/basicalarmevent-to-pyalarmevent/32949/6 "2020-03-10T00:13:44Z")

</div>

Okay, took a bit more doing:

```auto
class WrappedAlarmEvent:
	methods = {
		'Clear': 'clearedData',
		'Active': 'activeData',
		'Ack': 'ackData'
	}
	
	def __init__ (self, event):
		self.event = event
		self.properties = {prop.getName(): event.getOrDefault(prop) for prop in event.properties}
		self.properties.update({prop.getName(): event.getOrDefault(prop) for prop in getattr(event, WrappedAlarmEvent.methods[str(event.lastEventState)]).properties})

	def get(self, prop):
		return self.properties.get(prop)
		
	def __repr__ (self):
		return str(self.properties)

#if event.propertyName == "selectedAlarms":
#	alarmDatasetInfo = system.dataset.toPyDataSet(event.newValue)
#	if alarmDatasetInfo.getRowCount() >= 1:
# for row in alarmDatasetInfo:
# sourcePath = str(row["Source"])
alarms = [WrappedAlarmEvent(alarm) for alarm in system.alarm.queryStatus()]

for alarm in alarms:
	print alarm.event.displayPathOrSource
	for prop, value in alarm.properties.items():
		print '\t', prop, '=', value

```

---

<div class="post-metadata">

**Author:** ![jramirez3](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jramirez3](https://forum.inductiveautomation.com/u/jramirez3)\
**Post date:** [March 10, 2020, 12:22am UTC](https://forum.inductiveautomation.com/t/basicalarmevent-to-pyalarmevent/32949/7 "2020-03-10T00:22:41Z")

</div>

It keeps telling me prop is not defined. The ‘prop.getName()’ part, prop is not defined yet, so we can use it yet right?

---

<div class="post-metadata">

**Author:** ![Kurt.Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kurt.larson/32/6426_2.png) [@Kurt.Larson](https://forum.inductiveautomation.com/u/Kurt.Larson)\
**Post date:** [March 10, 2020, 3:10am UTC](https://forum.inductiveautomation.com/t/basicalarmevent-to-pyalarmevent/32949/8 "2020-03-10T03:10:59Z")

</div>

I’m guessing you’re not testing this on 8? Try this instead:

```auto
class WrappedAlarmEvent:
	methods = {
		'Clear': 'clearedData',
		'Active': 'activeData',
		'Ack': 'ackData'
	}
	
	def __init__ (self, event):
		self.event = event
		self.properties = dict((prop.getName(), event.getOrDefault(prop)) for prop in event.properties)
		self.properties.update(dict((prop.getName(), event.getOrDefault(prop)) for prop in getattr(event, WrappedAlarmEvent.methods[str(event.lastEventState)]).properties))

	def get(self, prop):
		return self.properties.get(prop)
		
	def __repr__ (self):
		return str(self.properties)

#if event.propertyName == "selectedAlarms":
#	alarmDatasetInfo = system.dataset.toPyDataSet(event.newValue)
#	if alarmDatasetInfo.getRowCount() >= 1:
# for row in alarmDatasetInfo:
# sourcePath = str(row["Source"])
alarms = [WrappedAlarmEvent(alarm) for alarm in system.alarm.queryStatus()]

for alarm in alarms:
	print alarm.event.displayPathOrSource
	for prop, value in alarm.properties.items():
		print '\t', prop, '=', value

```

---

<div class="post-metadata">

**Author:** ![jramirez3](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jramirez3](https://forum.inductiveautomation.com/u/jramirez3)\
**Post date:** [March 10, 2020, 12:47pm UTC](https://forum.inductiveautomation.com/t/basicalarmevent-to-pyalarmevent/32949/9 "2020-03-10T12:47:28Z")

</div>

Self is not defined error on line 11

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 10, 2020, 12:48pm UTC](https://forum.inductiveautomation.com/t/basicalarmevent-to-pyalarmevent/32949/10 "2020-03-10T12:48:47Z")

</div>

Fix the indentation on line 11.

---

<div class="post-metadata">

**Author:** ![jramirez3](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jramirez3](https://forum.inductiveautomation.com/u/jramirez3)\
**Post date:** [March 10, 2020, 1:06pm UTC](https://forum.inductiveautomation.com/t/basicalarmevent-to-pyalarmevent/32949/11 "2020-03-10T13:06:04Z")

</div>

Its intended to be in the WrappedAlarmEvent class, then intended once under the _init_ function.

---

<div class="post-metadata">

**Author:** ![jramirez3](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jramirez3](https://forum.inductiveautomation.com/u/jramirez3)\
**Post date:** [March 10, 2020, 1:08pm UTC](https://forum.inductiveautomation.com/t/basicalarmevent-to-pyalarmevent/32949/12 "2020-03-10T13:08:42Z")

</div>

I moved line 11 to line 10, then hit enter and now its working. I guess it did not see the indentation correctly. Thank you to all of you.

---

<div class="post-metadata">

**Author:** ![nideyijuyidong](https://avatars.discourse-cdn.com/v4/letter/n/7cd45c/32.png) [@nideyijuyidong](https://forum.inductiveautomation.com/u/nideyijuyidong)\
**Post date:** [April 28, 2021, 8:32am UTC](https://forum.inductiveautomation.com/t/basicalarmevent-to-pyalarmevent/32949/13 "2021-04-28T08:32:00Z")

</div>

Excuse me, why there is no getEventTime() method about BasicAlarmEvent? I want to build my own AlarmStatusTable, but can’t get eventtime from AlarmEventObject.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 28, 2021, 4:09pm UTC](https://forum.inductiveautomation.com/t/basicalarmevent-to-pyalarmevent/32949/14 "2021-04-28T16:09:19Z")

</div>

The alarm event doesn’t directly keep track of the event time. Instead you get the `EventData` from whatever state the alarm is in:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/8/c82b60ac4284ca710f9104903f990363153344ca.png)  
And then `EventData` has a `getTimestamp` method that returns a long timestamp.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 16, 2021, 3:24pm UTC](https://forum.inductiveautomation.com/t/basicalarmevent-to-pyalarmevent/32949/15 "2021-11-16T15:24:56Z")

</div>

For anyone reading this in the future; in 8.1.11 we changed things around internally so every `AlarmEvent` you get in scripting will automatically be a `PyAlarmEvent` that does the wrapping here automatically.
