Email on sensor change

I just modified a door sensor and put it in my mailbox to monitor open close activity (trying to catch some thiefs on my busy street). I had an email test application that I strung with a wizard script and ended up spamming my cell phone SMS.

Does anyone have a simple program to send an email on sensor change they could share with the class?

what code are you currently using - can you post it and we should be able to help

I have a simple send email line in the startup tab but it repeats over and over because that code is looping apparently.

I’d rather just have a check for sensor value change but I haven’t done this yet. Better yet I’d rather make a program where i can put a check mark on the sensor to receive update notifications via email and in the program specify which kinds of updates, battery alerts, etc…

I’ve never done any of this so getting started with it is the hard part but I know how to do basic programming. Startup tells me it runs once but apparently that isn’t true.

I think this video will help me accomplish my goal.
CODE: http://efficienthacks.com/homegenieprogram/

I’m not getting the options dialog loading like in the video though. Any ideas why? Nothing is ever as simple as I think it’s going to be sigh.

I believe that you should place the startup code into “Startup code” tab.
So in “Startup code” tab you have

Program.AddInputField("Sensor", "garage", "Sensors to alert on (comma separated)"); //e.g.: garage,window1
Program.AddInputField("Email.Recipients", "[email protected]", "Email recipients");	    //e.g.: [email protected],[email protected]

and the rest of the code in the “Program code” tab:

//Code that executes on sensor changes:
When.ModuleParameterChanged((module, parameter)=> {
    ...
});

Genius! Thanks Bounz.

Boo it’s not working :frowning:

-edit- Got it. Guess the sensor isn’t using Sensor.Generic like I thought I saw. All good now.

1 Like

Might be worth creating another repo on gihub where snippets can be posted @Bounz ?

Then bits like this can be added there as hgx?

We can create snippets here in themes, on GitHub and as a .hgx packages… I don’t know what method is better.
But all these possibilities will increase fragmentation level.

personally I think github is better as Easier to track changes but… whatever :slight_smile:

I don’t know why I have so much trouble with HG on PC anymore. The program just won’t work when I restart the service. I have to add a line of code to send an email within the first if statement and trigger the sensor to send an email and then remove the code I added for it to actually work on it’s own. Restarting the service resets whatever that accomplishes and I have to do the process over again. I guess this is a bug? Not sure what you want to try and track down the cause :\

can you paste the full code of you program on here?

Also try adding a program.notify(“MyApp”, “Sensor change blah”); prior to the send email command… to see if its the email side that isn’t working, ie Get the program working and then look at whether its the email…

Either way should be an easy enough fix :slight_smile:

//Code that executes on sensor changes:
When.ModuleParameterChanged((module, parameter)=> {
  if( parameter.Name == "Sensor.Alarm")
  {
    //Net.SendMessage("**EMAIL**", "test", "test");  //Insert to get this code to work then remove
    if(Program.InputField("Sensor").Value.Contains(module.Instance.Name))
    {     
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
      DateTime time = DateTime.Now;
      string format = "HH:mm MMM ddd d"; 
  
      var openClosed = parameter.DecimalValue == 0 ? "closed":"open";
      var messagetext = module.Instance.Name + " " + openClosed + time.ToString(format);
      Program.Notify("debug", "sending email " + messagetext); //uncomment for debugging
      var subject = "HomeGenie: Open/Closed" + messagetext;
      var recipients = Program.InputField("Email.Recipients").Value;
      if (recipients != "")
      {
        Program.RunAsyncTask(()=>{
        Net.SendMessage(recipients, subject, messagetext);
      });
    }
  }
}

return true;
});

I’d refactor it slightly - also the formatting for the braces is wrong (I really hate the tab size of 2 in homegenie… needs to be 4!!)

anyway - untested but I would try something like this, and I don’t know why you needed to run that step async…

if (String.IsNullOrEmpty(Program.InputField("Email.Recipients").Value))
{
    Program.Notify("Sensor Alarm","Recipient email address not configured");
    // Add code to quit here..
}

// Code that executes on sensor changes:
When.ModuleParameterChanged((module, parameter)=> {
    if (parameter.Name == "Sensor.Alarm")
    {
        if (Program.InputField("Sensor").Value.Contains(module.Instance.Name))
        {
            var openClosedState = parameter.DecimalValue == 0 ? "closed":"open";
            var messagetext = $"{module.Instance.Name} {openClosedState} {DateTime.Now.ToString("HH:mm MMM ddd d")}";

            try
            {
                Net.SendMessage(
                    Program.InputField("Email.Recipients").Value,
                    $"HomeGenie: {module.Instance.Name} {openClosedState}",
                    messagetext
                );
            }
            catch (System.Exception ex)
            {
               Program.Log.Error($"Sensor Alarm: Unable to send mail {ex.Message}");
            }
        }
    }

    return true;
});

Thanks for that I’ll try it later today.
How do you post code here? I was using the 4 spaces method.

Take a look at this article about formatting code in Discourse: https://discourse.stonehearth.net/t/discourse-guide-code-formatting/30587.
I usually use Block code formatting method.

Doesn’t like the $ symbols

This is string interpolation and it has been introduced in C# 6.
You can always continue to use string.Format if you don’t like the new syntax.

what version of mono are you running?

for formatting I just did three back ticks then the word csharp… infact hang on… a pic…

My code example

normal text again