# Query alarm journal to display alarm event data in a single row

**URL:** <https://forum.inductiveautomation.com/t/query-alarm-journal-to-display-alarm-event-data-in-a-single-row/69016>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [January 25, 2023, 5:08pm UTC](https://forum.inductiveautomation.com/t/query-alarm-journal-to-display-alarm-event-data-in-a-single-row/69016 "2023-01-25T17:08:41Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josh\_Glass](https://avatars.discourse-cdn.com/v4/letter/j/919ad9/32.png) [@Josh\_Glass](https://forum.inductiveautomation.com/u/Josh_Glass)\
**Post date:** [January 25, 2023, 5:08pm UTC](https://forum.inductiveautomation.com/t/query-alarm-journal-to-display-alarm-event-data-in-a-single-row/69016/1 "2023-01-25T17:08:41Z")

</div>

I've been working on a report that can query the alarm journal for a given period of time and return the alarms for a given period of time to show in 1 row the active time, active value, ack user, ack notes, ack time, and clear time. The way I would like the script to work is if there was a clear or ack event that occurred during the given time period I also want to provide the prior information for that alarm (so for example provide the active information if the alarm was cleared during the time period)

I've tried following the below forum posts but still can't seem to get my script to work.

- [Creating report from alarm data - getting user who acknowledged alarm and when? - Ignition - Inductive Automation Forum](https://forum.inductiveautomation.com/t/creating-report-from-alarm-data-getting-user-who-acknowlegded-alarm-and-when/60450/6)
- [Getting the Initial Value that Triggered an Alarm - Ignition - Inductive Automation Forum](https://forum.inductiveautomation.com/t/getting-the-initial-value-that-triggered-an-alarm/53820/6)

My script currently is as follows. Any suggestions?:

```auto
from com.inductiveautomation.ignition.common.alarming.config import CommonAlarmProperties
from com.inductiveautomation.ignition.common.alarming import AlarmState, AlarmStateTransition, PyAlarmEvent
from com.inductiveautomation.ignition.common.alarming.evaluation import EventProperty

# Queries the alarm journal based on the parameters provided
journalResults = system.alarm.queryJournal(includeData=True)

# Iterates through the journal results to determine the eventid, source, tag, label, priority, active, ack, and clear data
data = []
for x in journalResults:
      data.append({
            'source':x.getSource(),
            'priority':x.getPriority().getIntValue(),
            'label':x.getLabel(),
            'activeTime':x.getActiveData(),
            'value':x.get(CommonAlarmProperties.EventValue),
            'ackTime':x.getAckData(),
            'ackUser':x.get(CommonAlarmProperties.AckUserName),
            'ackNotes':x.get(CommonAlarmProperties.AckNotes),
            'clearTime':x.getClearedData()            
      })

```

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [January 25, 2023, 5:11pm UTC](https://forum.inductiveautomation.com/t/query-alarm-journal-to-display-alarm-event-data-in-a-single-row/69016/2 "2023-01-25T17:11:25Z")

</div>

What are you trying to do with the data? If this is a data source for a report I think you are just missing `return data` at the end.

---

<div class="post-metadata">

**Author:** ![Josh\_Glass](https://avatars.discourse-cdn.com/v4/letter/j/919ad9/32.png) [@Josh\_Glass](https://forum.inductiveautomation.com/u/Josh_Glass)\
**Post date:** [January 25, 2023, 5:18pm UTC](https://forum.inductiveautomation.com/t/query-alarm-journal-to-display-alarm-event-data-in-a-single-row/69016/3 "2023-01-25T17:18:58Z")

</div>

I'm formatting data into a dataset, but didn't post that part because I'm confident in that piece. The reason my script does not work is because depending on the event status that is returned from system.alarm.queryJournal. So for example if I were to change my script to the following each row would print "None"

```auto
# Queries the alarm journal based on the parameters provided
journalResults = system.alarm.queryJournal(includeData=True)

# Iterates through the journal results to determine the eventid, source, tag, label, priority, active, ack, and clear data
for x in journalResults:
	if x.getLastEventState().getIntValue() != 0:
		print x.getActiveData()

```

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [January 25, 2023, 7:44pm UTC](https://forum.inductiveautomation.com/t/query-alarm-journal-to-display-alarm-event-data-in-a-single-row/69016/4 "2023-01-25T19:44:59Z")

</div>

~~There does seem to be a flaw in [the documentation](https://docs.inductiveautomation.com/display/DOC81/system.alarm.queryStatus#system.alarm.queryStatus-StateValues) relating to alarm states.  
 [The documentation](https://docs.inductiveautomation.com/display/DOC81/system.alarm.queryStatus#system.alarm.queryStatus-StateValues) clearly depicts the following integer representations of alarm states:~~  
_Original picture no longer applicable_

...but try the following experiment with a filterAlarm extension function of the alarm status table:

```auto
#def filterAlarm(self, alarmEvent):
	state = alarmEvent.getLastEventState().getIntValue()
	if state == 0:
		return True
	else:
		return False

```

If the state number is incremented by one, the table result will follow this pattern:

```auto
#0 = Active Unacked
#1 = Cleared Unacked and Cleared Acked
#2 = Active Acked
#3 = None

```

In another experiment, I can place a power table underneath an alarm status table, and populate the alarm status table with the following script:

```auto
dataset = system.alarm.queryStatus().getDataset()
event.source.parent.getComponent('Power Table').data = dataset

```

~~The result seems to indicate the following which is also contradictory to [the documentation](https://docs.inductiveautomation.com/display/DOC81/system.alarm.queryStatus#system.alarm.queryStatus-StateValues):~~

```auto
#0 = Active Unacked
#1 = Cleared Acked
#2 = Active Acked
#3 = Active Acked

```

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/0/901ccac602792882e818e51e98be23842a0ae35d.png)

**Edit:** [The documentation](https://docs.inductiveautomation.com/display/DOC81/system.alarm.queryStatus#system.alarm.queryStatus-StateValues) has been corrected.

---

<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:** [January 25, 2023, 8:27pm UTC](https://forum.inductiveautomation.com/t/query-alarm-journal-to-display-alarm-event-data-in-a-single-row/69016/5 "2023-01-25T20:27:18Z")

</div>

`getLastEventState` returns an [`AlarmStateTransition`](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.24/com/inductiveautomation/ignition/common/alarming/AlarmStateTransition.html), where `getIntValue()` is just an alias for [`Enum.ordinal()`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Enum.html#ordinal()), so:

| String | Int |
| --- | --- |
| Ack | 0 |
| Active | 1 |
| Clear | 2 |
| Disabled | 3 |
| Enabled | 4 |
| Finished | 5 |

Whereas the state field in the state model is an [`AlarmState`](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.24/com/inductiveautomation/ignition/common/alarming/AlarmState.html), with hardcoded integer values:

| String | Int |
| --- | --- |
| ClearUnacked | 0 |
| ClearAcked | 1 |
| ActiveUnacked | 2 |
| ActiveAcked | 3 |
| Enabled | 4 |
| Disabled | 5 |

Those values are also those used to pass in to the query functions, and they haven't changed in over a decade, so I'm not sure where the ones in the manual came from. I'll contact the documentation team.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [January 25, 2023, 8:29pm UTC](https://forum.inductiveautomation.com/t/query-alarm-journal-to-display-alarm-event-data-in-a-single-row/69016/6 "2023-01-25T20:29:19Z")

</div>

Thanks. Earlier, I sent them an email with a link to this post.

---

<div class="post-metadata">

**Author:** ![Josh\_Glass](https://avatars.discourse-cdn.com/v4/letter/j/919ad9/32.png) [@Josh\_Glass](https://forum.inductiveautomation.com/u/Josh_Glass)\
**Post date:** [January 25, 2023, 8:40pm UTC](https://forum.inductiveautomation.com/t/query-alarm-journal-to-display-alarm-event-data-in-a-single-row/69016/7 "2023-01-25T20:40:19Z")

</div>

So @paul-griffith would you recommend I use [`AlarmStateTransition`](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.24/com/inductiveautomation/ignition/common/alarming/AlarmStateTransition.html) or [`AlarmState`](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.24/com/inductiveautomation/ignition/common/alarming/AlarmState.html) for obtaining the alarm event information I want?

Based on your [post](https://forum.inductiveautomation.com/t/getting-the-initial-value-that-triggered-an-alarm/53820/8) it looks like I should be able to get the information for the sourceEvent. However, when I've tried it on my end I get errors.

---

<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:** [January 25, 2023, 8:50pm UTC](https://forum.inductiveautomation.com/t/query-alarm-journal-to-display-alarm-event-data-in-a-single-row/69016/8 "2023-01-25T20:50:54Z")

</div>

Each alarm _event_ from the journal is independent. You will have to manually join them by the event ID.  
Once you do, for _a particular alarm event_, you can get at whatever associated data it has. You will want to retrieve based on the `getLastEventState` (note the documentation on `AlarmEvent`, below):

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/b/0b0ce70fc5e73940d07276ecd9cd19478124f843.png)

You could "automatically" retrieve the correct EventData with dynamic dispatch, something like this:

```auto
from com.inductiveautomation.ignition.common.alarming import AlarmStateTransition

dataGetters = {
	AlarmStateTransition.Ack: lambda e: e.ackData,
	AlarmStateTransition.Active: lambda e: e.activeData,
	AlarmStateTransition.Clear: lambda e: e.clearedData,
}

stateData = dataGetters[event.lastEventState]()

```

---

<div class="post-metadata">

**Author:** ![Josh\_Glass](https://avatars.discourse-cdn.com/v4/letter/j/919ad9/32.png) [@Josh\_Glass](https://forum.inductiveautomation.com/u/Josh_Glass)\
**Post date:** [January 25, 2023, 9:40pm UTC](https://forum.inductiveautomation.com/t/query-alarm-journal-to-display-alarm-event-data-in-a-single-row/69016/9 "2023-01-25T21:40:12Z")

</div>

I tried to do the following but got an error that says lambda() takes exactly 1 argument (0 given)

This is what I did;

```auto
from com.inductiveautomation.ignition.common.alarming import AlarmStateTransition

# Queries the alarm journal based on the parameters provided
journalResults = system.alarm.queryJournal(includeData=True)

# Iterates through the journal results to determine the eventid, source, tag, label, priority, active, ack, and clear data
for x in journalResults:
	dataGetters = {
		AlarmStateTransition.Ack: lambda e: e.ackData,
		AlarmStateTransition.Active: lambda e: e.activeData,
		AlarmStateTransition.Clear: lambda e: e.clearedData,
	}

	stateData = dataGetters[x.lastEventState]()

```

I'm not sure if this will get what I want. I want to iterate through the results of system.alarm.queryJournal and if the AlarmStateTransition is not Active then I'd like to query the active time. I know I can make a SQL query in the DB to get that information, just it slows down the script to minutes. Any other ideas?

---

<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:** [January 25, 2023, 9:44pm UTC](https://forum.inductiveautomation.com/t/query-alarm-journal-to-display-alarm-event-data-in-a-single-row/69016/10 "2023-01-25T21:44:05Z")

</div>

Oh, right, the last line needs to be:  
`stateData = dataGetters[x.lastEventState](x)`

> [@Josh\_Glass](#):
>
> I want to iterate through the results of system.alarm.queryJournal and if the AlarmStateTransition is not Active then I'd like to query the active time.

That's, fundamentally, just not how the alarm journal works. The information that is logged at a particular state transition is _only_ the event that pertains to that transition. So when an alarm goes active, it logs why it went active... but when it clears, it doesn't "know" why it was active (from the journal's perspective). So you _have to_, there is no way around this, group the individual events you get back from the journal back together by event ID if you want to get information about the active state from the clear, for instance. If the active event doesn't fall within the time slice you're querying for, you might not get it in the results, as well.

At least, such is my recollection of how the journal works. I could be wrong.

---

<div class="post-metadata">

**Author:** ![jsorlie](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jsorlie/32/3645_2.png) [@jsorlie](https://forum.inductiveautomation.com/u/jsorlie)\
**Post date:** [July 11, 2024, 7:16pm UTC](https://forum.inductiveautomation.com/t/query-alarm-journal-to-display-alarm-event-data-in-a-single-row/69016/11 "2024-07-11T19:16:00Z")

</div>

@paul-griffith  
For AlarmStateTransition, the enumeration is slightly different from what you posted. Running the following code in script console yields different results...

```python
print '--- enumerate(AlarmStateTransition.values()'
from com.inductiveautomation.ignition.common.alarming import AlarmStateTransition
#print AlarmStateTransition.values()
for i,s in enumerate(AlarmStateTransition.values()):
    print i, s
# --- enumerate(AlarmStateTransition.values()
# 0 Active
# 1 Clear
# 2 Ack
# 3 Finished
# 4 Enabled
# 5 Disabled
# 6 Shelved

print '--- i, AlarmStateTransition.fromIntValue(i)'
for i in range(7):
    print i, AlarmStateTransition.fromIntValue(i)
# --- i, AlarmStateTransition.fromIntValue(i)
# 0 Active
# 1 Clear
# 2 Ack
# 3 Finished
# 4 Enabled
# 5 Disabled
# 6 Shelved

print '--- Enum values: i, AlarmStateTransition'
print AlarmStateTransition.Ack.ordinal(), AlarmStateTransition.Ack
print AlarmStateTransition.Active.ordinal(), AlarmStateTransition.Active
print AlarmStateTransition.Clear.ordinal(), AlarmStateTransition.Clear
print AlarmStateTransition.Disabled.ordinal(), AlarmStateTransition.Disabled
print AlarmStateTransition.Enabled.ordinal(), AlarmStateTransition.Enabled
print AlarmStateTransition.Finished.ordinal(), AlarmStateTransition.Finished
print AlarmStateTransition.Shelved.ordinal(), AlarmStateTransition.Shelved
# --- Enum values: i, AlarmStateTransition
# 2 Ack
# 0 Active
# 1 Clear
# 5 Disabled
# 4 Enabled
# 3 Finished
# 6 Shelved

```
