Use persistence in JSRule

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? :slight_smile:

Rrd4j requires at least an everyMinute strategy to work. When it falls, persistence returns null.

In the object Strategies you mean? Or also actually in use by an item?

But this “working example” doesn’t have an ´everyMinute´strategy? Below you will find a complete example persistence configuration file.

I’m strategies and used by the items.

That’s a generic configuration that works for all the persistence engines except rrd4j.

Read the docs for rrd4j for details on the it works but the tl;dr is rrd4j is a round robin database that requires a sample to be saved at fixed intervals to work. The default database config for rrd4j requires that interval to be everyMinute.

Seems a bit over the top for what I want to achieve, but ok :slight_smile: Like this then?

Strategies {
    everyMinute : "0 * * * * ? *"
    everyDay : "0 0 0 * * ? *"
    default = everyChange
}

/*
Filters {
    // Zie https://www.openhab.org/docs/configuration/persistence.html#filters
}
*/

Items {
    Bewoners_Thuis* : everyChange, everyMinute
}

Unfortuntely, this still throws the same error…

(A side note:

Here (rrd4j - Persistence Services | openHAB) I read the following:

So I shouldn’t have created rrd4j.persist? I don’t fully understand how to read this…
)

So RRD4J comes with a default strategy, which is applied when you don’t create a rrd4j.persist and don’t configure persistence through the UI.
This default strategy is to persist every Item every minute, but in case you don’t want this behaviour, you can customise it the way you did.
RRD4J would have also worked without creating a config, both ways are fine.

Which openHAB version do you use?

And what is the output of

console.info(utils.OPENHAB_JS_VERSION)

Where should I run this command?

erik@MinipcLG2:~$ sudo openhab-cli console

Logging in as openhab
Password:

                           _   _     _     ____
   ___   ___   ___   ___  | | | |   / \   | __ )
  / _ \ / _ \ / _ \ / _ \ | |_| |  / _ \  |  _ \
 | (_) | (_) |  __/| | | ||  _  | / ___ \ | |_) )
  \___/|  __/ \___/|_| |_||_| |_|/_/   \_\|____/
       |_|       4.1.2 - Release Build

Use '<tab>' for a list of available commands
and '[cmd] --help' for help on a specific command.
To exit, use '<ctrl-d>' or 'logout'.

openhab> console.info(utils.OPENHAB_JS_VERSION)
Command not found: console.info(utils.OPENHAB_JS_VERSION)

&

erik@MinipcLG2:~$ console.info(utils.OPENHAB_JS_VERSION)
-bash: syntaxfout nabij onverwacht symbool 'utils.OPENHAB_JS_VERSION'

Paste this into a JS script, run it and have a look at the logs.

That makes sense in hindsight… :slight_smile:

The output is 4.7.3.

As you are using openHAB 4.1.2, you cannot use Item::persistence.
It is Item::history instead, just have a look at the docs: JavaScript Scripting - Automation | openHAB

1 Like

This did the trick!

But frankly, that means this statement is incorrect: Full documentation for the openHAB JavaScript library can be found at openhab-js.

Yeah the problem is that the JSDoc on GitHub Pages always is the latest one from the main branch, and not the version matching the add-on included openhab-js version.