Emailing Variable State in the Subject Line

Posted on
Sun Aug 31, 2014 9:48 am
DU Lou offline
Posts: 279
Joined: Mar 08, 2012
Location: Florida

Emailing Variable State in the Subject Line

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

Posted on
Sun Aug 31, 2014 10:06 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Emailing Variable State in the Subject Line

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.
Last edited by FlyingDiver on Mon Sep 01, 2014 7:41 am, edited 1 time in total.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Aug 31, 2014 10:10 am
matt (support) offline
Site Admin
User avatar
Posts: 21411
Joined: Jan 27, 2003
Location: Texas

Re: Emailing Variable State in the Subject Line

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.

Image

Posted on
Sun Aug 31, 2014 12:40 pm
DU Lou offline
Posts: 279
Joined: Mar 08, 2012
Location: Florida

Re: Emailing Variable State in the Subject Line

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

Posted on
Sun Aug 31, 2014 12:56 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Emailing Variable State in the Subject Line

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.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Sep 01, 2014 4:55 am
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Emailing Variable State in the Subject Line

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.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Dec 30, 2014 4:38 pm
DU Lou offline
Posts: 279
Joined: Mar 08, 2012
Location: Florida

Re: Emailing Variable State in the Subject Line

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

Posted on
Tue Dec 30, 2014 4:58 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Emailing Variable State in the Subject Line

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.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue Dec 30, 2014 5:09 pm
DU Lou offline
Posts: 279
Joined: Mar 08, 2012
Location: Florida

Re: Emailing Variable State in the Subject Line

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 5700 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 5700 times

Posted on
Tue Dec 30, 2014 5:12 pm
DU Lou offline
Posts: 279
Joined: Mar 08, 2012
Location: Florida

Re: Emailing Variable State in the Subject Line

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

Posted on
Tue Dec 30, 2014 5:17 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Emailing Variable State in the Subject Line


joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue Dec 30, 2014 5:26 pm
DU Lou offline
Posts: 279
Joined: Mar 08, 2012
Location: Florida

Re: Emailing Variable State in the Subject Line

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 5690 times

Posted on
Tue Dec 30, 2014 5:30 pm
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Emailing Variable State in the Subject Line

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

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Dec 30, 2014 5:50 pm
DU Lou offline
Posts: 279
Joined: Mar 08, 2012
Location: Florida

Re: Emailing Variable State in the Subject Line

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
Last edited by DU Lou on Tue Dec 30, 2014 6:09 pm, edited 1 time in total.

Posted on
Tue Dec 30, 2014 6:05 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Emailing Variable State in the Subject Line

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.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Who is online

Users browsing this forum: No registered users and 3 guests