Last action on Xiaomi Cube

I am wondering if it is possible to obtain the last action on the Cube. If it is not available, it will be nice if we can save it as an attribute. We can then significantly expand the number of actions/devices on the cube. For example, I can say if the last action was shake, use rotate to increase brightness otherwise use it to increase volume.

Hi there.

I did it already when I was trying to figure out the actual gestures, but this might help you:

### MQTT sensor to hold Cube last move ###
sensor:
  - platform: mqtt
    state_topic: "xiaomi_cube_state"
    name: "Cube State"
    force_update: True

####################################################
#                                                  #
#              Xiaomi Cube Automations             #
#                                                  #
####################################################
# Available actions are:                           #
# - flip90                                         #
# - flip180                                        #
# - move                                           #
# - tap_twice                                      #
# - shake_air                                      #
# - swing                                          #
# - alert                                          #
# - free_fall                                      #
# - rotate                                         #
####################################################
### Update MQTT sensor with cube last action ###
automation:
  - alias: 'Cube Flip 90'
    trigger:
      platform: event
      event_type: cube_action
      event_data:
        entity_id: binary_sensor.cube_xxxxxxxxxxxxxx
        action_type: flip90
    action:
      - service: mqtt.publish
        data:
          topic: "xiaomi_cube_state"
          payload: "Flip 90"
          retain: "True"

  - alias: 'Cube Flip 180'
    trigger:
      platform: event
      event_type: cube_action
      event_data:
        entity_id: binary_sensor.cube_xxxxxxxxxxxxxx
        action_type: flip180
    action:
      - service: mqtt.publish
        data:
          topic: "xiaomi_cube_state"
          payload: "Flip 180"
          retain: "True"

  - alias: 'Cube Move'
    trigger:
      platform: event
      event_type: cube_action
      event_data:
          entity_id: binary_sensor.cube_xxxxxxxxxxxxxx
          action_type: move
    action:
      - service: mqtt.publish
        data:
          topic: "xiaomi_cube_state"
          payload: "Move"
          retain: "True"

  - alias: 'Cube Tap Twice'
    trigger:
      platform: event
      event_type: cube_action
      event_data:
        entity_id: binary_sensor.cube_xxxxxxxxxxxxxx
        action_type: tap_twice
    action:
      - service: mqtt.publish
        data:
          topic: "xiaomi_cube_state"
          payload: "Tap Twice"
          retain: "True"

  - alias: 'Cube Shake Air'
    trigger:
      platform: event
      event_type: cube_action
      event_data:
          entity_id: binary_sensor.cube_xxxxxxxxxxxxxx
          action_type: shake_air
    action:
      - service: mqtt.publish
        data:
          topic: "xiaomi_cube_state"
          payload: "Shake Air"
          retain: "True"

  - alias: 'Cube Swing'
    trigger:
      platform: event
      event_type: cube_action
      event_data:
        entity_id: binary_sensor.cube_xxxxxxxxxxxxxx
        action_type: swing
    action:
      - service: mqtt.publish
        data:
          topic: "xiaomi_cube_state"
          payload: "Swing"
          retain: "True"

  - alias: 'Cube Alert'
    trigger:
      platform: event
      event_type: cube_action
      event_data:
        entity_id: binary_sensor.cube_xxxxxxxxxxxxxx
        action_type: alert
    action:
      - service: mqtt.publish
        data:
          topic: "xiaomi_cube_state"
          payload: "Alert"
          retain: "True"

  - alias: 'Cube Free Fall'
    trigger:
      platform: event
      event_type: cube_action
      event_data:
        entity_id: binary_sensor.cube_xxxxxxxxxxxxxx
        action_type: free_fall
    action:
      - service: mqtt.publish
        data:
          topic: "xiaomi_cube_state"
          payload: "Free Fall"
          retain: "True"

  - alias: 'Cube Rotate'
    trigger:
      platform: event
      event_type: cube_action
      event_data:
        entity_id: binary_sensor.cube_xxxxxxxxxxxxxx
        action_type: rotate
    action:
      - service: mqtt.publish
        data:
          topic: "xiaomi_cube_state"
          payload: "Rotate"
          retain: "True"

Hope this is somehow helpful for you.

2 Likes

This is great and more importantly highlights that I am not the only one interested in that feature :slight_smile:

I am thinking do we really need all these complications for a simple feature. May be we can simply add another attribute last_action to the cube entity that will save the action. Hoping @syssi or @Danielhiversen can throw some light.

Could this work https://github.com/home-assistant/home-assistant/compare/xiaomi_last_action?expand=1 ?
I have not tested it yet

@Danielhiversen Just tried it and it works well. Thanks!!