The underlying problem
I have a rule that’s triggered when both me and the lady of the house are away. I check that with our tado apps. But sometimes, here (iPhone) app stops updating (or at least there’s a disconnect somewhere), and as far as openHAB is concerned, she doesn’t leave the house.
So I want to daily check whether she has allegedly left the house in the past two days. If not, there’s probably something off.
Maybe my solution isn’t the best, but still, I want to understand how it works.
Getting this into a JSRule
I read this: Persistence | openHAB, and watched this: https://www.youtube.com/watch?v=zGnoGFuFySU (although it’s outdated). I also took a look at this: ItemPersistence - Documentation.
I ticked off ‘rrd4j’ as default persistance ‘tracker’:
I created rrd4j.persist
in the correct folder:
Strategies {
everyDay : "0 0 0 * * ? *"
default = everyChange
}
/*
Filters {
// Zie https://www.openhab.org/docs/configuration/persistence.html#filters
}
*/
Items {
Bewoners_Thuis* : everyChange
}
Bewoners_Thuis
is a group containing the two Items linked to the channels of our mobiledevice
Things:
I then created this rule:
const { rules, triggers, items } = require('openhab');
rules.JSRule({
name: "Tado thuis nog actief?",
description: 'Soms lijkt de Tado-app niet meer goed te registreren of we thuis zijn of niet. Deze rule controleert dat iedere dag rond middernacht.',
triggers: [triggers.GenericCronTrigger("0 0 0 * * ? *")],
execute: (event) => {
//var eergisteren = new Date(new Date().getTime() - (48 * 60 * 60 * 1000))
var eergisteren = new Date(new Date().getTime() - (0 * 20 * 60 * 1000)) // Since I just created the persistence file, I figured going back two days is maybe problematic...
var tadocln = items.getItem("iPhoneCLN_2023_Tado_Thuis")
var tadodbe = items.getItem("Rikia8_1_Tado_Thuis")
console.log(tadocln.persistence.changedSince(eergisteren))
}
});
Sadly, this didn’t work:
[ERROR] [on.script.file.tadoNietThuisActief.js] - Failed to execute rule Tado-thuis-nog-actief--89868a4a-5f67-4438-b54f-c2dcace77c2d: TypeError: undefined has no such function "changedSince": TypeError: undefined has no such function "changedSince"
at execute (tadoNietThuisActief.js:12)
at doExecute (/node_modules/openhab.js:2)
I’m missing something, no doubt… But what?