Page 1 of 2

Emailing Variable State in the Subject Line

PostPosted: Sun Aug 31, 2014 9:48 am
by DU Lou
I just attempted to setup a trigger to email me when a variable state changes. I typed "Kitchen Motion Sensor Battery %%v:741338509%%" in the subject line and when I tested it I received an email with exactly "Kitchen Motion Sensor Battery %%v:741338509%%" in the subject field. :)

Is what I am trying to accomplish even possible and if so whats the correct way to go about it.

Thanks All,
~Lou

Re: Emailing Variable State in the Subject Line

PostPosted: Sun Aug 31, 2014 10:06 am
by FlyingDiver
Exactly how are you sending the email? I'm doing it from a Python script, as follows:

Code: Select all
wsStates = indigo.devices[665073353].states # "Cape Coral WU"

def sendAlertEmail(alert, message, expires):
   theSubject = "Cape Coral Weather Alert: %s" % alert
   theBody = "%s\n\nThis weather alert is valid until %s" % (message, expires)
   indigo.server.sendEmailTo("xxx@yyy.zzz", subject=theSubject, body=theBody)
   return

if wsStates[u'alertDescription1'] != ' ':
   sendAlertEmail(wsStates[u'alertDescription1'], wsStates[u'alertMessage1'], wsStates[u'alertExpires1'])



I'm using device state info, but variable values would essentially be the same. The only place I've ever used the %%v: notation is for writing to the event log.

Re: Emailing Variable State in the Subject Line

PostPosted: Sun Aug 31, 2014 10:10 am
by matt (support)
That variable substitution syntax only works in plugin based UI. There isn't an easy way to know this, but the email action isn't based on a plugin (unlike, for example, the Growl notification UI).

So, we need to add variable substitution to the non-plugin UI. :-)

In the mean time, take a look at this wiki article which should give you a way to do it. We really need to re-write that wiki article to show a python example as well since you can have Indigo send an email via python now (as well as AppleScript).

Edit: I recommend using FlyingDiver's technique above over the wiki AppleScript example I linked to.

Re: Emailing Variable State in the Subject Line

PostPosted: Sun Aug 31, 2014 12:40 pm
by DU Lou
Hi Matt,
I was able to come up with a simple solution based on the wiki you provided:
Code: Select all
property emailAddress : "ljw@yahoo.com"
property emailSubject : "Kitchen Motion Battery @ "

tell application "IndigoServer"
   set emailSubject to emailSubject & value of variable "motionBatteryLevelKitchen"
   send email to emailAddress with subject emailSubject
end tell


...and accomplishes exactly what I need. Any reason why FlyingDiver's solution is the recommended approach?

FlyingDiver,
Thank you for the contribution as well! I will take a closer look to see if I understand what is going on with your code a little later.

~Lou

Re: Emailing Variable State in the Subject Line

PostPosted: Sun Aug 31, 2014 12:56 pm
by FlyingDiver
DU Lou wrote:
FlyingDiver,
Thank you for the contribution as well! I will take a closer look to see if I understand what is going on with your code a little later.


The non-obvious part about mine is that it's using a subroutine to actually send the message. That's because the action it's defined in can send multiple emails, depending on the values of some of the device states. So I used a subroutine to reduce the amount of code written.

I think Matt's point may be that using Python scripts rather than AppleScripts is more flexible and efficient.

Re: Emailing Variable State in the Subject Line

PostPosted: Mon Sep 01, 2014 4:55 am
by DaveL17
Plus, if memory serves, you can't use ID number in AppleScript (references are item name only I believe). In Python, you can use ID--which will keep your code from breaking if you ever change the variable's name later on.

Re: Emailing Variable State in the Subject Line

PostPosted: Tue Dec 30, 2014 4:38 pm
by DU Lou
I have a nightly schedule run that emails me the battery level of my motion sensors when they hit less than 20% but it looks like Indigo just keeps appending the value every subsequent email so I finally am getting an email with this in the subject line :
Code: Select all
Foyer Motion Battery @ 1918181919191816151210975432200000000000000000000000000000000000000000000

I think I need to reset my emailSubject variable but I am not sure how to do it.
What do I need to add here to get just one value every night?
Code: Select all
property emailAddress : "abc@123.com"
property emailSubject : "Foyer Motion Battery @ "

tell application "IndigoServer"
   set emailSubject to emailSubject & value of variable "motionBatteryLevelFoyer"
   send email to emailAddress with subject emailSubject
end tell

Thanks,
~Lou

Re: Emailing Variable State in the Subject Line

PostPosted: Tue Dec 30, 2014 4:58 pm
by FlyingDiver
DU Lou wrote:
I have a nightly schedule run that emails me the battery level of my motion sensors when they hit less than 20% but it looks like Indigo just keeps appending the value every subsequent email so I finally am getting an email with this in the subject line :
Code: Select all
Foyer Motion Battery @ 1918181919191816151210975432200000000000000000000000000000000000000000000

I think I need to reset my emailSubject variable but I am not sure how to do it.
What do I need to add here to get just one value every night?
Code: Select all
property emailAddress : "abc@123.com"
property emailSubject : "Foyer Motion Battery @ "

tell application "IndigoServer"
   set emailSubject to emailSubject & value of variable "motionBatteryLevelFoyer"
   send email to emailAddress with subject emailSubject
end tell

Thanks,
~Lou


We need to see the code that's setting the variable "motionBatteryLevelFoyer". That's where the appending is done. You would probably be better off using the sensor property value rather than a variable.

Re: Emailing Variable State in the Subject Line

PostPosted: Tue Dec 30, 2014 5:09 pm
by DU Lou
Thats just a trigger that I have setup that inserts the battery state when any battery state change is detected:
Screen Shot 2014-12-30 at 6.03.51 PM.png
Screen Shot 2014-12-30 at 6.03.51 PM.png (42.87 KiB) Viewed 5770 times

The Variable ID 1373479876 is the variable named "motionBatteryLevelFoyer"
Screen Shot 2014-12-30 at 6.07.50 PM.png
Screen Shot 2014-12-30 at 6.07.50 PM.png (22.98 KiB) Viewed 5770 times

Re: Emailing Variable State in the Subject Line

PostPosted: Tue Dec 30, 2014 5:12 pm
by DU Lou
FlyingDiver wrote:
You would probably be better off using the sensor property value rather than a variable.

I am game. The device ID for the Foyer motion sensor is 386482511. How would I write a script to email the battery state of that device?

Thanks,
~Lou

Re: Emailing Variable State in the Subject Line

PostPosted: Tue Dec 30, 2014 5:17 pm
by FlyingDiver

Re: Emailing Variable State in the Subject Line

PostPosted: Tue Dec 30, 2014 5:26 pm
by DU Lou
I am reviewing that topic now:
# Get the state values you want to use - you can find the state IDs by selecting the device in the device list of
# the client then expanding the control area below the list until you see the list of custom states.

Is the "state ID" just "Battery Level"?
Screen Shot 2014-12-30 at 6.23.00 PM.png
Screen Shot 2014-12-30 at 6.23.00 PM.png (31.2 KiB) Viewed 5760 times

Re: Emailing Variable State in the Subject Line

PostPosted: Tue Dec 30, 2014 5:30 pm
by DaveL17
That is just for custom devices that have their own states. To find the state name of "stock" devices, I find the easiest thing to do is to write the device properties to the log and then copy the state name from there. Put this code (adjusted for your device ID number) into an embedded script and run it:

Code: Select all
indigo.server.log(unicode(indigo.devices[12345678]))


Dave

Re: Emailing Variable State in the Subject Line

PostPosted: Tue Dec 30, 2014 5:50 pm
by DU Lou
Ok so I was able to come up with this:
Code: Select all
myFoyer = indigo.devices[386482511]
batteryLevel = myFoyer.states["batteryLevel.ui"]
theSubject = “Foyer battery level is” % (batteryLevel)
indigo.server.sendEmailTo(“abc@123.com", subject=theSubject)

But I am getting a syntax error on this line
Code: Select all
theSubject = “Foyer battery level is” % (batteryLevel)

Please bare with me as I am trying to learn :oops:

Thanks,
~Lou

Re: Emailing Variable State in the Subject Line

PostPosted: Tue Dec 30, 2014 6:05 pm
by FlyingDiver
Needs to be:

Code: Select all
theSubject = “Foyer battery level is %s” % (batteryLevel)


The "%s" says "put a string value here", and it gets that value from the variable in the parenthesis.