JS - Strange behavior of `event`

Although a lot of my rules work well, I notice this strange behavior:

I’ve got a “test switch”:

version: 1
items:
  testswitch:
    type: Switch
    label: testswitch

and a “test rule”:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: testswitch
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: >-
        console.log("event.itemName = "+event.itemName)

        console.log("typeof event.itemName = "+typeof event.itemName)

        console.log("event.newState = "+console.log(event.newState))

        console.log("typeof event.newState = "+typeof event.newState)

        console.log("event.receivedCommand =
        "+console.log(event.receivedCommand))

        console.log("typeof event.receivedCommand = "+typeof
        event.receivedCommand)
    type: script.ScriptAction

I switched testswitch ON and then OFF, and these are the logs:

17:13:00.141 [INFO ] [openhab.event.RuleUpdatedEvent       ] - Rule 'test' has been updated.
17:13:06.257 [INFO ] [openhab.event.ItemCommandEvent       ] - Item 'testswitch' received command ON (source: org.openhab.ui=>org.openhab.core.io.rest$deboeck.erik@gmail.com)
17:13:06.259 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'testswitch' changed from OFF to ON (source: org.openhab.core.autoupdate)
17:13:06.263 [INFO ] [nhab.automation.jsscripting.rule.test] - event.itemName = testswitch
17:13:06.265 [INFO ] [nhab.automation.jsscripting.rule.test] - typeof event.itemName = string
17:13:06.266 [INFO ] [nhab.automation.jsscripting.rule.test] - ON
17:13:06.268 [INFO ] [nhab.automation.jsscripting.rule.test] - event.newState = undefined
17:13:06.269 [INFO ] [nhab.automation.jsscripting.rule.test] - typeof event.newState = string
17:13:06.271 [INFO ] [nhab.automation.jsscripting.rule.test] - undefined
17:13:06.272 [INFO ] [nhab.automation.jsscripting.rule.test] - event.receivedCommand = undefined
17:13:06.274 [INFO ] [nhab.automation.jsscripting.rule.test] - typeof event.receivedCommand = undefined
17:13:10.858 [INFO ] [openhab.event.ItemCommandEvent       ] - Item 'testswitch' received command OFF (source: org.openhab.ui=>org.openhab.core.io.rest$deboeck.erik@gmail.com)
17:13:10.860 [INFO ] [nhab.automation.jsscripting.rule.test] - event.itemName = testswitch
17:13:10.861 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'testswitch' changed from ON to OFF (source: org.openhab.core.autoupdate)
17:13:10.863 [INFO ] [nhab.automation.jsscripting.rule.test] - typeof event.itemName = string
17:13:10.866 [INFO ] [nhab.automation.jsscripting.rule.test] - OFF
17:13:10.867 [INFO ] [nhab.automation.jsscripting.rule.test] - event.newState = undefined
17:13:10.869 [INFO ] [nhab.automation.jsscripting.rule.test] - typeof event.newState = string
17:13:10.870 [INFO ] [nhab.automation.jsscripting.rule.test] - undefined
17:13:10.872 [INFO ] [nhab.automation.jsscripting.rule.test] - event.receivedCommand = undefined
17:13:10.873 [INFO ] [nhab.automation.jsscripting.rule.test] - typeof event.receivedCommand = undefined
  1. Where do these outputs ON and OFF come from (resp. 17:13:06.266 and 17:13:10.866)?
  2. What’s with all the undefineds?

You have a console.log inside a call to console.log.

I would have expressed an error but apparently it’s just calling the inner one before the outer one and since console.log doesn’t return anything the outer one logs undefined.

It should be

console.log("event.newState = "+event.newState);

As for the other undefined, not all properties are available for all rule triggers. This rule is triggered by an item change so there will never be event.receivedCommand. That will always be undefined.

1 Like

Ah. So obvious… :slight_smile:

1 Like

I now see the same thing:

triggers:
  - id: "2"
    configuration:
      itemName: piholes_status
    type: core.ItemCommandTrigger
  - id: "3"
    configuration:
      itemName: piholes_blokkeringstermijn
    type: core.ItemCommandTrigger

&

console.log("event.newState = "+event.newState);
console.log("typeof event.newState = "+typeof event.newState);
console.log("event.newState == 'ON'"+event.newState == "ON");
console.log("event.receivedState = "+event.receivedState);
console.log("typeof event.receivedState = "+typeof event.receivedState);
console.log("event.receivedState == 'ON'"+event.receivedState == "ON");

Output:

21:20:52.139 [INFO ] [openhab.event.ItemCommandEvent       ] - Item 'piholes_status' received command OFF (source: org.openhab.ui=>org.openhab.core.io.rest$XXXXXXX)
21:20:52.141 [INFO ] [ab.automation.jsscripting.rule.pihole] - event.newState = undefined
21:20:52.141 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'piholes_status' changed from ON to OFF (source: org.openhab.core.autoupdate)
21:20:52.141 [INFO ] [ab.automation.jsscripting.rule.pihole] - typeof event.newState = undefined
21:20:52.142 [INFO ] [ab.automation.jsscripting.rule.pihole] - false
21:20:52.143 [INFO ] [ab.automation.jsscripting.rule.pihole] - event.receivedState = undefined
21:20:52.143 [INFO ] [ab.automation.jsscripting.rule.pihole] - typeof event.receivedState = undefined
21:20:52.143 [INFO ] [ab.automation.jsscripting.rule.pihole] - false

The trigger for the script is core.ItemCommandTrigger so the event object won’t have a newState property, where as your first script was an core.ItemStateChangeTrigger, which will have a newState property, but not a received command property.

To provide a bit more details, from @Johnno’s correct answer, please see the table in the JS Scripting docs. It tells you which properties exist on the event Object depending on the way the rule was triggered. JavaScript Scripting - Automation | openHAB

It makes no sense to have a newState nor receivedState property on the event that was triggered by a command because a command doesn’t change the state of the Item. There is no newState yet. Only an update can change the state of an Item. There is no newState (necessarily) for an update event. There is only a newState when the Item has changed state.

You must know how the Item is triggered before accessing properties of event because only the relevant properties will be defined.

Most of time, you’ll just know because the rule only has one trigger or all the same type of triggers. However, if the rule has multiple different types of triggers, you can determine what type of trigger it was through the event.type and event.triggerType properties. And even then, those are only defined when the rule is triggered and not when the rule was called from another rule.

What are you trying to achieve with this rule? If it’s reacting to a change in PiHole, you should use a changed trigger, However, if it is running some code to make a change in PiHole, received command is appropriate. But you need to use event.receivedCommand instead of event.newState or event.receivedState.

Of course (bis)… There’s not only the two ...States, but also the ...Command. I should have focused better. :grimacing:

In any case, now the documentation is more than complete :wink: