Scripting High Wind Warning Alerts

Introduction

Some time ago (way before OH3), I had a DSL rule to send me high wind alerts. The inspiration may have come from this community but I don’t remember now. Since I’m rebuilding my home automation with openHAB 3 from ground zero I decided it was time to revisit this script and learn to use the UI approach with Blockly. I wrote this expecting the user to have some familiarity with openHAB but hopefully there is enough detail for the novice to get started with rules and scripting.

Objective

The purpose of High Wind Warning is to give notice when wind could cause damage to property. Notify the owner or trigger additional automations such as retracting a solar shade that could be damaged.

Functional Requirements

  • Receive input from OpenWeather or local source
  • Send alert to mobile device when threshold is exceeded
  • Send notice when wind speed falls below the threshold
  • Do not continue to send alerts if notice has already been sent
  • Write notification to event log when triggered

Prerequisites

  1. Tested on OpenHAB 3.3.0 Release
  2. Setup persistence mechanism to maintain state of the alert status
  3. Setup preferred notification service. This example leverages Pushover.
  4. Create a switch Item ‘WindAlertStatus’ that indicates current alarm state (On/Off).
  5. Define your source for current wind speed. One option is to use OpenWeatherMap binding’s Local Weather and Forecast. Then, under the Wind Speed channel create Item ‘LocalWeatherandForecast_WindSpeed’.

High-Level Steps

  1. Install OpenWeatherMap Binding and Wind Speed linked item [Documentation]
  2. Install Pushover Binding and configure with your API key [Documentation]
  3. Create Wind Alert Status Item
  4. Create the script
  5. Create the rule
  6. Link script to the rule
  7. Test / Validation

Initial Setup

Create Switch Item: Wind Alert Status item should look something similar to the screen shot below. Go ahead and “initialize” the item by toggling the switch ON then OFF. This will setup the correct initial state.

Add this item to your preferred persistence mechanism to maintain the state of your “switch” after a reboot.

Create Wind Speed Item: Wind Speed after linking a new item to the Wind Speed channel

The Script

  • From the Settings > Scripts menu click the blue + and create a new script using “Design with Blockly”
  • Give it a name such as “High Wind Monitor”
  • Reproduce the blocks as shown in the examples below
  • Or click the “Code View” button and copy & paste the example code, then save

Version 1

Initial version created with Blockly to test rule trigger, getting the item value, and sending Pushover message.

var windSpeed, msgBody;

var things = Java.type('org.openhab.core.model.script.actions.Things');

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);


windSpeed = itemRegistry.getItem('LocalWeatherandForecast_WindSpeed').getState();
if (windSpeed >= 10) {
  msgBody = 'High Wind Warning! ';
  msgBody += String(windSpeed);
  things.getActions(
    'pushover',
    'pushover:pushover-account:*********').sendMessage(
      msgBody,
      'openHAB'
  );
  logger.info(msgBody);
}

Version 2

Enhanced revision to meet all of the functional requirements.

var alertThreshold, windSpeedCurrent, windAlarm, logEntry, msgBody;

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);

var things = Java.type('org.openhab.core.model.script.actions.Things');


alertThreshold = 10;
windSpeedCurrent = itemRegistry.getItem('LocalWeatherandForecast_WindSpeed').getState();
windAlarm = itemRegistry.getItem('WindAlertStatus').getState();
logEntry = 'Running script with windSpeedCurrent: ';
logEntry += String(windSpeedCurrent);
logEntry += ', windAlarm: ';
logEntry += String(windAlarm);
logEntry += ', alertThreshold: ';
logEntry += String(alertThreshold);
logEntry += ' mph.';
logger.info(logEntry);
if (windSpeedCurrent < alertThreshold) {
  if (windAlarm == 'ON') {
    events.sendCommand('WindAlertStatus', 'OFF');
    things.getActions(
      'pushover',
      'pushover:pushover-account:*********').sendMessage(
        'High Wind Warning is now over.',
        'openHAB'
    );
    logger.info('High Wind Warning is now over.');
  }
}
windAlarm = itemRegistry.getItem('WindAlertStatus').getState();
if (windAlarm == 'ON') {
  logger.info('Skipping notification since Wind Alarm is ON');
} else if (windAlarm == 'OFF') {
  logger.info('Wind Alarm is OFF');
  if (windSpeedCurrent >= alertThreshold) {
    msgBody = 'High Wind Warning! ';
    msgBody += String(windSpeedCurrent);
    things.getActions(
      'pushover',
      'pushover:pushover-account:*********').sendMessage(
        msgBody,
        'openHAB'
    );
    logger.info(msgBody);
    events.sendCommand('WindAlertStatus', 'ON');
  }
}

The Rule

Create a Rule to trigger the script whenever the ‘LocalWeatherandForecast_WindSpeed’ is updated.

  1. Choose When an item state changes
  2. Select the wind speed item ‘LocalWeatherandForecast_WindSpeed’
  3. Under the Then section click “Add Action” > Other Rules > run these rule(s) and select the script “High Wind Monitor”

Example:

Rule logging in openHAB Log Viewer (frontail):

2022-11-04 07:25:29.922 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'LocalWeatherandForecast_WindSpeed' changed from 0 mph to 12.661059413027917 mph
2022-11-04 07:25:30.046 [INFO ] [org.openhab.rule.d51c89a003         ] - Running script with windSpeedCurrent: 12.661059413027917 mph, windAlarm: OFF, alertThreshold: 10 mph.
2022-11-04 07:25:30.064 [INFO ] [org.openhab.rule.d51c89a003         ] - Wind Alarm is OFF
2022-11-04 07:25:30.618 [INFO ] [org.openhab.rule.d51c89a003         ] - High Wind Warning! 12.661059413027917 mph
2022-11-04 07:25:30.629 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'WindAlertStatus' received command ON
2022-11-04 07:25:30.636 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'WindAlertStatus' changed from OFF to ON

Pushover

Testing

Test the script logic by modifying the threshold value below the current wind speed. Monitor the event log output to make sense of what is going on.

Example

  1. Edit Blockly alertThreshold value to be less than the current wind speed. Click Run, and observe log output
  2. Click Run again and note that the switch WindAlertStatus is already ON and no alerts are sent
  3. Raise the threshold above current wind speed and confirm warning is reset and switch WindAlertStatus is set to OFF


  • Platform information:
    • Hardware: Raspberry Pi 3 Model B Rev 1.2/1GB RAM/64GB SD
    • OS: openHABian
    • Java Runtime Environment: openjdk 11.0.16 2022-07-19
    • openHAB version: 3.3
1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.