Hi,
I’m trying to modify a rule that is watching a group of squeezeboxes and turns them off if they’re not playing…
Previously I checked after a timeout of 2 minutes if the player is stopped and send a power off command.
but when the track changes exactly at this moment the player reports a stop for a short moment, and I get a false positive and the power is turned of while the player is still playing.
To avoid that I’m trying to use the history.changedSince
which returns a boolean…
My prolem is, the returned boolean is always true. I think this happens because I’m not using an item name but a data variable - but I’m not sure if this is really the problem.
I also made sure that the changedSince period is a bit shorter than the timeout period before the check.
const { items, rules, triggers } = require("openhab");
let watchdogTimer;
rules.JSRule({
name: "Squeeze - Squeezebox Watchdog",
description: "switch squeezebox off on pause after 2 minutes",
triggers: [
triggers.GroupStateChangeTrigger("gSqueezePlaypause", "ON", "OFF"),
],
execute: (data) => {
if (watchdogTimer) clearTimeout(watchdogTimer);
watchdogTimer = null;
watchdogTimer = setTimeout(fn_watchdog, 125000);
const twominutes = time.ZonedDateTime.now().minusMinutes(2);
console.log("Starting Watchdog")
function fn_watchdog() {
console.log(items.getItem(data.itemName).history.changedSince(twominutes));
if (items.getItem(data.itemName).history.changedSince(twominutes) === false) {
if (
items.getItem(data.itemName.replace("Playpause", "Power")).state !==
"OFF"
) {
console.log("Power Off", data.itemName.replace("Playpause", ""));
items
.getItem(data.itemName.replace("Playpause", "Power"))
.sendCommand("OFF");
}
}
clearTimeout(watchdogTimer);
watchdogTimer = null;
}
},
tags: [],
});
normally I would think the changedSince
should be false
if the item not changed for the last 2 minutes, but it returns true
no matter if a player is running or not, and the player never gets powered off…
I’m looking for help how to call the item, which initiated the rule to launch. But maybe I’m on the wrong tracks…