šŸ§Æ Zigbee2MQTT - Xiaomi Cube Controller

In case anyone else needs help splitting up Sir_Goodenoughā€™s super helpful YAML for Seeing the cube commands for training the operator, hereā€™s what I have done in my separate template.yaml file that contains a bunch of other stuff not related to this thread:

configuration.yaml:
template: !include template.yaml

template.yaml

- sensor:
# bunch of
# other stuff
# not related
# to this thread

##################################################
# Aqara Cube Blueprint Event Listener
# https://community.home-assistant.io/t/zigbee2mqtt-xiaomi-cube-controller/393203
# After this, add an entity card in your dashboard for sensor.cube_last_action to see what actions occur as you move the cube.
##################################################
- trigger:
  - platform: event
    event_type: cube_last_action
  sensor:
    - name: "Cube Last Action"
      unique_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
      icon: mdi:eye-refresh-outline
      attributes:
        friendly_name: "Cube Action"
      state: >
        {{ trigger.event.data.friendly_name }} - 
        {{ trigger.event.data.action }} - 
        {{ trigger.event.data.side }} frm 
        {{ trigger.event.data.last_side }}

Tip: You donā€™t have to restart HA for this to take effect. Just go to sidebar Developer Tools > go down to the ā€œYAML configuration reloadingā€ section > click ā€œTEMPLATE ENTITIESā€

If you use the Code Server / VS Code for editing, you can also restart anything from there using right click in a blank area or the F1 key to get to the command pallet.

1 Like

i was able to get this blueprint working but it seems to be unstable for me.

I am on Home Assistant 2022.8.7 and am seeing this error in the logs this is just after restarting home assisant and not even using the automation yet:

Logger: homeassistant.components.automation
Source: components/automation/__init__.py:242
Integration: Automation ([documentation](https://www.home-assistant.io/integrations/automation), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+automation%22))
First occurred: 7:57:03 AM (2 occurrences)
Last logged: 7:57:03 AM

* Blueprint Aqara Magic Cube Zigbee2MQTT - 2022-05-05 generated invalid automation with inputs OrderedDict([('remote', 'sensor.magic_cube_action'), ('flipped90_face_0', [OrderedDict([('service', 'light.turn_on'), ('data', OrderedDict([('brightness_pct', 98), ('color_name', 'purple')])), ('target', OrderedDict([('device_id', 'c44e5f4ded082a8473581468057dc043')]))])]), ('flipped90_face_2', []), ('rotate_cw_face_2', []), ('flipped90_face_1', [OrderedDict([('service', 'light.turn_on'), ('data', OrderedDict([('brightness', 255), ('color_name', 'red')])), ('target', OrderedDict([('device_id', 'c44e5f4ded082a8473581468057dc043')]))])]), ('rotate_cw_face_0', [OrderedDict([('service', '')])]), ('rotate_ccw_face_0', [OrderedDict([('service', 'light.turn_on'), ('data', OrderedDict([('brightness_step_pct', -10)])), ('target', OrderedDict([('device_id', 'c44e5f4ded082a8473581468057dc043')]))])]), ('shake', [OrderedDict([('service', 'media_player.play_media'), ('data', OrderedDict([('media_content_id', 'https://music.youtube.com/watch?v=ZxDlvx-s3lo'), ('media_content_type', 'music')])), ('target', OrderedDict([('device_id', '7feaf0fe9e51cfb7226cc38661de7178')]))])])]): Service does not match format <domain>.<name> for dictionary value @ data['action'][3]['choose'][4]['sequence'][0]['choose'][0]['sequence'][0]['service']. Got None
* Blueprint Aqara Magic Cube Zigbee2MQTT - 2022-05-05 generated invalid automation with inputs OrderedDict([('remote', 'sensor.magic_cube_action'), ('flipped90_face_0', [OrderedDict([('service', 'light.turn_on'), ('data', OrderedDict([('rgb_color', [255, 0, 208]), ('brightness_pct', 100)])), ('target', OrderedDict([('device_id', 'c44e5f4ded082a8473581468057dc043')]))])]), ('flipped180_face_1', []), ('flipped90_face_1', [OrderedDict([('service', 'light.turn_on'), ('data', OrderedDict([('rgb_color', [255, 0, 0])])), ('target', OrderedDict([('device_id', 'c44e5f4ded082a8473581468057dc043')]))])]), ('rotate_cw_face_1', [OrderedDict([('service', 'light.turn_on'), ('data', OrderedDict([('brightness_step_pct', 10)]))])]), ('rotate_ccw_face_1', [OrderedDict([('service', 'light.turn_on'), ('data', OrderedDict([('brightness_step_pct', -10)])), ('target', OrderedDict([('device_id', 'c44e5f4ded082a8473581468057dc043')]))])]), ('rotate_cw_face_2', [OrderedDict([('service', ''), ('data', OrderedDict())])])]): Service does not match format <domain>.<name> for dictionary value @ data['action'][3]['choose'][4]['sequence'][0]['choose'][2]['sequence'][0]['service']. Got None

It appears to me to show errors with the automation(s) you built, not the blueprint it is calling.
Can you share the automation(s)?

From what I can read from that error, you have 2 automations with partially filled in entries.
The first one

('rotate_cw_face_0', [OrderedDict([('service', '')])]), 

which fails as

Service does not match format <domain>.<name> for dictionary value @ data['action'][3]['choose'][4]['sequence'][0]['choose'][0]['sequence'][0]['service']. Got None

The other one

('rotate_cw_face_2', [OrderedDict([('service', ''), ('data', OrderedDict())])])]): 

which fails as

Service does not match format <domain>.<name> for dictionary value @ data['action'][3]['choose'][4]['sequence'][0]['choose'][2]['sequence'][0]['service']. Got None

Itā€™s only stable if you give it valid sequences when building an automation. I doubt seriously that these automations passed config check before you restarted Home Assistant.

ok this helped - turns out in the autmoations yaml it can created a few orphaned versions of the automation. iā€™m checking now if itā€™s working ok.

My daughter loved this cube and your blueprint made it really easy to be a good Dad thank you!

1 Like

Holy shizzā€¦ this is magic bro. I owe you a beer. :beer:

1 Like

There are links around to buy me one if you are so inclinedā€¦ (end of the top message)

How can I fix this?

Logger: homeassistant.components.automation
Source: components/automation/__init__.py:255
Integration: Automation (documentation, issues)
First occurred: 10:46:04 (4 occurrences)
Last logged: 10:46:04

Blueprint Aqara Magic Cube Zigbee2MQTT - 2022-05-05 generated invalid automation with inputs OrderedDict([('remote', 'sensor.cube_1_living_room_action_angle'), ('slide_any', []), ('doubletap_any', [OrderedDict([('type', 'turn_on'), ('device_id', 'dab8e2497c63bd8c393d94f80ae89456'), ('entity_id', 'light.0x00178801036fb1ab'), ('domain', 'light'), ('flash', 'short')]), OrderedDict([('device_id', ''), ('domain', ''), ('entity_id', '')])]), ('flipped90_any', [OrderedDict([('type', 'turn_off'), ('device_id', 'dab8e2497c63bd8c393d94f80ae89456'), ('entity_id', 'light.0x00178801036fb1ab'), ('domain', 'light')])]), ('rotate_cw_any', [OrderedDict([('device_id', 'dab8e2497c63bd8c393d94f80ae89456'), ('domain', 'light'), ('entity_id', 'light.0x00178801036fb1ab'), ('type', 'brightness_increase')])]), ('rotate_ccw_any', [OrderedDict([('device_id', 'dab8e2497c63bd8c393d94f80ae89456'), ('domain', 'light'), ('entity_id', 'light.0x00178801036fb1ab'), ('type', 'brightness_decrease')])])]): Integration '' not found. Got <homeassistant.components.blueprint.models.BlueprintInputs object at 0x7fd493fc1060>
Blueprint Aqara Magic Cube Zigbee2MQTT - 2022-05-05 generated invalid automation with inputs OrderedDict([('remote', 'sensor.cube_1_living_room_action'), ('rotate_ccw_any', [OrderedDict([('device_id', ''), ('domain', ''), ('entity_id', '')])])]): Integration '' not found. Got <homeassistant.components.blueprint.models.BlueprintInputs object at 0x7fd4940209d0>
Blueprint Aqara Magic Cube Zigbee2MQTT - 2022-05-05 generated invalid automation with inputs OrderedDict([('remote', 'sensor.0x00158d0007e39ff6_action'), ('shake', [OrderedDict([('service', 'scene.create'), ('data', OrderedDict([('scene_id', 'antesVictor'), ('snapshot_entities', 'light.hue_2_victor')]))]), OrderedDict([('repeat', OrderedDict([('count', '5'), ('sequence', [OrderedDict([('service', 'light.turn_on'), ('data', OrderedDict([('rgb_color', [0, 255, 0])])), ('target', OrderedDict([('device_id', 'bfa6e23d5dc48fdd1e384f747737d3b6')]))]), OrderedDict([('delay', OrderedDict([('hours', 0), ('minutes', 0), ('seconds', 1), ('milliseconds', 0)]))]), OrderedDict([('service', 'light.turn_on'), ('data', OrderedDict([('rgb_color', [0, 255, 255])])), ('target', OrderedDict([('device_id', 'bfa6e23d5dc48fdd1e384f747737d3b6')]))]), OrderedDict([('delay', OrderedDict([('hours', 0), ('minutes', 0), ('seconds', 1), ('milliseconds', 0)]))])])]))]), OrderedDict([('service', 'scene.turn_on'), ('target', OrderedDict([('entity_id', 'antesVictor')])), ('metadata', OrderedDict())])])]): not a valid value for dictionary value @ data['action'][2]['choose'][6]['sequence'][2]['target']['entity_id']. Got None
Blueprint Aqara Magic Cube Zigbee2MQTT - 2022-05-05 generated invalid automation with inputs OrderedDict([('remote', 'sensor.cube_2_babaciuni_room_action'), ('rotate_cw_any', [OrderedDict([('device_id', '32dc34d856fb18390539e535b30dee96'), ('domain', 'light'), ('entity_id', 'light.hue_bulb_babaciuni'), ('type', 'brightness_increase')]), OrderedDict([('device_id', ''), ('domain', ''), ('entity_id', '')])])]): Integration '' not found. Got <homeassistant.components.blueprint.models.BlueprintInputs object at 0x7fd493ef72b0>

Well, first of all you have 4 failed attempts at answering the blueprint questions. they appear to be incomplete (blank entities), one has a bad remote sensor selected ( sensor.cube_1_living_room_action_angle).

My suggestion is to delete all four, and start over. Get one working with one simple action like toggle a light, and add on to it or change it after you figure it out.

1 Like

I have started getting the following error every minute. I sure it used to work fine, and tbh I can still use the cube for what I want (most the timeā€¦). Any idea how to fix this?

2022-10-21 10:30:46.031 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'trigger' is undefined when rendering '{{ trigger.event.data.friendly_name }} - {{ trigger.event.data.action }} - {{ trigger.event.data.side }} frm {{ trigger.event.data.last_side }}'

Here aremy trigger and template sensors from my templates.yaml file

  - trigger:
      - platform: event
        event_type: cube_last_action
  - sensor:
      - name: "Cube Last Action"
        unique_id: cubelastaction
        icon: mdi:eye-refresh-outline
        attributes:
          friendly_name: "Cube Action"
        state: >
          {{ trigger.event.data.friendly_name }} - 
          {{ trigger.event.data.action }} - 
          {{ trigger.event.data.side }} frm 
          {{ trigger.event.data.last_side }}

Cheers

That is not the correct format for the event sensor.
If you donā€™t use or need the event sensor, you can just delete that. Itā€™s optional. Otherwise check back and fix the type.
Link to the correct code

You can see where I have it in my template stack in my Home Assistant Config

I need some help getting my cube to respond to Z2M blueprints.

I set the device in the blueprint to ā€œcube_living_room_actionā€ but the blueprint never triggers. I can see from the event log that the action is logged whatever I do with the cube.

I have Appdaemon with Controllerx installed but I have not configured the cube in Appdaemon.

I can set up a new automation with no blueprint and set an action to the ā€œshakeā€ trigger and it works.

Hi. This is my first blueprint setup. Not sure how it suppose to work, but I think I am following the steps correctly. When I add a simple turn on a light action in the blueprint via ui, it doesā€™t seem to save the settings after clicking on the save button. When I click on the traces, there is no traces.

HA is seeing the device and all these sensors.
2022-10-23_7-57-39

This is what I have input in the blueprint ui. When I go back to this same menu after saving the settings, the sensor is not save nor is the action.

Update: I figured out my issue. I was had moved my automations to a folder and blueprint by default created the automations in automations.yaml. I needed to reference that file in the main config file. Working now.

1 Like

Without more information, all I can tell you is look at the traces and figure out what it is doing. Also remember that 'runā€™ning the automation from within the UI does not cause the cube to trigger anything. You need to save it, make sure there are no syntax errors, and activate the function on the cube that matches what you programmed in.Shake or drop are the easiest ones to activate, so testing with those gives a better chance of success.

There are no errors in the blueprint. I set the device and an action then move the cube. The blueprint does not trigger , the last used timestamp is ā€œneverā€ so there is nothing to trace.

The home assistant log shows that input from the cube works as does making a blank automation without a blueprint.

alias: Cube living room
description: ""
use_blueprint:
  path: SirGoodenough/Zigbee2MQTT - Xiaomi Cube Controller.yaml
  input:
    remote: sensor.cube_living_room_action
    slide_face_0:
      - service: light.turn_off
        data: {}
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.kitchen_light_outlet_local
    rotate_cw_face_0:
      - service: media_player.volume_up
        data: {}
        target:
          entity_id: media_player.sony_ke_48a9
    rotate_ccw_face_0:
      - service: media_player.volume_down
        data: {}
        target:
          entity_id: media_player.sony_ke_48a9

Here is a working automation without using the blueprint

alias: cube test
description: ""
trigger:
  - platform: device
    domain: mqtt
    device_id: c192d63506aee91d9cda805a15bbd43b
    type: action
    subtype: shake
    discovery_id: 0x00158d00051e7302 action_shake
condition: []
action:
  - service: switch.toggle
    data: {}
    target:
      entity_id: switch.kitchen_light_outlet_local
mode: single

This doesnā€™t expose the same trigger though but it proves at least that the cube works.

The event log that I have added to the blueprint or something else? If itā€™s from the event log in the blueprint, the Blueprint is working and there is a problem with the programmed actions.

Also I do not believe that is validā€¦ The light.turn_off needs an entity or syntax should be bad.

Delete that automation with all the volumes and everything. Do a simple blueprint automation with the code you used for the successful testā€¦ 1 action for drop or shake. Make it simple. Build from there.

  - service: switch.toggle
    data: {}
    target:
      entity_id: switch.kitchen_light_outlet_local

So i re-paired the Cube and created a test automation without a blueprint when shaked. The switch turns on / off.

I deleted this test automation and created a new one based on the blueprint

alias: Cube
description: ""
use_blueprint:
  path: SirGoodenough/Zigbee2MQTT - Xiaomi Cube Controller.yaml
  input:
    shake:
      - service: switch.toggle
        data: {}
        target:
          entity_id: switch.kitchen_light_outlet_local
    remote: sensor.cube_action

The cube action is logged in the HA logbook but the blueprint is never triggered. It says ā€œlast triggered - neverā€.

It feels like the trigger event is the root of the problem. " remote: sensor.cube_action" i suspect does not seem to be listened to. I am using HA 2022.10.3.

The cube worked fined with blueprints when i used ZHA but so far itā€™s the only device giving me an issue now iā€™ve moved to z2m.

Is the stuff at this link correct from the installation Docs?

Does this actually exist as an entity?

Itā€™s available as an entity but Iā€™m wondering if itā€™s doing anything.

Iā€™m not making up the yaml myself, I use the gui builder :blush:

Hi. I am using the cube_dimmer_control script in the first post and seeing these errors in log. Lights are dimmer both directions, however. Any ideas how to remove the errors? Using ikea lights.

error:

Logger: homeassistant.components.script.cube_dimmer_control
Source: helpers/script.py:409
Integration: Script (documentation, issues)
First occurred: 5:28:43 AM (6 occurrences)
Last logged: 5:28:47 AM

cube_dimmer_control: Error executing script. Invalid data for call_service at pos 1: value must be at most 100 for dictionary value @ data['brightness_pct']

-
Logger: homeassistant.components.automation.cube_livingroom_bright_toggle
Source: components/automation/__init__.py:567
Integration: Automation (documentation, issues)
First occurred: 5:28:43 AM (6 occurrences)
Last logged: 5:28:47 AM

Error while executing automation automation.cube_livingroom_bright_toggle: value must be at most 100 for dictionary value @ data['brightness_pct']

-
Logger: homeassistant.components.automation.cube_livingroom_bright_toggle
Source: helpers/script.py:409
Integration: Automation (documentation, issues)
First occurred: 5:28:43 AM (18 occurrences)
Last logged: 5:28:47 AM

Cube Livingroom Bright Toggle: Group 1 šŸŽ These are action events based on the action broken down by side: choice 5: These are rotate CW events: choice 1: Error executing script. Invalid data for call_service at pos 1: value must be at most 100 for dictionary value @ data['brightness_pct']
Cube Livingroom Bright Toggle: Group 1 šŸŽ These are action events based on the action broken down by side: choice 5: Error executing script. Invalid data for choose at pos 1: value must be at most 100 for dictionary value @ data['brightness_pct']
Cube Livingroom Bright Toggle: Error executing script. Invalid data for choose at pos 4: value must be at most 100 for dictionary value @ data['brightness_pct']

script:

  cube_dimmer_control:
    description: Template Dimmer Control
    variables:
      light:    
      angle:
    sequence:
      - service: light.turn_on
        data_template:
          entity_id: "{{ light }}"
          brightness_pct: >
            {% set step_size = angle * 0.4 %}
              {# Get brightness as a percent. #}
            {% set cb = (state_attr( light, 'brightness') | float(10) / 255.0) * 100.0 %}
            {% set new_brightness = cb | int(10) + step_size %}
            {% if new_brightness < 5 %}
              {# If it gets really low set to 0. Adjust if needed for your lights. #}
              0
            {% elif new_brightness <= 10 %}
              {# If it's not quite 0, set to a minimum working brightness. #}
              10
            {% elif 91 <= new_brightness < (90 + step_size) %}
              {# If it's almost full brightness, set to 100%. #}
              100
            {% else %}
              {# Send actual calculated value. #}
              {{ new_brightness }}
            {% endif %}

ikea light:

min_mireds: 250
max_mireds: 454
supported_color_modes:
  - color_temp
color_mode: color_temp
brightness: 255
color_temp: 250
hs_color:
  - 26.812
  - 34.87
rgb_color:
  - 255
  - 205
  - 166
xy_color:
  - 0.421
  - 0.364
friendly_name: living
supported_features: 40