I just updated these instructions for OH3 and have not tested it yet but it should be good. I’m heading out of town for a week and I didn’t want to wait until I was back to post. Note that if you change the blockly script the email command inside of it goes away so you need to reinsert it. It’s this part:
var Actions = Java.type("org.openhab.core.model.script.actions.Things");
var mailActions = Actions.getActions("mail","mail:smtp:SMTP_Server");
mailActions.sendHtmlMail("email@youremail.com", "Alert – openHAB System Offline", "An openHAB system has gone offline. Please check Tailscale to identify offline system.");
Also, you’ll need to change the name “SMTP_Server” to whatever the ID of your SMTP server is. The rule:
Alert – OH System Down
This rule will send an email if an openHAB system is offline for more than 3 hours.
Required: Tailscale installed on system to be monitored, another OH system to monitor IP addresses with Network Binding and Mail Binding installed, and a group (Item) for the OH systems you want to monitor with the aggregation function set to “All ON then ON else OFF”.
Settings > Things > + > Network Binding > Pingable Network Device > ID = Ping_Customername# > Label = Ping – Customername# > Hostname or IP = ip from Tailscale (without :8080) > Refresh Interval = 300000 > Retry = 6 > Create Thing
Settings > Things > Thing just created > Channels > Online (switch) > Add Link To Item > Create a new Item > Name = Ping_Customername# > Label = Ping – Customername# > Parent Group = OH Systems > Close > Link > Save
Rules > New Rule > ID = Alert_System_Offline > Name = Alert – System Offline
Code > Replace existing code with code below:
configuration: {}
triggers:
- id: "1"
configuration:
groupName: OH_Systems
type: core.GroupStateChangeTrigger
conditions: []
actions:
- inputs: {}
id: "2"
configuration:
blockSource: <xml xmlns="https://developers.google.com/blockly/xml"><block
type="oh_log" id="3ntY/cqP4OEGlO7ekWty" x="-2016" y="20"><field
name="severity">info</field><value name="message"><shadow type="text"
id="@OcaG9FOaYg;%cr@X4`X"><field name="TEXT">OH System Offline Rule
Started</field></shadow></value><next><block type="controls_if"
id="4@U|GA#I9vjU{n3s.~8R"><mutation elseif="1"></mutation><value
name="IF0"><block type="logic_compare" id="R{MRikkH@)iW/;AFZj[;"><field
name="OP">EQ</field><value name="A"><block type="oh_getitem_state"
id="G)zY2,`/ZYvEBN]~{%rM"><value name="itemName"><shadow type="oh_item"
id="GaA70K1x29}AXBTTI{-{"><field
name="itemName">OH_Systems</field></shadow></value></block></value><value
name="B"><block type="text" id="H=Fzh6lL;=|~mCoqAwBd"><field
name="TEXT">OFF</field></block></value></block></value><statement
name="DO0"><block type="oh_timer" id="NdTJ2fW$1~t6gvsjW|mp"><field
name="delayUnits">plusHours</field><value name="delay"><shadow
type="math_number" id="otI%iP,VT#I=+[%M8=$v"><field
name="NUM">3</field></shadow></value><value name="timerName"><shadow
type="text" id="hry6.e7COO#4Yrlw%PNr"><field name="TEXT">OH System
Offline Timer</field></shadow></value><statement name="timerCode"><block
type="oh_log" id="{k@Er3wG8e6$n.*+A_g["><field
name="severity">info</field><value name="message"><shadow type="text"
id="C`ZX6$9jB.8rJ+#GpmIk"><field name="TEXT">Email Sent - OH System
Offline</field></shadow></value></block></statement></block></statement><value
name="IF1"><block type="logic_compare" id="`0xmVOp9Yi!jHAVTY~nr"><field
name="OP">EQ</field><value name="A"><block type="oh_getitem_state"
id="gn_P~zcF0r4[%lu*XxNO"><value name="itemName"><shadow type="oh_item"
id="rB6;`FJ6$r67tZ2u0Y|0"><field
name="itemName">OH_Systems</field></shadow></value></block></value><value
name="B"><block type="text" id="Vv,,Sxe#prf8)FVHzium"><field
name="TEXT">ON</field></block></value></block></value><statement
name="DO1"><block type="oh_timer_cancel"
id="o(tjF3t^a{zsOe8!gwom"><value name="timerName"><shadow type="text"
id="N?I[ze3evs@))+@xy@h{"><field name="TEXT">OH System Offline
Timer</field></shadow></value></block></statement></block></next></block></xml>
type: application/javascript
script: >
var logger =
Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' +
ctx.ruleUID);
var scriptExecution = Java.type('org.openhab.core.model.script.actions.ScriptExecution');
var zdt = Java.type('java.time.ZonedDateTime');
if (typeof this.timers === 'undefined') {
this.timers = [];
}
logger.info('OH System Offline Rule Started');
var Actions = Java.type("org.openhab.core.model.script.actions.Things");
var mailActions = Actions.getActions("mail","mail:smtp:SMTP_Server");
mailActions.sendHtmlMail("email@youremail.com", "Alert – openHAB System Offline", "An openHAB system has gone offline. Please check Tailscale to identify offline system.");
if (itemRegistry.getItem('OH_Systems').getState() == 'OFF') {
if (typeof this.timers['OH System Offline Timer'] === 'undefined' || this.timers['OH System Offline Timer'].hasTerminated()) {
this.timers['OH System Offline Timer'] = scriptExecution.createTimer(zdt.now().plusHours(3), function () {
logger.info('Email Sent - OH System Offline');
})
}
} else if (itemRegistry.getItem('OH_Systems').getState() == 'ON') {
if (typeof this.timers['OH System Offline Timer'] !== 'undefined') {
this.timers['OH System Offline Timer'].cancel();
this.timers['OH System Offline Timer'] = undefined;
}
}
type: script.ScriptAction
Save > Save