I have a very strange issue with a switch block. Have to admit that I am still not sufficiently familiar with the syntax, being much more familiar with C++ and PHP from a background perspective.
Anyhow, searching through other threads, I still think I got it right but it somehow just isn’t working.
So, here we go.
I have an item which I use to store an integer in (Tasmota_Toggle_lights). The number is representing a specific scenario on external lights usage.
In the rule where I want to toggle through the scenarios (i.e. Tasmota_Toggle_lights numbers 1 to 4), I somehow canno get the switch block working.
I use the following code:
var actionName = receivedEvent.getEvent()
switch(actionName) {
case "SHORT_PRESSED": {
logInfo("XiaomiWirelessSwitch1", "SHORT_PRESSED")
var Number ttl
ttl = Tasmota_Toggle_lights.state as Number
if (ttl <0) {
ttl = 0
logInfo("XiaomiWirelessSwitch1", "Setting ttl to 0")
}
ttl = ttl + 1
if (ttl > 4) {
ttl = 1
logInfo("XiaomiWirelessSwitch1", "Setting ttl back to 1")
}
logInfo("XiaomiWirelessSwitch1", "ttl is now " + ttl + ".")
switch (ttl) {
case 1: {
logInfo("XiaomiWirelessSwitch1", "case 1")
}
case 2: {
logInfo("XiaomiWirelessSwitch1", "case 2")
}
case 3: {
logInfo("XiaomiWirelessSwitch1", "case 3")
}
case 4: {
logInfo("XiaomiWirelessSwitch1", "case 4")
}
default: {
logInfo("XiaomiWirelessSwitch1", "case default.")
}
}
Tasmota_Toggle_lights.sendCommand(ttl)
}
Can anyone see any error??
When executing the rule, I get the folloing log outputs:
2020-04-19 18:28:46.717 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - SHORT_PRESSED
2020-04-19 18:28:46.735 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - ttl is now 1.
2020-04-19 18:28:46.741 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - case default.
2020-04-19 18:28:50.335 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - SHORT_PRESSED
2020-04-19 18:28:50.356 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - ttl is now 2.
2020-04-19 18:28:50.362 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - case default.
2020-04-19 18:28:53.416 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - SHORT_PRESSED
2020-04-19 18:28:53.436 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - ttl is now 3.
2020-04-19 18:28:53.443 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - case default.
2020-04-19 18:28:56.707 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - SHORT_PRESSED
2020-04-19 18:28:56.724 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - ttl is now 4.
2020-04-19 18:28:56.731 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - case default.
2020-04-19 18:29:00.140 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - SHORT_PRESSED
2020-04-19 18:29:00.148 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - Setting ttl back to 1
2020-04-19 18:29:00.153 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - ttl is now 1.
2020-04-19 18:29:00.156 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - case 1
2020-04-19 18:29:05.839 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - SHORT_PRESSED
2020-04-19 18:29:05.858 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - ttl is now 2.
2020-04-19 18:29:05.864 [INFO ] [e.model.script.XiaomiWirelessSwitch1] - case default.
This tells me that somehow the switch statement does not recognize the numeric values!? It only works once when ttl is set back to value “1”.
But why isn’t the ttl = ttl +1 not working? Or well, in the log I get the correct numbers, but again, not in the switch block apparently.
I am really puzzled… can anyone help?