I’m not great at complex rules, but ever since chatGPT I made some progress. Where I had simple rules like ‘when off, change to on’ and a separate rule for ‘when on, change to off’ I started to merge them with some help of chatGPT and they all work. Now I started to ask for more complex things, but I can’t seem to figure out why they don’t work and why I can’t copy paste it properly, even with a passage through VST to check the syntax. They seem correct to me, as the logic seems, well, logic
Anyway here is the code, and the error from the log.
This is the code from chatGPT, used in openhabian and build 4.0.0
configuration: {}
triggers:
- id: "1"
configuration:
event: PRESSED
channelUID: nikobus:push-button:ead36cb534:5F8542:Nikobus_Push_Button_10A87E_3_BP35_D_2
type: core.ChannelEventTrigger
- id: "2"
configuration:
event: PRESSED
channelUID: nikobus:push-button:ead36cb534:3F8542:Nikobus_Push_Button_10A87E_7_BP35_C_1
type: core.ChannelEventTrigger
- id: "3"
configuration:
event: PRESSED
channelUID: nikobus:push-button:ead36cb534:7F8542:Nikobus_Push_Button_10A87E_6_BP35_D_1
type: core.ChannelEventTrigger
- id: "4"
configuration:
event: PRESSED
channelUID: nikobus:push-button:ead36cb534:BF8542:Nikobus_Push_Button_10A87E_7_BP35_A_1
type: core.ChannelEventTrigger
- id: "5"
configuration:
event: PRESSED
channelUID: nikobus:push-button:ead36cb534:FF8542:Nikobus_Push_Button_10A87E_8_BP35_B_1
type: core.ChannelEventTrigger
conditions: []
actions:
- inputs: {}
id: "7"
configuration:
type: application/vnd.openhab.dsl.rule
script: >-
// Define item names
val itemName = triggeringItem.name
val itemMappings = [
"ID1": "MQTTSonoff4chan2_Fontein",
"ID2": "MQTTSonoff4chan1_Tuinpad",
"ID3": "MQTTSonoff4chan1_DownlightTuinhuis",
"ID4": "MQTTSonoff4chan1_UplightBomen",
"ID5": "MQTTSonoff4chan1_UplightTuinhuis"
]
// Check if the button was pressed
if (itemMappings.containsKey(itemName)) {
val itemToSwitch = itemMappings.get(itemName)
val currentState = items[itemToSwitch].state.toString()
if (currentState == "ON") {
sendCommand(itemToSwitch, "OFF")
} else if (currentState == "OFF") {
sendCommand(itemToSwitch, "ON")
}
}
type: script.ScriptAction
This is what openhab interface makes of after saving (the rest remains the same, I did a side by side comparison in VSC.
actions:
- inputs: {}
id: "7"
configuration:
type: application/vnd.openhab.dsl.rule
script: >-
// Define item names val itemName = triggeringItem.name val itemMappings
= [
"ID1": "MQTTSonoff4chan2_Fontein",
"ID2": "MQTTSonoff4chan1_Tuinpad",
"ID3": "MQTTSonoff4chan1_DownlightTuinhuis",
"ID4": "MQTTSonoff4chan1_UplightBomen",
"ID5": "MQTTSonoff4chan1_UplightTuinhuis"
]
// Check if the button was pressed if (itemMappings.containsKey(itemName)) {
val itemToSwitch = itemMappings.get(itemName)
val currentState = items[itemToSwitch].state.toString()
if (currentState == "ON") {
sendCommand(itemToSwitch, "OFF")
} else if (currentState == "OFF") {
sendCommand(itemToSwitch, "ON")
}
}
type: script.ScriptAction
and this is the error in the log…
2023-09-07 20:23:02.289 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '5608ee7d5e' failed: // Define item names val itemName = triggeringItem.name val itemMappings = [
"ID1": "MQTTSonoff4chan2_Fontein",
"ID2": "MQTTSonoff4chan1_Tuinpad",
"ID3": "MQTTSonoff4chan1_DownlightTuinhuis",
"ID4": "MQTTSonoff4chan1_UplightBomen",
"ID5": "MQTTSonoff4chan1_UplightTuinhuis"
]
// Check if the button was pressed if (itemMappings.containsKey(itemName)) {
val itemToSwitch = itemMappings.get(itemName)
val currentState = items[itemToSwitch].state.toString()
if (currentState == "ON") {
sendCommand(itemToSwitch, "OFF")
} else if (currentState == "OFF") {
sendCommand(itemToSwitch, "ON")
}
}
2023-09-07 20:23:02.300 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'b435a99536' failed: val itemName = triggeringItem.name val itemToSwitch1 = "MQTTSonoff4chan2_Fontein" val itemToSwitch2 = "MQTTSonoff4chan1_Tuinpad" val itemToSwitch3 = "MQTTSonoff4chan1_DownlightTuinhuis" val itemToSwitch4 = "MQTTSonoff4chan1_UplightBomen" val itemToSwitch5 = "MQTTSonoff4chan1_UplightTuinhuis"
// Check which button was pressed if (itemName == "ID1") {
val currentState = items[itemToSwitch1].state.toString()
if (currentState == "ON") {
sendCommand(itemToSwitch1, "OFF")
} else if (currentState == "OFF") {
sendCommand(itemToSwitch1, "ON")
}
} else if (itemName == "ID2") {
val currentState = items[itemToSwitch2].state.toString()
if (currentState == "ON") {
sendCommand(itemToSwitch2, "OFF")
} else if (currentState == "OFF") {
sendCommand(itemToSwitch2, "ON")
}
} else if (itemName == "ID3") {
val currentState = items[itemToSwitch3].state.toString()
if (currentState == "ON") {
sendCommand(itemToSwitch3, "OFF")
} else if (currentState == "OFF") {
sendCommand(itemToSwitch2, "ON")
}
}
I have no idea
- why openhab does not save the rule correctly
- why the rule does not work.