Automation for a Xiaomi Cube with ZHA component

Anyone knows how to do automation for a Xiaomi Cube with ZHA component?
https://i.imgur.com/49cEkbb.png

Hi @Adb , did you manage to find a solution for this. I am currently trying to set up Cube automations with ZHA and would welcome any pointers - thanks!

Actually i ended up buying the Xiaomi hub, newer got this to work.

Soo… did you find a solution for this? :smiley:

My solution was buying the Xiaomi hub :slight_smile:

I deleted and re-added my directly with ZHA, and the quirks seem to take care of this device quite well, nowadays – the trick is knowing what events to look out for. I managed to get some features going with the following in automation: , although I would have needed more testing time to control with more ‘advanced’ gestures (like double-flip).

- alias: Cube left vol down
  description: 'When the Control Cube is rotated left then turn down the volume'
  trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_ieee: '00:15:8d:00:12:34:56:78'
      command: 'rotate_left'
  action:
    service: media_player.volume_set
    data_template:
      entity_id: media_player.spotify
      volume_level: "{{state_attr('media_player.spotify', 'volume_level')|round(2)|float - 0.03 | float}}"

- alias: Cube right vol up
  description: 'When the Control Cube is rotated right then turn up the volume'
  trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_ieee: '00:15:8d:00:12:34:56:78'
      command: 'rotate_right'
  action:
    service: media_player.volume_set
    data_template:
      entity_id: media_player.spotify
      volume_level: "{{state_attr('media_player.spotify', 'volume_level')|round(2)|float + 0.03 | float}}"

- alias: Cube shake play
  description: 'When the Control Cube is shaken then play the music'
  trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_ieee: '00:15:8d:00:12:34:56:78'
      command: 'shake'
  action:
    service: media_player.media_play
    data_template:
      entity_id: media_player.spotify

- alias: Cube flip pause
  description: 'When the Control Cube is flipped then pause the music'
  trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_ieee: '00:15:8d:00:12:34:56:78'
      command: 'flip'
  action:
    service: media_player.media_pause
    data_template:
      entity_id: media_player.spotify

HTH

1 Like

I actually solved it before you replies. I went to NodeRed and made a Hass Event node and plugged it into Debug node. This makes it so it logs all events that Hass spits out. From there it’s very trivial to find the name for ZHA Events ( zha_event), node names, all the data you need and such. And it’s possible to actually pass rotation degrees data from Cube to automation by using a template node containing

{"data":
    {"brightness_step_pct":
    {{payload.event.args.relative_degrees}}
    }
}

which feeds directly into the call service node and populates the Data field automatically when run. So with just 1 command I can now control brightness of a light by rotating it in either direction, as it gives negative values when rotated left. And I can rotate it like 200 degrees to bring light to 100% in one turn, or turn it just a bit to change its brightness a little.

Extra bonus is that Cube actually also passes you the side it’s on when flipped (could be when turned too, not sure). Either way you can use this information to control up to 6 ‘knobs’ by assigning one to each side of the cube. Gotta label them though :smiley:

Can’t imagine how messy it would be to create it in Hass’s native automations. I like them, but for IFTTT stuff mostly, complex logic like this is better handled in NodeRed

@Adb you now have two solutions to go with and get rid of the hub :slight_smile: If you wish. Feel free to mark either of our solutions, so this topic shows up as solved for others looking for a solution

Great, Michael - two solutions are better than one. I still prefer to hand code solutions in HA for the moment, but it’s not always easy and I will doubtless dip my toe into Node Red soon as the whole project is moving away from config files

Admittedly both these solutions use ZHA, but I think that’s a testament to how much hard work has been put into it by dmulcahey and friends.

Mhm :slight_smile: NodeRed’s great, and is very easy to get set up and running as an Addon, if you run Hass.io (HomeAssistant after rebranding :smiley: ). It’s not even comparable to Hass’s own automation system, though. Has is basically IFTTT, trigger -> conditions -> actions, that’s it. NodeRed has IF\ELSE logic as well as a lot of different logic operators My whole automation for 2 cubes is just 1 flow: https://i.imgur.com/KvBIFCl.png

First switch to split based on device ID, second switch is for action tag (rotate, flip, etc), third is for 90\180 if it’s flip. Cringe when I think about how bulky this same thing would be in native Hass

Hi, I don’t have NodeRed running, but I also want this to work in my automation.

The ZHA_event gives me this:

{
    "event_type": "zha_event",
    "data": {
        "device_ieee": "xx:xx:xx:xx:xx:xx:xx:xx",
        "unique_id": "xx:xx:xx:xx:xx:xx:xx:xx:x:0x000c",
        "endpoint_id": 3,
        "cluster_id": 12,
        "command": "rotate_right",
        "args": {
            "relative_degrees": 33.82000732421875
        }
    },
    "origin": "LOCAL",
    "time_fired": "2020-09-19T06:09:16.663123+00:00",
    "context": {
        "id": "a5e8c42afa3e11ea9683572a597b6b59",
        "parent_id": null,
        "user_id": null
    }
}

I can use an automation which triggers on this, no problem, but how do I get the value relative_degrees out of the trigger?
I tried:

{{trigger.event.args.relative_degrees}}
{{trigger.event.attributes.relative_degrees}}
{{trigger.to_state.attributes.relative_degrees}}

Suggestions?

Figured it out :wink:

It is

trigger.event.data.args.relative_degrees