Email on sensor change

I’m a little confused. What should I be using?

if you mean confused with regards to the code and string interpolation isn’t working in your version of mono then you could try this:

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 = string.Format("{0} {1} {2}", module.Instance.Name, openClosedState, {DateTime.Now.ToString("HH:mm MMM ddd d"));

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

    return true;
});

Might need uppercase String.Format though - untested

Sorry, I don’t know what mono is. I’m just putting it right into HG. I’ve
never programmed in C# before only .net and Java.

Finally, I got around to this program and fixed the code to make it runnable)
The main issue with previous versions was lack of Program.GoBackground(); command after ModuleParameterChanged event handler.

So, here is my code:

Startup code
Program.AddOption("Sensor","garage","Sensors to alert on (comma separated)","text"); //e.g.: garage,window1
Program.AddOption("Email.Recipients","[email protected]","Email recipients","text");			 //e.g.: [email protected],[email protected]
Program.Run();
Program code
//Code that executes on sensor changes:
When.ModuleParameterChanged((module, parameter) =>
{
    if (Program.Option("Sensor").Value.Contains(module.Instance.Name) &&
        parameter.Name == "Status.Level") //Sensor.Alarm
    {
        var openClosedState = parameter.DecimalValue == 0 ? "closed" : "open";
        var messagetext = string.Format("{0} {1} {2}", module.Instance.Name, openClosedState, DateTime.Now.ToString("HH:mm MMM ddd d"));

        try
        {
            Net.SendMessage(
                Program.Option("Email.Recipients").Value,
                string.Format("HomeGenie: {0} {1}", module.Instance.Name, openClosedState),
                messagetext
            );
        }
        catch (Exception ex)
        {
            Program.Log.Error(string.Format("Sensor Alarm: Unable to send mail {0}", ex.Message));
        }
    }
    return true;
});

// the program will be running in the background waiting for events
Program.GoBackground();

Perhaps you will have to change Status.Level in parameter.Name == "Status.Level") to Sensor.Alarm or something else (depends on what parameter your device uses to report status, I tested the program with Fibaro motion sensor and it uses Status.Level).

1007-Garage_Door_to_Email.hgx (1.9 KB)

Thanks Bounz it is running. I’m not home right now to actually test it but we will see later today. I really appreciate all the help lately.