Get Source of Each Alarm from QueryStatus

I've got this script in the console that gets me what I want, but I don't want to just print it, I want to store it in a list of strings.

#get the list of all acknowledged alarms
alms = system.alarm.queryStatus(state=["ActiveAcked"])

#initialize empty list of sources
sources = []

#active ack'd alarms count is non-zero
if alms is not None:
	#for each alarm, get the source path and add it to the list
	for alm in alms:
		print alm.source

but whenever I try to cast the source path to a string or assign it to a variable I get the error NameError: name 'source' is not defined

#get the list of all acknowledged alarms
alms = system.alarm.queryStatus(state=["ActiveAcked"])

#initialize empty list of sources
sources = []

#active ack'd alarms count is non-zero
if alms is not None:
	#for each alarm, get the source path and add it to the list
	for alm in alms:
		sources.append(str(alm.source))

Of course now that I post it here I don't get an error...

This is a classic case for a list comprehension, for what it's worth:

sources = [str(alm.source) for alm in system.alarm.queryStatus(state=["ActiveAcked"])]
1 Like

Getting better at list comprehension, but I don't always spot it right out of the gate.

How does this handle the null list?

Can queryStatus return null? Most of our functions that return a list are pretty good about returning an empty list if there's no values, not an outright null.

You're right that my implementation wouldn't handle it if it were somehow null.

1 Like

Great question, not sure but I have that catch in there to make sure if there ever was a null that I'd get an empty list instead. I noticed that a new string array tag had value null, so I just reminded me to check for that.

Since this is a query, is there a way to order the results outside of doing it in python after I have the list? And on that note, how is the order determined?

I ask because there's a button on the screen that stores the current list of Active and Acknowledged alarms and I want to be able to compare that stored list against the active list and I need to know when there's a difference. Right now I'm using

ackdALMs,knownAckdALMs = system.tag.readBlocking(['[default]Alarms/Remote Reset/ackdALMs','[default]Alarms/Remote Reset/ackdALMs_known'])
	return not ackdALMs.value == knownAckdALMs.value

but that may not work if the order is not always the same