ErikDB
(Erik)
January 7, 2025, 2:43pm
1
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.
…?
rlkoshak
(Rich Koshak)
January 7, 2025, 3:05pm
2
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.
ErikDB
(Erik)
January 7, 2025, 3:30pm
3
I just created a variable. Doesn’t matter how I call it, right? But it was redundant, that’s true.
This seems like a bigger problem.
I think I did that? (Be it via a redundant variable…)
rlkoshak
(Rich Koshak)
January 7, 2025, 3:32pm
4
I think I missed that.
I didn’t notice the variable as I wasn’t expecting it to be there.
Oliver2
(Oliver)
January 7, 2025, 4:10pm
5
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
ErikDB
(Erik)
January 7, 2025, 4:38pm
6
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.
JustinG
(JustinG)
January 7, 2025, 4:48pm
7
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