Yes, you are right, that is the better solution. I decided to go for the minimum, seems like the most stable option. I see also no problem with running the rule more often, now set it to every five minutes.
Not sure if using a change to the humidity is a good trigger. Say if my threshold is 60 % and it drops from 60 % to 59 % but then does not drop further, the rule would not really work, or would it? The initial change would not lead to an action, because the last hour minimum/maximum is still above/below the threshold, and then there would not be any further change to trigger the rule again.
Anyway, here is the updated script:
/*
Checks humidity values and turns a humidifer on/off if needed
sensorName: name of the Point (type Number) representing the humidity measurement
humidifierName: name of the Point (type Switch) controlling the humidifer
floorShort: short code for the floor/room, for logging and email notification
floorLong: long name of the floor/room, for logging and email notification
lowerThresholdName: name of the Point (type Number) representing the lower threshold, under which the humidfier will be turned on.
lowerThresholdName: name of the Point (type Number) representing the upper threshold, under which the humidfier will be turned off.
thresholdPeriodName: name of the Point (type Number) representing the amount of minutes we check back for the last reading. Only if both current and last reading are above or below the threshold an action will be taken, to avoid unncessary switching if the humidity changes for only s hort period of time, e.g. when the windows are opened.
*/
function controlHumidifier( sensorName, humidifierName, floorShort, floorLong, lowerThresholdName, upperThresholdName, thresholdPeriodName )
{
// Retrieve all the items we need
var sensor = items.getItem( sensorName );
var humidifier = items.getItem( humidifierName );
var lowerThreshold = parseFloat( items.getItem( lowerThresholdName ).state );
var upperThreshold = parseFloat( items.getItem( upperThresholdName ).state );
var lastReadingMinutes = parseFloat( items.getItem( thresholdPeriodName ).state );
// Get humidity values, current and last reading as per the configured minutes interval
var humidity = parseFloat( sensor.state );
var intervalStartDate= new Date();
intervalStartDate.setMinutes( intervalStartDate.getMinutes() - lastReadingMinutes );
var humidityMinimum = parseFloat( sensor.history.minimumSince( intervalStartDate) );
var humidityMaximum = parseFloat( sensor.history.maximumSince( intervalStartDate) );
var consoleMsg = "Humidity " + floorShort + ": " + lastReadingMinutes + "min min=" + humidityMinimum + "%, " + lastReadingMinutes + "min max=" + humidityMaximum + "%, ";
var emailSubj = null;
var emailText = null;
// If current and last reading are above the upper limit and the humidifier is ON, we switch it OFF
if( humidityMinimum > upperThreshold && humidifier.state == "ON" )
{
humidifier.sendCommand( "OFF" );
emailSubj = "Luftfeuchtigkeit " + floorShort + " > " + upperThreshold + " %";
emailText = "Luftfeuchtigkeit im " + floorLong + " ist " + humidity + " %. Luftbefeuchter wurde abgeschalten.";
consoleMsg += "upper threshold=" + upperThreshold + "% => switching OFF humidifier";
console.info( consoleMsg );
}
// If current and last reading are below the lower limit and the humidifier is OFF, we switch it ON
else if( humidityMaximum < lowerThreshold && humidifier.state == "OFF" )
{
humidifier.sendCommand( "ON" );
emailSubj = "Luftfeuchtigkeit " + floorShort + " < " + lowerThreshold + " %";
emailText = "Luftfeuchtigkeit im " + floorLong + " ist " + humidity + " %. Luftbefeuchter wurde angeschalten.";
consoleMsg += "lower threshold=" + lowerThreshold + "% => switching ON humidifier";
console.info( consoleMsg );
}
else
{
consoleMsg += "lower threshold=" + lowerThreshold + "%, upper threshold=" + upperThreshold + "%, humidifer=" + humidifier.state + " => no action required.";
console.info( consoleMsg );
}
// Send out email
if( emailSubj )
{
actions
.get( "mail", "<your-smtp-thing>" )
.sendMail( "<your-email-address>", emailSubj, emailText );
}
}
// Check humidity on ground floor
controlHumidifier
(
"HT_EG_WZ_Sensor_Humidity", "Humidifier_EG_WZ_Power",
"EG", "Erdgeschoss",
"Config_Humidity_EG_Lower_Threshold", "Config_Humidity_EG_Upper_Threshold", "Config_Humidity_EG_Last_Reading_Minutes"
);
// Check humidity on first floor
controlHumidifier
(
"HT_OG_SZ_Sensor_Humidity", "Humidifier_OG_GL_Power",
"OG", "Obergeschoss",
"Config_Humidity_OG_Lower_Threshold", "Config_Humidity_OG_Upper_Threshold", "Config_Humidity_OG_Last_Reading_Minutes"
);