Z2M - Xiaomi Cube Controller

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?

Did you ever figure this out? I have the same issue.

This would be a lot easier for your users to import and keep track of if you add an import link…
Simply fill this in and then add the markdown code to the original message…

Like this:

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Looks like this in Markdown…

[![Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.](https://my.home-assistant.io/badges/blueprint_import.svg)](https://my.home-assistant.io/redirect/blueprint_import/?blueprint_url=https%3A%2F%2Fcommunity.home-assistant.io%2Ft%2Fz2m-xiaomi-cube-controller%2F263006)

As far as the Blueprint itself goes, it’s awesome and works perfectly.
You may want to add in the initial message ( or better yet the Description of ‘sensor to use’ inside the blueprint ) what sensor to use to get the is to work. I see people seem to be confused because all you need exported to HA to make this work is the sensor.##Device_Name##_action sensor and that is what you put in for the controller to use question. This may seem obvious, but not for everyone. The rest of the sensors can be hidden unless you want to watch battery or signal from diagnostics.

Thanks for the great code!!

What about lowering the brightness?
Is it as simple as adding a negative sign in the template?

UPDATE…
I figured this out and put the new code in my ‘forked’ version of this blueprint.

CW is a positive angle and CCW is a negative angle, so don’t futz with the sign…

Thanks for the idea.

Thanks for this bud, works flawless for me and love that I can change scenes using the rotate clockwise and counter clockwise

I see a couple of other changes that I think should be made.
I believe I’m going to ‘fork’ off of this and start another version. The logging should not be on all the time, and I have a better way to display the last action to a sensor instead, so that you can troubleshoot and generate the actions easier.
That along with some of the descriptions I think a few tweaks like that would make this a lot better.
The basic code is sound and doesn’t need a thing.

Community link for my forked version of this blueprint:

source_url from GitHub: https://github.com/SirGoodenough/HA_Blueprints/blob/master/Automations/Zigbee2MQTT%20-%20Xiaomi%20Cube%20Controller.yaml
Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Event Sensor in Action:

Sample Script Generation Screen

1 Like