How to get the state of triggering item OH4 (Windows)

Hello,

please help me…I found many examples for this topic but for OH2, OH3, nothing for OH4 (ver. 4.1.1 Windows platform) :frowning:

In DSL script I’m trying to get the state of triggering item, but nothing of methods work :frowning:

I tried to do that by:
var vsTriggerItemName = triggeringItemName
var vsState = itemRegistry.getItem(vsTriggerItemName).getState().toString()

var vsState = triggeringItem.state
var vsState = getState(triggeringItem)
var vsState = triggeringItem.newState
var vsState = triggeringItem.getState()

so how to get the state of triggered item? Somebody can help me?

It is triggeringItemName.
As for the state, do NOT try to get the state by .state.
You need to use implicit variables as described here

Otherwise you run into a problem that your rule gets triggered and before OH gets the state back, the state might have already changed.
The name of that implicit variable depends on how the rule is triggered (by channel, by command or by update).
If you post the complete rule we are able to help you.

As you see above I tried to use also “.newState” as you menioned in the link “Rules” here is also newState, but it doesn’t work.

My rule is here:

Administration
Rules
Search (for advanced search, use the developer sidebar (Shift+Alt+D))
13 rules
N
O
P
R
S
U
Z
Update timer for all switches - how long are ON
1
configuration: {}
2
triggers:
3
  - id: "1"
4
    configuration:
5
      itemName: SwitchSalon01_SwitchSalon01A
6
      state: ON
7
    type: core.ItemStateUpdateTrigger
8
  - id: "3"
9
    configuration:
10
      itemName: SwitchSalon01_SwitchSalon01B
11
      state: ON
12
    type: core.ItemStateUpdateTrigger
13
  - id: "4"
14
    configuration:
15
      itemName: SwitchSalon02_SwitchSalon02A
16
      state: ON
17
    type: core.ItemStateUpdateTrigger
18
  - id: "5"
19
    configuration:
20
      itemName: SwitchSalon02_SwitchSalon02B
21
      state: ON
22
    type: core.ItemStateUpdateTrigger
23
  - id: "6"
24
    configuration:
25
      itemName: SwitchSalon03_SwitchSalon03A
26
      state: ON
27
    type: core.ItemStateUpdateTrigger
28
  - id: "7"
29
    configuration:
30
      itemName: SwitchSalon03_SwitchSalon03B
31
      state: ON
32
    type: core.ItemStateUpdateTrigger
33
  - id: "8"
34
    configuration:
35
      itemName: SwitchSalon04_SwitchSalon04A
36
      state: ON
37
    type: core.ItemStateUpdateTrigger
38
  - id: "9"
39
    configuration:
40
      itemName: SwitchSalon04_SwitchSalon04B
41
      state: ON
42
    type: core.ItemStateUpdateTrigger
43
  - id: "10"
44
    configuration:
45
      itemName: SwitchKorytarz01_SwitchKorytarz01A
46
      state: ON
47
    type: core.ItemStateUpdateTrigger
48
  - id: "11"
49
    configuration:
50
      itemName: SwitchKorytarz01_SwitchKorytarz01B
51
      state: ON
52
    type: core.ItemStateUpdateTrigger
53
conditions: []
54
actions:
55
  - inputs: {}
56
    id: "2"
57
    configuration:
58
      type: application/vnd.openhab.dsl.rule
59
      script: >-
60
        
61
​
62
        logInfo("UPDATE TIMER OF SWITCH", "")
63
​
64
​
65
        var vlSwitch01 = "Salon01"
66
​
67
        var vlSwitch02 = "Salon02"
68
​
69
        var vlSwitch03 = "Salon03"
70
​
71
        var vlSwitch04 = "Salon04"
72
​
73
        var vlSwitch05 = "Korytarz01"
74
​
75
        var vlSwitches = newArrayList(vlSwitch01, vlSwitch02, vlSwitch03, vlSwitch04, vlSwitch05)
76
​
77
​
78
        var vlTasmotaSalon01 = "tasmota_34DAFA"
79
​
80
        var vlTasmotaSalon02 = "tasmota_1222E4"
81
​
82
        var vlTasmotaSalon03 = "tasmota_34D599"
83
​
84
        var vlTasmotaSalon04 = "tasmota_34DB77"
85
​
86
        var vlTasmotaKorytarz01 = "tasmota_344CE2"
87
​
88
        var vlTasmotaSwitches = newArrayList(vlTasmotaSalon01, vlTasmotaSalon02, vlTasmotaSalon03, vlTasmotaSalon04, vlTasmotaKorytarz01)
89
​
90
​
91
        var vsTriggerItemName = "isEmpty"
92
​
93
        vsTriggerItemName = triggeringItemName
94
​
95
        //var itemName = triggerItem.substring(0, triggerItem.length - 4).toString
96
​
97
        //sendCommand(itemName, ON)
98
​
99
        logInfo("vsTriggerItemName", "vsTriggerItemName: {}", vsTriggerItemName)
100
​
101
​
102
        if(vsTriggerItemName!=null){
103
​
104
          var viTemp = -1
105
          var vsTemp = ""
106
          var viListCount = 5
107
          var vsSwitchName = ""
108
          var viSwitchIndex = -1
109
          
110
          while((viTemp=viTemp+1)<viListCount){
111
            vsTemp = vlSwitches.get(viTemp)
112
            if(vsTriggerItemName.contains(vsTemp)){
113
              viSwitchIndex = viTemp
114
              vsSwitchName = vsTemp
115
              vsTemp = "Switch" + vsTemp + "_Switch"+vsTemp+"Timer"
116
              //SwitchSalon02_SwitchSalon02Timer
117
              logInfo("itemName", "itemName: {}", vsTemp)
118
              sendCommand(vsTemp, now.toLocalDateTime().toString())
119
              viTemp = viListCount
120
            }
121
          }
122
​
123
          if(viSwitchIndex>-1){
124
            var viLen = 0
125
            var vsChar = ""
126
            var vsMQTTCmd = ""
127
            viLen = vsTriggerItemName.length()
128
            //logInfo("length", "length: {}", vsTriggerItemName+"("+viLen.toString()+")")
129
            vsChar = vsTriggerItemName.substring(viLen-1)
130
            //logInfo("vsSwitchName", "vsSwitchName-: {}", vsSwitchName+"-"+vsChar)
131
          
132
            //var vsState = itemRegistry.getItem(vsTriggerItemName).getState().toString()
133
            //var vsState = triggeringItem.state
134
            //var vsState = getState(triggeringItem)
135
            //var vsState = triggeringItem.newState
136
            //var vsState = triggeringItem.getState()
137
            logInfo("vsState", "vsState-: {}", vsState.toString())
138
            
139
            switch (vsChar) {
140
             case "A":{
141
                vsMQTTCmd = 'cmnd/'+vlTasmotaSwitches.get(viSwitchIndex)+'/POWER1'
142
                }
143
             case "B":{
144
                vsMQTTCmd = 'cmnd/'+vlTasmotaSwitches.get(viSwitchIndex)+'/POWER2'
145
                }
146
            }
147
          
148
            logInfo("vsMQTTCmd", "vsMQTTCmd-: {}", vsMQTTCmd)
149
            //mqttActions.publishMQTT(vsMQTTCmd, "OFF")
150
            
151
          }
152
        }
153
    type: script.ScriptAction
154
​

logInfo(triggeringItemName)
logInfo(newState)

Also think about adding all your items into a group and then have the rule triggered by a change of a group member. Your rule becomes easier und you still can access the triggering item by

triggeringItem.name

(please note the difference to triggeringItemName)

Hello @Oliver2

the first one:

logInfo(triggeringItemName)

it doesn’t work :frowning:
[internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘c440808901’ failed: cannot invoke method public abstract java.lang.String org.openhab.core.items.Item.getName() on null

the second one:

logInfo(newState)

it works! :slight_smile:

Thank you very much for your help! Have a nice day.

…and going the same way like “Rules” doc, why the previousState doesn’t work?

    var vsPrevState = ""
    vsPrevState = previousState.toString()
    logInfo("vsPrevState", "vsPrevState-: {}", vsPrevState)

the ERROR is:
[internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘c440808901’ failed: cannot invoke method public java.lang.String java.lang.Object.toString() on null

? :frowning:

previousState is available when rule is triggered by CHANGE of item, not update of an item

could you make a test with
triggeringItem.name
I remember there were plans to align implicit variables. Maybe this was already done but the docs were not updated.

Hello @Oliver2 - thank you so much for your help and claryfication.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.