Suggestions for best way to parse a Number state. (deCONZ Xiaomi cube related)

Thank you @vzorglub for pointing me in this direction.

Just for completion I am posting my working solution:

rule "Xiaomi Cube action"
when
    Item Pink1CubeButtonLastUpdated changed
then
    Thread::sleep(20) // without this, the previos buttonevent is returned
    var P1buttonstate = (Pink1CubeButtonEvent.state as DecimalType).intValue
    var P1face = Math::round(P1buttonstate / 1000)
    var P1prevFace = Math::round(P1buttonstate - P1face*1000)

    var name = "Cube P1" 
    logInfo(name, "Event: " + P1buttonstate)
    logInfo(name, "Face UP: " + P1face)
    logInfo(name, "Last Face: " + P1prevFace)

    switch(P1face) {
        case 1: {            
            switch(P1prevFace) {
                case 0: {                    
                  logInfo(name, "Pushed - w/face"+ P1face+" up")
                }
                case 1: {                    
                  logInfo(name, "DoubleTap - w/face"+ P1face+" up")
                }
                default: {                    
                  logInfo(name, "Face"+ P1face+" action")
                }
            }
        }
        case 2: {            
            switch(P1prevFace) {
                case 0: {                    
                  logInfo(name, "Pushed - w/face"+ P1face+" up")
                }
                case 2: {                    
                  logInfo(name, "DoubleTap - w/face"+ P1face+" up")
                }
                default: {                    
                  logInfo(name, "Face"+ P1face+" action")
                }
            }
        }
        case 3: {            
            switch(P1prevFace) {
                case 0: {                    
                  logInfo(name, "Pushed - w/face"+ P1face+" up")
                }
                case 3: {                    
                  logInfo(name, "DoubleTap - w/face"+ P1face+" up")
                }
                default: {                    
                  logInfo(name, "Face"+ P1face+" action")
                }
            }
        }
        case 4: {            
            switch(P1prevFace) {
                case 0: {                    
                  logInfo(name, "Pushed - w/face"+ P1face+" up")
                }
                case 4: {                    
                  logInfo(name, "DoubleTap - w/face"+ P1face+" up")
                }
                default: {                    
                  logInfo(name, "Face"+ P1face+" action")
                }
            }
        }
        case 5: {            
            switch(P1prevFace) {
                case 0: {                    
                  logInfo(name, "Pushed - w/face"+ P1face+" up")
                }
                case 5: {                    
                  logInfo(name, "DoubleTap - w/face"+ P1face+" up")
                }
                default: {                    
                  logInfo(name, "Face"+ P1face+" action")
                }
            }
        }
        case 6: {            
            switch(P1prevFace) {
                case 0: {                    
                  logInfo(name, "Pushed - w/face"+ P1face+" up")
                }
                case 6: {                    
                  logInfo(name, "DoubleTap - w/face"+ P1face+" up")
                }
                default: {                    
                  logInfo(name, "Face"+ P1face+" action")
                }
            }
        }
        case 7: {
            switch(P1prevFace) {
                case 0: {                    
                  logInfo(name, "Woke up")
                }
                case 7: {                    
                  logInfo(name, "Shaken & stirred")
                }
                case 8: { // from deCONZ 2.05.29               
                  logInfo(name, "Free-falling!!")
                }
            }
        }
    }
end

Log example:

2018-06-10 15:22:58.014 [INFO ] [lipse.smarthome.model.script.Cube P1] - Event: 1006
2018-06-10 15:22:58.015 [INFO ] [lipse.smarthome.model.script.Cube P1] - Face UP: 1
2018-06-10 15:22:58.016 [INFO ] [lipse.smarthome.model.script.Cube P1] - Last Face: 6
2018-06-10 15:22:58.017 [INFO ] [lipse.smarthome.model.script.Cube P1] - Face1 action
2018-06-10 15:23:09.165 [INFO ] [lipse.smarthome.model.script.Cube P1] - Rotation: 711
2018-06-10 15:23:12.175 [INFO ] [lipse.smarthome.model.script.Cube P1] - Event: 6001
2018-06-10 15:23:12.176 [INFO ] [lipse.smarthome.model.script.Cube P1] - Face UP: 6
2018-06-10 15:23:12.176 [INFO ] [lipse.smarthome.model.script.Cube P1] - Last Face: 1
2018-06-10 15:23:12.177 [INFO ] [lipse.smarthome.model.script.Cube P1] - Face6 action
2018-06-10 15:23:17.297 [INFO ] [lipse.smarthome.model.script.Cube P1] - Event: 1006
2018-06-10 15:23:17.298 [INFO ] [lipse.smarthome.model.script.Cube P1] - Face UP: 1
2018-06-10 15:23:17.298 [INFO ] [lipse.smarthome.model.script.Cube P1] - Last Face: 6
2018-06-10 15:23:17.299 [INFO ] [lipse.smarthome.model.script.Cube P1] - Face1 action
2018-06-10 15:23:22.388 [INFO ] [lipse.smarthome.model.script.Cube P1] - Event: 6001
2018-06-10 15:23:22.388 [INFO ] [lipse.smarthome.model.script.Cube P1] - Face UP: 6
2018-06-10 15:23:22.389 [INFO ] [lipse.smarthome.model.script.Cube P1] - Last Face: 1
2018-06-10 15:23:22.390 [INFO ] [lipse.smarthome.model.script.Cube P1] - Face6 action
2018-06-10 15:28:20.318 [INFO ] [lipse.smarthome.model.script.Cube P1] - Event: 6000
2018-06-10 15:28:20.319 [INFO ] [lipse.smarthome.model.script.Cube P1] - Face UP: 6
2018-06-10 15:28:20.319 [INFO ] [lipse.smarthome.model.script.Cube P1] - Last Face: 0
2018-06-10 15:28:20.320 [INFO ] [lipse.smarthome.model.script.Cube P1] - Pushed - w/face6 up
2018-06-10 15:28:29.329 [INFO ] [lipse.smarthome.model.script.Cube P1] - Event: 2006
2018-06-10 15:28:29.329 [INFO ] [lipse.smarthome.model.script.Cube P1] - Face UP: 2
2018-06-10 15:28:29.330 [INFO ] [lipse.smarthome.model.script.Cube P1] - Last Face: 6
2018-06-10 15:28:29.330 [INFO ] [lipse.smarthome.model.script.Cube P1] - Face2 action
2018-06-10 15:28:32.449 [INFO ] [lipse.smarthome.model.script.Cube P1] - Event: 2002
2018-06-10 15:28:32.449 [INFO ] [lipse.smarthome.model.script.Cube P1] - Face UP: 2
2018-06-10 15:28:32.450 [INFO ] [lipse.smarthome.model.script.Cube P1] - Last Face: 2
2018-06-10 15:28:32.450 [INFO ] [lipse.smarthome.model.script.Cube P1] - DoubleTap - w/face2 up
2018-06-10 15:28:35.460 [INFO ] [lipse.smarthome.model.script.Cube P1] - Event: 7007
2018-06-10 15:28:35.460 [INFO ] [lipse.smarthome.model.script.Cube P1] - Face UP: 7
2018-06-10 15:28:35.461 [INFO ] [lipse.smarthome.model.script.Cube P1] - Last Face: 7
2018-06-10 15:28:35.462 [INFO ] [lipse.smarthome.model.script.Cube P1] - Shaken & stirred

Now, for the real fun! :slight_smile:

1 Like