Getting itemName of rule with DateTimeTrigger

I’ve got this rule:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: haard_eetkamer_gepland_commando_OFF
      timeOnly: false
    type: timer.DateTimeTrigger
  - id: "2"
    configuration:
      itemName: haard_salon_gepland_commando_OFF
      timeOnly: "false"
    type: timer.DateTimeTrigger
conditions: []
actions:
  - inputs: {}
    id: "3"
    configuration:
      type: application/javascript
      script: |-
        var triggerenditem = event.itemName
        console.log("triggerenditem = "+triggerenditem)
        console.log("event = "+event)
    type: script.ScriptAction

But itemName isn’t logged:

15:40:28.519 [INFO ] [ipt.ui.haarden-tijdssturing-bellfires] - triggerenditem = undefined
15:40:28.524 [INFO ] [ipt.ui.haarden-tijdssturing-bellfires] - event = Timer 1 triggered.

…?

That’s not how you get the triggering Item name in JS Scripting. See JavaScript Scripting - Automation | openHAB.

Though I think that the Item name may not be available in a “time is item” type trigger in the first place. But changing your log to event.itemName instead of triggeringItem (notice you also mispelled this as `“triggerenditem” so it wouldn’t have worked even in Rules DSL) should indicate if that’s the case or not.

I just created a variable. Doesn’t matter how I call it, right? :slight_smile: But it was redundant, that’s true.

This seems like a bigger problem.

I think I did that? (Be it via a redundant variable…)

I think I missed that.

I didn’t notice the variable as I wasn’t expecting it to be there.

You can try examining the content of the event object. Maybe you find some information as to which item triggered the time event:

console.log("tostring:", this.event.toString());
console.log("payload:", this.event.getPayload());
console.log("topic:", this.event.getTopic());
1 Like

Indeed, that did the trick:

17:20:20.064 [INFO ] [ipt.ui.haarden-tijdssturing-bellfires] - triggerenditem = undefined
17:20:20.067 [INFO ] [ipt.ui.haarden-tijdssturing-bellfires] - event = Timer 1 triggered.
17:20:20.068 [INFO ] [ipt.ui.haarden-tijdssturing-bellfires] - tostring: Timer 1 triggered.
17:20:20.069 [INFO ] [ipt.ui.haarden-tijdssturing-bellfires] - payload: {"itemName":"haard_eetkamer_gepland_commando_OFF","offset":0,"timeOnly":false}
17:20:20.070 [INFO ] [ipt.ui.haarden-tijdssturing-bellfires] - topic: openhab/timer/timer.DateTimeTrigger/triggered

However, I keep failing in retrieving the property itemName from object this.event.getPayload(). Could someone show me how? I tried these:

var payload = this.event.getPayload()
var itemnaam = payload["itemName"]
console.log("itemnaam = "+itemnaam)
var itemnaamtris = payload.itemName
console.log("itemnaamtris = "+itemnaamtris)
var itemnaamvier = payload['"itemName"']
console.log("itemnaamvier = "+itemnaamvier)
var itemnaambis = payload[itemName]
console.log("itemnaambis = "+itemnaambis)

But with these results:

17:41:12.068 [INFO ] [ipt.ui.haarden-tijdssturing-bellfires] - itemnaam = undefined
17:41:12.069 [INFO ] [ipt.ui.haarden-tijdssturing-bellfires] - itemnaamtris = undefined
17:41:12.071 [INFO ] [ipt.ui.haarden-tijdssturing-bellfires] - itemnaamvier = undefined
17:41:12.073 [ERROR] [l.handler.AbstractScriptModuleHandler] - Script execution of rule with UID 'haarden-tijdssturing-bellfires' failed: org.graalvm.polyglot.PolyglotException: ReferenceError: "itemName" is not defined

Okay, I found out why:

var payload = this.event.getPayload()
console.log("typeof payload = "+typeof payload)

Log:

17:45:19.761 [INFO ] [ipt.ui.haarden-tijdssturing-bellfires] - typeof payload = string

Not what I expected, but since I wanted to run .includes(), I’m helped.

getPayload returns a string, not an object. You would have to parse that string into an object first:

var itemnaam = JSON.parse(this.event.getPayload())["itemName"]
2 Likes