Z2M - Xiaomi Cube Controller

This blueprint is one of several that addresses the challenge of creating actions for ‘remote-control’ devices. The Aqara Cube can have as many as 32 actions (5 events x 6 sub-events = 30 + 2 events). Most of the work is defining the actions.

For the curious, here’s how it could be done without a blueprint.

Create a script for each action and name each script using a consistent format. For example:

script.cube1_flip_3

That means it’s the script for the sensor called cube1 when its event isflip and its sub-event is 3.

Make as many scripts as you need for each cube, which means up to 32 scripts for a single Aqara Cube (it’s the same amount of work as filling out a blueprint).

This one short automation handles all actions for two Aqara Cubes:

alias: example 1
trigger:
- platform: event
  event_type: state_changed
  event_data:
    entity_id: sensor.cube1
- platform: event
  event_type: state_changed
  event_data:
    entity_id: sensor.cube2
action:
- service: script.turn_on
  data:
    entity_id: "script.{{trigger.to_state.object_id}}_{{ trigger.event.data.old_state.state }}_{{ trigger.event.data.old_state.attributes.side }}"

Anyway, that would be one of the ‘old-school’ ways of doing it. :slight_smile:

Good job!

I just wondering if it is possible to have a global condition (time period) to disable multiple toggles. Since I have multiple coordinators. Thx.

I want to look at that, but it is difficult for me to test this customization. Do you have an example of another automation in which you have been able to circumvent this problem? Do I understand correctly that you are using 2 coordinators in the same zigbee network and publishing in the same mqtt topic?

Thanks. Here is one of my automation tos disable repeated actions (> 2s).

I do find that it becomes more common on the Xiaomi single button. It always publishes two topics at the same time. Here is the discussion for ‘zigbee2mqtt’. https://github.com/Koenkk/zigbee2mqtt/issues/649https://github.com/Koenkk/zigbee2mqtt/issues/1309

alias: fall cube for corner light
description: ''
trigger:
  - platform: device
    domain: mqtt
    device_id: ***
    type: action
    subtype: fall
    discovery_id: **** action_fall
condition:
  - condition: template
    value_template: >
      {{ (as_timestamp(now()) - as_timestamp(states.switch.sw3.last_changed)) >
      2 }}
action:
  - type: toggle
    device_id: ***
    entity_id: switch.sw3
    domain: switch
mode: single

For future reference, this Template Condition:

condition:
  - condition: template
    value_template: >
      {{ (as_timestamp(now()) - as_timestamp(states.switch.sw3.last_changed)) >
      2 }}

can be reduced to this:

condition: "{{ now().timestamp() - states.switch.sw3.last_changed.timestamp() > 2 }}"

It uses shorthand notation and takes advantage of the built-in timestamp() method in now() and an entity’s State Object.

2 Likes

Hi There!

Thanks for great blueprint! Sadly I’ve faced an issue - laggy turning actions.
I’m using these to dim/brighten my mesh lights with zigbe2mqtt and Conbee II stick. With ZHA I had it with queued option seted to 5 and it was working fast and accurate. Now it’s roulette if the lights will dimm or not. Sometimes no reaction at all and sometimes it goes with all delayed actions. In general this should dim/brigten different lights based on side action. Any tips?
Thanks in advance!

hi thanks for sending this feedback. During testing, I noticed that it does not always go smoothly. With the adjustment you mentioned (queued option set to 5) it seems to be going better.

I will update the blueprint, you could test whether it is better for you?

Hi!
Thanks a lot for quick action. I’ve tested your new version but still facing delays and no action at all.

My default setup is:
side:0 turning cw/ccw is adjusting brightness of lamp 1,
side:3 cw/ccw is adjusting brighness of lamp 2
and accordingly:
side:0 tap toggles on/off for lamp 1 and side:3 toggles on/off for lamp 2.

Issues I’m facing:

  • toggle light is not working somehow, lights are just flashing once and turning off again (Have to use light.turn_on),
  • actions are not taken at all or with huge delay (all of these),
  • random actions are working (increasing brigtness is working but reducing not).

HA log shows that action is taken and automatization is released but nothing happen (I’ve noticed that for one automatization released there is quite some entries - I will provide screenshot) and Zigbee2MQTT log is also showing that actions are registered. I have Hassio running on Raspberry Pi 4 with 4GB RAM and maybe that is the reason why is so laggy? From the other hand when I was using cracked Xiaomi Gaterway 3 with ZHA actions were taken almost immediately but I really want fancy option to dim different lights according to cube side :wink:

I will test in a moment if Cube connected to Xiaomi Gateway with ZHA is working faster and if ordinary automatization just for one action a time is also giving better results.

sorry to hear that it is not working properly yet. I cannot reproduce the problems you describe and it seems to work well here with a similar automation: Turn to dim (side-0) and Shake or fall to turn off.

In your first post your problems sounded worse than I was experiencing and also immediately wondered if there might be some other cause such as overloaded hardware. Hopefully you have the option to rule this out somehow and maybe someone else can check this out

I am using a synology DS420 + and HA, MQTT, Z2M run in its own docker containers. And as coordinator a CC2538 Flashed with: https://github.com/Koenkk/Z-Stack-firmware

I see the same issues with light.toggle, it flashes and returns to the original state. It appears to be specifically z-wave devices for my setup, zigbee/wifi lights don’t experience it. I can issue the same light.toggle service call from the HA console and it works as expected, seems like something subtle is going on here. Just wanted to add my 2c.

The problem is that this automation sends the action 2 times. With scripts happen the same it executes them two times., if you have a toggle script it doesn’t work.

I also find a double action when setting toggle - thanks for this, its enabled me to get going and great fun, I wouldn’t have known how to use it otherwise, appreciated!

Did anyone find a solution to avoid the double action? It seems like the zigbee2mqtt message comes in two times.

This blueprint requires an entity in the first GUI field.
Z2M does not expose an action entity for me, with several tries to re-pair.
How to add the cube to this blueprint then?

Hi,

for filtering double actions, you can use the following 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") }}

best regards,
Max

1 Like

Thanks Max… where would this condition go?

Hi,
i use a automation, based on leow example

- alias: Cube1                                                                                                                                                           
  description: ''                                                                                                                                                        
  trigger:                                                                                                                                                               
  - platform: state                                                                                                                                                      
    entity_id: sensor.cube1_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:                                                                                                                                                                
  - service_template: >-
      {% if trigger.to_state.attributes.action in ("rotate_right", "rotate_left", "slide", "tap") %}
      script.cube_side{{ trigger.to_state.attributes.side}}_{{ trigger.to_state.attributes.action }} 
      {% elif trigger.to_state.attributes.action in ("shake", "fall") %} 
      script.cube_{{ trigger.to_state.attributes.action }}                                                                                       
      {% else %} 
      script.none 
      {% endif %}

But you can import it to the blueprint too. (untested, but i think it must work so)

[...]
trigger:
- platform: event
  event_type: state_changed
  event_data:
    entity_id: !input 'remote'
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.event.data.new_state.state }}'
    sub_event: '{{ trigger.event.data.new_state.attributes.side }}'
[...]

For some reason this just comes up with an error in the log:

UndefinedError: 'dict object' has no attribute 'from_state'
1 Like

I’m getting the same error when using @mbacks 's condition in the template.

Which code block did you use? the blueprint was untested. when i think about it, with platform: event, there is no trigger.from_state object, so my blueprint codeblock cant work. i need for that more than 5 minutes to rewrite the blueprint. i try to have a look on it tomorrow…

2 Likes