Z2M - Xiaomi Cube Controller

I had some issues with this blueprint and used it as inspiration to make a better version: Home-Assistant-Blueprints/zigbee2mqtt_aqara_magic_cube.yaml at 9a63ee03f1d0cf10448fb89a28528ac4105461ac · golles/Home-Assistant-Blueprints · GitHub

4 Likes

Thank you so much for this updated blueprint! I had it working with my cubes a week or two ago with zigbee2mqtt in a separate docker container. But they stopped working and I can’t tell why. The cubes talk to z2m, z2m sends mqtt to hass’s mosquitto, mosquitto shows that the mqtt events are received, the entities in hass show their states, but the automation won’t fire. I don’t get where the disconnect is.

I’m not quite sure how to troubleshoot it since the automation won’t fire at all, so a trace doesn’t exist to debug.

Didn’t work for me.
Checked the automation trace and it stops on the condition.

I have the same issue :frowning:. Things where working for quite a wile. But now the automation stopped working.
I might be related to some recent updates of either homeassistant core, homeassistant host or Zigbee2mqtt .

Home assistant is running on a raspberrypi4 and a CC2531 USB stick is used for Zigbee.
MQTT server runs externally.

The MQTT integration in homeassistant does not show any entities in the Aqara device MQTT info.
It seems like the MQTT addresses exposed by Zigbee2MQTT did change and therefore the auto MQTT discovery in homeassistant no longer works.
Looking at the published MQTT messages (using MQTT Explorer), the only aqara related (retained) topic I see in /homeassistant is: /homeassistant/device_automation/<my_aqara_id>/action_shake/config. Containing the following message:

{
  "automation_type": "trigger",
  "device": {
    "identifiers": [
      "zigbee2mqtt_<my_aqara_id>"
    ],
    "manufacturer": "Xiaomi",
    "model": "Mi/Aqara smart home cube (MFKZQ01LM)",
    "name": "Aqara",
    "sw_version": "Zigbee2MQTT 1.21.2"
  },
  "payload": "shake",
  "subtype": "shake",
  "topic": "zigbee2mqtt/Aqara/action",
  "type": "action"
}

Solved my problem. Not sure how :frowning:
After adding some new zigbee lights, I was not able to add more lights. Google gave me the tip to unplug the zigbee USB stick and plug it in again.
After this action, I was able to add the remaining lights.
The next day I found out that my aqara was working again :slight_smile:
Inspcting the /homeassistant/device_automation/<my_aqara_id> topic now also shows the topics action_wakeup, action_slide, action_flip180 and some others.

I’ve just received my cube but can’t get either of the blueprints in this post to work. The trace gives an error of

Error: UndefinedError: ‘dict object’ has no attribute ‘to_state’

The entities which have been created in HomeAssistant are:

sensor.cube_action
sensor.cube_action_angle
sensor.cube_action_from_side
sensor.cube_action_side
sensor.cube_action_to_side
sensor.cube_battery

If I look at the mqtt package it looks something like this:

{“action”:“rotate_right”,“action_angle”:68.02,“action_from_side”:null,“action_side”:null,“action_to_side”:null,“battery”:74,“linkquality”:108,“side”:2,“voltage”:2955}

I can successfully create a simple automation doing something like watching for a shake or a drop but I’d prefer to use a blueprint which might increase the options.

I’m guessing things have changed in HomeAssistant and maybe zigbee2mqtt recently which has upset the formatting of things. Any help would be much appreciated.

OK - more information - the following entities aren’t updating at all:

sensor.cube_action_angle
sensor.cube_action_from_side
sensor.cube_action_side
sensor.cube_action_to_side

although the MQTT messages are updating. Looks like something is up with how Homeassistant is interpreting the MQTT messages.

I tried to use this blueprint but it fails to work. I’m using Homey as my zigbee hub, this send the data through via mqtt, have mqtt broker & hub running on homey and added mqtt on HA.
The cube from Homey is visible in HA as mqtt device, I see the changes in the cube device but the blueprint does not work.
Do I need to change anything in the blueprint code to get it to work with this setup? is this only for zigbe2mqtt and Conbee II stick or should this also work with my Homey as MQTT broker?

I could not get this to work either. The “action” sensor doesn’t have an action attribute, nor a side attribute. I eventually just switched to using an MQTT trigger, rather than an entity trigger.

trigger:
- platform: mqtt
  topic: !input 'remote_mqtt_topic'
condition:
  - condition: template
    value_template: >-
      {{ trigger.payload_json.action in ('rotate_right', 'rotate_left', 'flip90', 'flip180', 'slide', 'tap', 'shake', 'fall') }}
action:
- variables:
    event: '{{  trigger.payload_json.action }}'
    sub_event: '{{  trigger.payload_json.side }}'
    event_angle: '{{  trigger.payload_json.action_angle }}'

Hello,
The blueprint works well. I spent some time on an automation where the brightness will increase by action angle.

here is the automation action:

service: light.turn_on
target:
  entity_id: light.led_stripe
data_template:
  brightness_step_pct: "{{ trigger.to_state.attributes.action_angle * 0.4 }}"

I hope it helps someone.

2 Likes

I’m seeing the following trigger, which doesn’t match what this blueprint expects:

trigger:
  id: '0'
  idx: '0'
  platform: state
  entity_id: sensor.0x00158d0005d9d322_action
  from_state:
    entity_id: sensor.0x00158d0005d9d322_action
    state: None
    attributes:
      icon: mdi:gesture-double-tap
      friendly_name: cube-k action
    last_changed: '2022-01-07T04:40:57.421837+00:00'
    last_updated: '2022-01-07T04:40:57.421837+00:00'
    context:
      id: ba971f749101817d6781218323d7c368
      parent_id: null
      user_id: null
  to_state:
    entity_id: sensor.0x00158d0005d9d322_action
    state: slide
    attributes:
      icon: mdi:gesture-double-tap
      friendly_name: cube-k action
    last_changed: '2022-01-07T04:40:57.422376+00:00'
    last_updated: '2022-01-07T04:40:57.422376+00:00'
    context:
      id: cd2c98be8ca4a3f9b43d36ac0e28bad9
      parent_id: null
      user_id: null
  for: null
  attribute: null
  description: state of sensor.0x00158d0005d9d322_action

In particular, this blueprint is looking at trigger.from_state.attributes.action, but the data is showing up as just trigger.from_state.state. Did something change at some point? I tried with both legacy=true and legacy=false in zigbee2mqtt.

One quick question. How can I identify which face of de cube is up? Is not possible to use the last face which is up as 0 face? Like the original cube does with Xiaomi app.

Thanks!

I have the same issue, did you find a resolution?

I just go my cube and connected to zigbee2mqtt.

MQTT Explorer shows correct at first then publishs again and then shows null on everything…

{"action":"flip90","action_angle":null,"action_from_side":3,"action_side":5,"action_to_side":5,"battery":38,"linkquality":57,"side":5,"voltage":2875}

{"action":"","action_angle":null,"action_from_side":null,"action_side":null,"action_to_side":null,"battery":38,"linkquality":57,"side":5,"voltage":2875}

I ended up using Node Red as this was correctly reading the MQTT messages. I’ve just rechecked as there have been a few updates of Home Assistant since I originally tried and it looks like the sensors are all working as they should now. @Billy_Freitag the message does just “flash” as the cube is operated the goes back to reporting null. If you trigger on the action going from null to something is should work.

When I get a chance, I’ll investigate automations with the cube again in Home Assistant and retire the Node Red ones.

1 Like

Can you please share your node-red flow? That’s the implementation I would prefer. Thank you.

I think this is the one I used:
https://gist.github.com/iAbadia/901594084e941ebe88a14e393ca32a88

1 Like

I tried the original blueprint v1.2 for the first time with my new sensors, it doesnt work. Error is always:

Test multiple conditions using "and"
Stopped because a condition failed at January 14, 2022, 22:16:26 (runtime: 0.00 seconds)

Result:
result: false
conditions/0


condition: and
conditions:
  - condition: template
    value_template: >-
      {{ trigger.from_state.attributes.action !=
      trigger.to_state.attributes.action }} 
  - condition: template
    value_template: >-
      {{ trigger.to_state.attributes.action in ('rotate_right', 'rotate_left',
      'flip90', 'flip180', 'slide', 'tap', 'shake', 'fall') }}

No matter how simple the setup is, it usually ends in this error. Especially after using some time, it seems to do with this part, i will try to quote it out:

  • condition: template
    value_template: >-
    {{ trigger.from_state.attributes.action !=
    trigger.to_state.attributes.action }}

The one from @golles is working but only for shaking its reliable, other actions sometimes unreliable for some reason. I found that’s because its firing any possible action with the cube, even if you did not configure an action for that specific flip/round/tap etc. When firing an action not configured its not accessible for the right action if you hit that one straight after.

Edit: possible it is due that side 3 is actually the top side with the Aqara logo.

I am currently having the same issue as @Inferi0r with the 1.2 version of the blueprint. No matter which actions I setup, I get “result:false” on the conditions step on my debug. I have validated through mqtt explorer and the state messages in developer tools that the sides are correct. I can see all the state changes happening, and the side 0 is the first one I’ve been attempting to configure but without any success.

Executed: January 17, 2022, 6:11:03 PM
Result:
result: false
conditions/0


condition: and
conditions:
  - condition: template
    value_template: >-
      {{ trigger.from_state.attributes.action !=
      trigger.to_state.attributes.action }} 
  - condition: template
    value_template: >-
      {{ trigger.to_state.attributes.action in ('rotate_right', 'rotate_left',
      'flip90', 'flip180', 'slide', 'tap', 'shake', 'fall') }}

This is the topic and information I see in MQTT for the messages when I rotate right on side 0

{
  "action": "rotate_right",
  "action_angle": 70.46,
  "action_from_side": null,
  "action_side": null,
  "action_to_side": null,
  "angle": -60.48,
  "battery": 100,
  "linkquality": 51,
  "side": 0,
  "voltage": 3005
}

Full automation configuration as well

mode: queued
max: 5
max_exceeded: silent
trigger:
  - platform: state
    entity_id: sensor.aqara_cube_action
condition:
  - condition: and
    conditions:
      - condition: template
        value_template: >-
          {{ trigger.from_state.attributes.action !=
          trigger.to_state.attributes.action }} 
      - condition: template
        value_template: >-
          {{ trigger.to_state.attributes.action in ('rotate_right',
          'rotate_left', 'flip90', 'flip180', 'slide', 'tap', 'shake', 'fall')
          }}
action:
  - variables:
      event: '{{ trigger.to_state.attributes.action }}'
      sub_event: '{{ trigger.to_state.attributes.side }}'
  - service: system_log.write
    data:
      level: info
      message: 'Blueprint Script: {{ event }} {{ sub_event }}'
  - choose:
      - conditions: '{{ event == "slide" }}'
        sequence:
          - choose:
              - conditions: '{{ sub_event == 0 }}'
                sequence: []
              - conditions: '{{ sub_event == 1 }}'
                sequence: []
              - conditions: '{{ sub_event == 2 }}'
                sequence: []
              - conditions: '{{ sub_event == 3 }}'
                sequence: []
              - conditions: '{{ sub_event == 4 }}'
                sequence: []
              - conditions: '{{ sub_event == 5 }}'
                sequence: []
      - conditions: '{{ event == "tap" }}'
        sequence:
          - choose:
              - conditions: '{{ sub_event == 0 }}'
                sequence: []
              - conditions: '{{ sub_event == 1 }}'
                sequence: []
              - conditions: '{{ sub_event == 2 }}'
                sequence: []
              - conditions: '{{ sub_event == 3 }}'
                sequence: []
              - conditions: '{{ sub_event == 4 }}'
                sequence: []
              - conditions: '{{ sub_event == 5 }}'
                sequence: []
      - conditions: '{{ event == "flip90" }}'
        sequence:
          - choose:
              - conditions: '{{ sub_event == 0 }}'
                sequence:
                  - service: light.turn_on
                    target:
                      entity_id: light.office_closet_light
                    data:
                      brightness: 128
              - conditions: '{{ sub_event == 1 }}'
                sequence: []
              - conditions: '{{ sub_event == 2 }}'
                sequence: []
              - conditions: '{{ sub_event == 3 }}'
                sequence: []
              - conditions: '{{ sub_event == 4 }}'
                sequence: []
              - conditions: '{{ sub_event == 5 }}'
                sequence: []
      - conditions: '{{ event == "flip180" }}'
        sequence:
          - choose:
              - conditions: '{{ sub_event == 0 }}'
                sequence:
                  - service: light.turn_on
                    data:
                      brightness: 128
                    target:
                      entity_id: light.office_closet_light
              - conditions: '{{ sub_event == 1 }}'
                sequence: []
              - conditions: '{{ sub_event == 2 }}'
                sequence: []
              - conditions: '{{ sub_event == 3 }}'
                sequence: []
              - conditions: '{{ sub_event == 4 }}'
                sequence: []
              - conditions: '{{ sub_event == 5 }}'
                sequence: []
      - conditions: '{{ event == "rotate_right" }}'
        sequence:
          - choose:
              - conditions: '{{ sub_event == 0 }}'
                sequence:
                  - service: light.turn_on
                    target:
                      device_id: 1613c888150941e2bb2cddf362a294f8
                    data:
                      brightness_step: 10
              - conditions: '{{ sub_event == 1 }}'
                sequence: []
              - conditions: '{{ sub_event == 2 }}'
                sequence: []
              - conditions: '{{ sub_event == 3 }}'
                sequence: []
              - conditions: '{{ sub_event == 4 }}'
                sequence: []
              - conditions: '{{ sub_event == 5 }}'
                sequence: []
      - conditions: '{{ event == "rotate_left" }}'
        sequence:
          - choose:
              - conditions: '{{ sub_event == 0 }}'
                sequence:
                  - service: light.turn_on
                    target:
                      device_id: 1613c888150941e2bb2cddf362a294f8
                    data:
                      brightness_step_pct: -10
              - conditions: '{{ sub_event == 1 }}'
                sequence: []
              - conditions: '{{ sub_event == 2 }}'
                sequence: []
              - conditions: '{{ sub_event == 3 }}'
                sequence: []
              - conditions: '{{ sub_event == 4 }}'
                sequence: []
              - conditions: '{{ sub_event == 5 }}'
                sequence: []
      - conditions: '{{ event == "shake" }}'
        sequence: []
      - conditions: '{{ event == "fall" }}'
        sequence: []
id: '1642466419586'
alias: Aqara Magic Cube (Z2M)
description: ''

Any idea what’s going wrong here? I’ve tried side 3 for some actions as well, same results.

Is it possible to remove this sub condition

- conditions: '{{ sub_event == 0 }}'
                sequence: []

I don’t want to add all scenarios for each side.
For me enaught just to rotate in any side.

Im trying to configure blueprint, unfortunately in GUI i dont see device (magic_cube).
Im using Z2m with conbee II and i have aqara magic cube on the list, working properly.

Any idea?