I don’t store or retrieve data from cache storage, only from persisted storage.
Here is the recurrent rule:
/*
Version: 1.0
*/
// remove namespaces that are not needed by your code
//const { actions, cache, items, things, time, triggers, utils, Quantity } = require('openhab')
const { items, triggers, actions, time } = require('openhab')
var { PercentType } = require('@runtime');
var Heating = require('heating')
var Notification = require('notification')
rules.JSRule({
name: "Check contact sensors",
description: "This rule will check every minute if any contact sensor has been open for longer than the allowed time based on the outside temperature. If it finds any, it will trigger the alert for that sensor and send a notification to the user.",
triggers: [triggers.GenericCronTrigger('0 0/1 * ? * *')],
execute: () => {
console.debug("Contact Sensors > Check contact sensors > STARTING.")
if (items.Override_Home_Away.state === 'OFF' && items.Override_Vacation_Mode.state === 'OFF' && items.Season.state != "SUMMER") {
console.debug("Contact Sensors > Check contact sensors > The home is in active mode and the season is " + items.Season.state)
console.debug("Contact Sensors > Check contact sensors > Iterating through the members of the group group_Sensors_Contact_Open.")
items.group_Sensors_Contact_Open.members.forEach(sensor => {
if (sensor.type === 'Switch') {
console.debug("Contact Sensors > Check contact sensors > The item %s is a switch.", sensor.name)
var roomName = sensor.name.split("_")[0]
console.debug("Contact Sensors > Check contact sensors > Evaluating the %s contact sensor.", roomName)
var room_alert = items.getItem(roomName + "_Sensor_Contact_Window_1_Alert")
var contactsensor_override = items.getItem(roomName + "_Sensor_Contact_AutomationOverride")
if (sensor.state === 'ON' && room_alert.state === 'OFF' && contactsensor_override.state === 'ON') {
console.debug("Contact Sensors > Check contact sensors > The sensor's status is open, the alarm is off, and the sensor's override is ON.")
var time_open = time.toZDT(items.getItem(roomName + "_Sensor_Contact_Window_1_LastOpened").state)
const outside_temp = items.Weather_OWM_Temperature.numericState
const threshold_cold = items.HM_Temperature_Threshold_Cold.numericState
const threshold_cool = items.HM_Temperature_Threshold_Cool.numericState
const forecastTemperature = items.Weather_OWM_Temp_h2.numericState
var threshold_minutes = 0.00
const roomName_transformed = actions.Transformation.transform('MAP', 'locations.map', roomName)
console.debug("Contact Sensors > Check contact sensors > Evaluating the temperature outside.")
if (outside_temp <= threshold_cold && forecastTemperature <= threshold_cold) {
console.debug("Contact Sensors > Check contact sensors > The current and forecasted temperatures are below the cold threshold. Evaluating the rules for cold weather.")
threshold_minutes = items.HM_Time_Threshold_Cold_Open.numericState
if (time.ZonedDateTime.now().minusMinutes(threshold_minutes).isAfterTime(time_open)) {
console.debug("Contact Sensors > Check contact sensors > The %s has been open longer than required. Notifying and triggering the alarm.", roomName)
room_alert.sendCommand("ON")
var message = "The window in the " + roomName_transformed + " has been open for more than " + threshold_minutes + " minutes. It's time to close it."
actions.Voice.say(message, new PercentType(items.Home_Notifications_DefaultVolumeLevel.numericState))
Notification.alert("Rosie > " + message)
}
} else if (outside_temp <= threshold_cool && forecastTemperature <= threshold_cool) {
console.debug("Contact Sensors > Check contact sensors > The current and forecasted temperatures are below the cool threshold. Evaluating the rules for cool weather.")
threshold_minutes = items.HM_Time_Threshold_Cool_Open.numericState
if (time.ZonedDateTime.now().minusMinutes(threshold_minutes).isAfterTime(time_open)) {
console.debug("Contact Sensors > Check contact sensors > The %s has been open longer than required. Notifying and triggering the alarm.", roomName)
room_alert.sendCommand("ON")
var message = "The window in the " + roomName_transformed + " has been open for more than " + threshold_minutes + " minutes. It's time to close it."
actions.Voice.say(message, new PercentType(items.Home_Notifications_DefaultVolumeLevel.numericState))
Notification.alert("Rosie > " + message)
}
} else {
console.debug("Contact Sensors > Check contact sensors > The current or forecasted temperatures are above the cool threshold. No changes will be applied")
}
} else {
console.debug("Contact Sensors > Check contact sensors > The contact sensor is off, the room alert is already on, or the contact sensor override is off. No changes will be applied")
}
} else {
console.debug("Contact Sensors > Check contact sensors > There is a strange item in the list: %s", sensor.name)
}
})
}
console.debug("Contact Sensors > Check contact sensors > ENDING.")
},
tags: ["Sensors","Contact Sensors"],
id: "CheckContactSensors"
})
And the one triggered once the alert group changes to ON. This is the one that throws the error.
var Lights = require('lights')
rules.JSRule({
name: "Alert On",
description: "When the alert for a contact sensor is turned on, this rule will set the color of the lights in the master bedroom and living room to red and disable the motion sensors and automated lights in those rooms.",
triggers: [triggers.ItemStateChangeTrigger('group_Sensors_Contact_Alert', 'OFF', 'ON')],
execute: () => {
console.debug("Contact Sensors > Alert On > STARTING.")
console.debug("Contact Sensors > Alert On > The group sensor item switched from OFF to ON. Storing the current state of the light switches.")
if (items.MBR_Sensor_Contact_Light_OldStatus.state != items.group_MBR_Lamp_1_Switch.state) {
items.MBR_Sensor_Contact_Light_OldStatus.postUpdate(items.group_MBR_Lamp_1_Switch.state)
}
if (items.LR_Sensor_Contact_Light_OldStatus.state != items.group_LR_Lamp_1_Switch.state) {
items.LR_Sensor_Contact_Light_OldStatus.postUpdate(items.group_LR_Lamp_1_Switch.state)
}
console.debug("Contact Sensors > Alert On > LR Switch Status: " + items.LR_Sensor_Contact_Light_OldStatus.state +
" | MBR Switch Status: " + items.MBR_Sensor_Contact_Light_OldStatus.state)
console.debug("Contact Sensors > Alert On > Disabling the motion sensors and light automations.")
items.Override_MBR_Sensor_Motion.sendCommand("OFF")
items.Override_LR_Sensor_Motion.sendCommand("OFF")
items.Override_MBR_Automated_Lights.sendCommand("OFF")
items.Override_LR_Automated_Lights.sendCommand("OFF")
console.debug("Contact Sensors > Alert On > Settings the color lights to red.")
Lights.updateColorLight(items.group_MBR_Lamp_1_Color,342,90,100)
Lights.updateColorLight(items.group_LR_Lamp_1_Color,342,90,100)
console.debug("Contact Sensors > Alert On > ENDING.")
},
tags: ["Sensors","Contact Sensors"],
id: "AlertOn"
})