Type Error when creating JSRule

Hello community/@florian-h05,

Relevant setup info: OH 3.4.0, openhab-js 3.2.4, IDE: VS Code

When creating a Rule using rules:JSRule I have type errors on every triggers definition since updating to openhab-js 3.2.4:

Type ‘{}’ is missing the following properties from type ‘typeof import(“[…]/automation/js/node_modules/openhab/types/triggers”)’: ChannelEventTrigger, ItemStateChangeTrigger, ItemStateUpdateTrigger, ItemCommandTrigger, and 11 more.ts(2740)

I guess the definition should be okay as it works, but the ts-error is irritating:

oh.rules.JSRule({
    name: "start-stop-helios-air-quality",
    description: "Starts the helios vent respecting humidity, air-quality, smartphone-presence and room-presence",
    triggers: [oh.triggers.GenericCronTrigger("0 0/10 * * * ?")], // 10 Minutes
    execute: async (event) => {...}
})

Regards

Can you please post the full code of your rule (I want to see how you import the library)?

Your code looks good, except from the async keyword for the execute function.

And please use code fences as well, so your code is better readable. See Markdown Code Block: Including Code In .md Files - Markdown Land if you need some help with code fences.

Thanks for the tip with code fences. I updated my post.

I import openhab-js using require(“openhab”):

// @ts-check
const oh = require("openhab");

oh.rules.JSRule({
    name: "reset-item-values",
    description: "Resets item values for the upcoming day",
    triggers: [oh.triggers.GenericCronTrigger("0 0 5 * * ?")],
    execute: (event) => {...}
})

oh.rules.JSRule({
    name: "late-night-state-mgmt",
    description: "Sets isLate and isNight flags",
    triggers: [oh.triggers.GenericCronTrigger("0 0 21 * * ?"), oh.triggers.GenericCronTrigger("0 0 22 * * ?")],
    execute: (event) => {...}
})

changing the import to

const { rules, triggers, items, time, actions } = require("openhab");

produces the same error

I guess this (GenericCronTrigger returns object)


is the reason for the error…

I hope I didn’t miss something…?

Thanks for the more details, I guess I have to check this myself on my PC, I will inform you when I have an idea what’s the problem.

I finally had the time to have a look at it, was a wrong JSDoc annotation.
I fixed it with [rules] Fix wrong JSDoc/type def for RuleConfig · openhab/openhab-js@3b1aa52 · GitHub, this will be available with the next openHAB JavaScript library release. Be AWARE that this release will include a few breaking changes, see openhab-js/CHANGELOG.md at main · openhab/openhab-js · GitHub.