Alarm query status write to text file

When i run the following i get the output i require from print. Then i am trying ti insert the results into my create file. I can input a property.text value in there and it works fine or just 'string' but when i try to call the results in there it just dosen't do anything.

alarms = system.alarm.queryStatus(state=["ActiveUnacked", "ClearAcked"])

for alarm in alarms:
print alarm.getName(), alarm.getState()

result

Dryer 7 Temp low Cleared, Acknowledged
Dryer 10 Temp High Cleared, Acknowledged
Dryer 5 Burner Fault Cleared, Acknowledged

when i run this
alarms = system.alarm.queryStatus(state=["ActiveUnacked", "ClearAcked"])
for alarm in alarms:
alarm1 = alarm.getName(), alarm.getState()

my_file = open('D:\send_file3.txt','w')
my_file.write('from call import Client\n')
my_file.write('account_sid = "test"\n')
my_file.write('auth_token = "test"\n')
my_file.write('client = Client(account_sid, auth_token)\n')
my_file.write('message = client.api.account.messages.create(to="+6140000000", from_="+614000000000", body=" ')
my_file.write(alarm1)
my_file.write(' ")')
my_file.close()

text file output

it writes all lines to
body="

then blank. If i change alarm1 to 'hello'

i get body="hello ").

What i want to achieve is
body="Dryer 7 Temp low Cleared, Acknowledged Dryer 10 Temp High Cleared, Acknowledged Dryer 5 Burner Fault Cleared, Acknowledged"

Any help would be appreciated.

Make alarm1 a string?

my_file.write(str(alarm1))

It’s hard to follow the code since it’s not formatted (need to use triple-backquotes before and after), but the for loop needs to include my_file.write(str(alarm1)). As shown, alarm1 only has the last alarm object when written out.

hi. i ended up doing the following

alarms = system.alarm.queryStatus(state=[])
for alarm in alarms:
      msg = '%s %s' % (alarm.getName(), alarm.getState())

And gave me the output i required in my write file.
i didn’t get to try str(alarm1)

'%s' % value means to format the value as a string. So it’s exactly the same as str(value), but I guess str(value) is more readable for a single value, and the %s substitutions are better when formatting longer strings (like you formatted the string with the space).

1 Like