Openhab-js - jss scripting

can somebody explain me the diffrence between data and event in execute section of JSRule. i noticed that when i am using current state of an item instead of received command (changing state takes a few milis) my rules are not working correctly.

1/ First question data vs event?

rules.JSRule({

    name: `Termostat ${i} modes management`,

    id: `Termostat_${i}_modes_management`,

    triggers: [triggers.ItemCommandTrigger(`RadiatorAC${i}_homekit_TargetHeatingCoolingMode`)],

    execute: data => { } 

vs 

    execute: event => {}

2/ Second question how to use “Received Command” in JSRule jss. Example of problematic rule:

triggers: [triggers.ItemCommandTrigger(RadiatorAC${i}_homekit_TargetHeatingCoolingMode)],

execute: data => { switch (items.getItem(RadiatorAC${i}_homekit_TargetHeatingCoolingMode).state.toString()) {
…}}
the triggers is working ok, but in “switch” i would rather refer to event.command (for a received command) rather than state.

thank you in advance!

  1. event doesn’t exist in a file based JS Scripting rule. The library provides a JS Object instead of the Java event Object and for what ever reason changed the names of the properties. The names generally match the names of the implicit variables you will find in the Rules DSL docs. If you console.log it I think it will dump the full object and show you what’s in it.

  2. Since there is no event, you can’t refer to that. The command should be in the data object though, probably called data.receivedCommand. Again, log it out and you’ll see what’s there.

Thank you so match, it’s indeed data.receivedCommand using this as a switch solved my issue with rule:)
but… openhab-js team introduced pwm and pid trigger in triggers - Documentation and they use “event” instead if data - both rules are working ok, but just pointing it out, don’t know if it should be changed or not

You can name that variable what ever you want. That;'s what execute: data => { } does. It names the “event” Object variable for your rule.

In the examples you’ll see they named it “event” instead of “data” like you did.

execute: (event) => {

1 Like

@Jacek_Kaczmarczyk

As @rlkoshak said, you can name that variable absolutely what you want.

Personally, I prefer to name that variable event as it is describing the event instance that triggered the rule, and as I implemented PWM & PID triggers in openhab-js I wrote event in the docs.

@rlkoshak & @Jacek_Kaczmarczyk
Have a look at File-Based Event Object Docs, where all properties of event or data or whatever you name it are described.

I know that it is confusing that that docs currently are only at openhab-js, but I am working on getting them to the JS Scripting docs soon.

@rlkoshak
Please feel free to tag me to get me into a conversation about openhab-js / JS Scripting.