[ANSWERED]: Variables in an email body

Posted on
Sun Jul 27, 2014 5:24 pm
towsled offline
Posts: 32
Joined: Jul 19, 2014

[ANSWERED]: Variables in an email body

Is there an way to embed variables or device states within the body of an automatically triggered email? Say for instance, I wanted to send myself an email if the NOAA weather Plus Plug-in weather alert state changed; and I wanted to include the alert value in the generated email..

Thanks for any help!
Dan..

Posted on
Sun Jul 27, 2014 9:10 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Variables in an email body

Here's a script that sends an email with data from a Weather Underground device. Global variables will work similarly.

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("joe@flyingdiver.com", subject=theSubject, body=theBody)
   return

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

if wsStates[u'alertDescription2'] != ' ':
   sendAlertEmail(wsStates[u'alertDescription2'], wsStates[u'alertMessage2'], wsStates[u'alertExpires2'])

if wsStates[u'alertDescription3'] != ' ':
   sendAlertEmail(wsStates[u'alertDescription3'], wsStates[u'alertMessage3'], wsStates[u'alertExpires3'])

if wsStates[u'alertDescription4'] != ' ':
   sendAlertEmail(wsStates[u'alertDescription4'], wsStates[u'alertMessage4'], wsStates[u'alertExpires4'])

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

Posted on
Mon Jul 28, 2014 9:04 am
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Variables in an email body

Not with the built-in send email action. You can, as FlyingDiver suggests, use a script. I'll simplify and comment heavily here:

Code: Select all
# Get your weather station device - use the ID rather than the name since the name can change.
myWeatherStationDevice = indigo.devices[IDOFWEATHERDEVICE] # insert the device ID between the brackets

# 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.
someState1 = myWeatherStationDevice.states["someStateId"]  # enter the state ID you want to use in the email
someState2 = myWeatherStationDevice.states["someOtherStateId"]  # enter the state ID you want to use in the email

# Subject of the email
theSubject = "Subject of the email goes here"

# Create the body of the email - add %s everywhere you want to do a substitution
# Follow up the body string with a % and a list of values to insert into the string - the list must have the same number
# of items in it as the number of %s values in the string itself.
theBody = "Body of the email goes here. Use 'percent-s' where you want a value entered, like this value: %s. Here's another value: %s." % (someState1, SomeState2)

# Send the email
indigo.server.sendEmailTo("EMAILADDRESSHERE", subject=theSubject, body=theBody)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Jul 28, 2014 9:08 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Variables in an email body

Yeah, mine is a little more complicated than it would otherwise be, but it was the best way to handle the fact that the WU plugin can have as many as four different alerts in the same update. I've never actually seen more than one, but it's possible. The script works well - I have five WU devices (locations) and usually get at least one a day between them all. Mostly thunderstorm or flooding alerts this time of year.

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

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests