Zigbee2mqtt: aqara cube automations in home assistant

Created on 7 Nov 2018  路  20Comments  路  Source: Koenkk/zigbee2mqtt

I already postet this in the HA community, but afterwards I think the better chance to get an answer
is here.

I wanted to get rid of the Aqara Control gateway and so I bought a cc2531 zigbee sniffer.

My doors sensors work fine already.

But I have some problems with the Cube.

First it is not autodetected like the other senors, so I had to implement this:

- sensor:
  - platform: "mqtt"
    name: Cube state
    state_topic: "zigbee2mqtt/0x00XXXXXXXX"
    availability_topic: "zigbee2mqtt/bridge/state"
    icon: "mdi:gesture-double-tap"
    value_template: "{{ value_json.action }}"
    json_attributes: 
      - "battery"
      - "voltage"
      - "angle"
      - "side"
      - "from_side"
      - "to_side"
      - "brightness"
      - "angle_x_absolute"
      - "angle_y_absolute"
      - "angle_z"
      - "angle_y"
      - "angle_x"
      - "unknown_data"
    force_update: true

This is an example from here https://github.com/Koenkk/zigbee2mqtt/wiki/Integrating-with-Home-Assistant 1

Before I had a working automation like this:

- alias: Cube - flip90
  initial_state: 'on'
  trigger:
    platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_XXXXXXX
      action_type: flip90
  action:
    - service: homeassistant.toggle
      entity_id: light.flaschen

With the new mqtt sensor I tried this:

- alias: Cube - flip90
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: sensor.cube_state
    to: 'flip90'
  action:
    - service: homeassistant.toggle
      entity_id: light.flaschen

The problem is that toggling the light will not work, because the state does not change. So if flip90 changes to flip90 again the automation will not do anything.

How can I make this work?

The next thing that does not work is using the rotation as a dimmer function
this one worked before, but I have no clue how to change it to make it work with the mqtt sensor.

- alias: Cube - rotate
  initial_state: 'on'
  trigger:
    - platform: event
      event_type: cube_action
      event_data:
        entity_id: binary_sensor.cube_XXXXXXX
        action_type: rotate
  action:         
    - service: light.turn_on
      data:
        entity_id: light.kugellicht
        color_name: darkturquoise
    - service: light.turn_on
      data_template:
        entity_id: light.kugellicht
        brightness: >-
          {% if trigger.event.data.action_value | float > 0 %}
          {{  states.light.kugellicht.attributes.brightness | int + 30 }}
          {% else %}
          {{  states.light.kugellicht.attributes.brightness | int - 30 }}
          {% endif %}

Most helpful comment

Adding some lines to the above automation, will add the function to turn off the lights. Here is the complete automation (you can add multiple of these automations, just change accordingly; cubemode...):

- alias: Cube - Licht - rotate
  initial_state: 'on'
  trigger:
    platform: mqtt
    topic: 'zigbee2mqtt/Zigbee Cube'
  condition:
    - condition: template
      value_template: "{{ trigger.payload_json.action in ['rotate_right', 'rotate_left'] }}"
    - condition: state
      entity_id: input_select.cube_mode
      state: 'Licht'  
  action:         
    - service: light.turn_on
      data:
        entity_id: light.trainingsraum
    - service: light.turn_on
      data_template:
        entity_id: light.trainingsraum
        brightness: >-
          {% if trigger.payload_json.angle | float > 0 %}
          {{  states.light.trainingsraum.attributes.brightness | int + (trigger.payload_json.angle | int) * 2 }}
          {% else %}
          {{  states.light.trainingsraum.attributes.brightness | int + (trigger.payload_json.angle | int) * 2 }}
          {% endif %}
    - condition: template
      value_template: "{{ states.light.trainingsraum.attributes.brightness == 0 }}"
    - service: light.turn_off
      data:
        entity_id: light.trainingsraum

Adding this to your config.yaml:

input_select:
  cube_mode:
    name: Cube Mode
    options:
      - Licht
      - Kugellicht
    initial: Licht 
    icon: mdi:cube-outline

and you will be able to switch modes of the cube. To switch the cubes I use this automation:

- alias: Cube - Toggle Cube Mode - tap
  trigger:
    platform: mqtt
    topic: 'zigbee2mqtt/Zigbee Cube'
  condition:
    condition: template
    value_template: "{{ 'tap' == trigger.payload_json.action }}"
  action:
     service: input_select.select_option
     data_template:
      entity_id: input_select.cube_mode
      option: >
            {% if states.input_select.cube_mode.state == "Licht" %} Kugellicht 
            {% elif states.input_select.cube_mode.state == "Kugellicht" %} Licht
            {% endif %} 

Inspired my this thread:
https://community.home-assistant.io/t/rotate-to-the-left-or-right-in-xiaomi-magic-cube/24990/43

All 20 comments

You should listen to the MQTT topic like mentioned here: https://github.com/Koenkk/zigbee2mqtt/wiki/Integrating-with-Home-Assistant (To respond to button clicks (e.g. WXKG01LM))

I'm not sure how to use this with the cube
Specially I don't know how I have to change this line
value_template: "{{ 'single' == trigger.payload_json.click }}"

I guess instead of single I have to use action, but I absolutly don't now what trigger.payload_json.click does.
Do I have to change it to trigger.payload_json.flip90?

automation:
  - alias: Respond to button clicks
    trigger:
      platform: mqtt
      topic: 'zigbee2mqtt/<FRIENDLY_NAME'
    condition:
      condition: template
      value_template: "{{ 'single' == trigger.payload_json.click }}"
    action:
      entity_id: light.bedroom
      service: light.toggle

value_template: "{{ 'flip90' == trigger.payload_json.action}}"

Thank you, it works.

Is it also possible to use it as a dimmer? See last code in my first posting.

@h4nc sure, an very simple example
when the light is not turned on the brightness attribute doesn't exist, HA throws a error about it

  action:
    service: light.turn_on
    data_template:
      entity_id:
        - light.gateway_light_XX
      brightness: >-
        {% if trigger.payload_json.action == 'rotate_right' %}
          {{ states.light.gateway_light_XX.attributes.brightness | int + 50 }}
        {% else %}
          {{ states.light.gateway_light_XX.attributes.brightness | int - 50 }}
        {% endif %}

Ok so I tried it like this now:

- alias: Cube - rotate
  initial_state: 'on'
  trigger:
    platform: mqtt
    topic: 'zigbee2mqtt/0x00XXXX'
  condition:
    condition: or
    conditions:
      - condition: template
        value_template: "{{ 'rotate_left' == trigger.payload_json.action }}"
      - condition: template
        value_template: "{{ 'rotate_right' == trigger.payload_json.action }}"
  action:         
    - service: light.turn_on
      data:
        entity_id: light.kugellicht
        color_name: darkturquoise
    - service: light.turn_on
      data_template:
        entity_id: light.kugellicht
        brightness: >-
          {% if trigger.payload_json.angle | float > 0 %}
          {{  states.light.kugellicht.attributes.brightness | int + 30 }}
          {% else %}
          {{  states.light.kugellicht.attributes.brightness | int - 30 }}
          {% endif %}  

The automation does turn un the light but no dimming.
Altough I turn the light on before I get this error in HA:

Error rendering data template: UndefinedError: 'dict object' has no attribute 'event'

as condition I use a simple template {{ trigger.payload_json.action in ['rotate_right', 'rotate_left'] }}
I guess you need to diag your automation my making the action simpler and adding more statements to see which one causes the issue

Thanks I thing this did the trick, because it works now as before

- alias: Cube - rotate
  initial_state: 'on'
  trigger:
    platform: mqtt
    topic: 'zigbee2mqtt/0x00XXX'
  condition:
    condition: template
    value_template: "{{ trigger.payload_json.action in ['rotate_right', 'rotate_left'] }}"
  action:         
    - service: light.turn_on
      data:
        entity_id: light.kugellicht
        color_name: darkturquoise
    - service: light.turn_on
      data_template:
        entity_id: light.kugellicht
        brightness: >-
          {% if trigger.payload_json.angle | float > 0 %}
          {{  states.light.kugellicht.attributes.brightness | int + 30 }}
          {% else %}
          {{  states.light.kugellicht.attributes.brightness | int - 30 }}
          {% endif %}  

Thank you guys, I was wondering how to get this working and this works very well!
But is it also possible to use the angle value that is also provided by the cube? The solution above just applies a fixed increase or decrease by 30 based upon weather the rotation is to the left or to the right, but I would like to use the angle as well.
Maybe one point in light level for every two degrees turned would be a good value

This is how you can use the angle, next step would be to mark the cube sides (3 sides for rgb) and use the choosen side to change the colors. But I think that would be only a nice to have feature.

- alias: Cube - rotate
  initial_state: 'on'
  trigger:
    platform: mqtt
    topic: 'zigbee2mqtt/0x00XXXX'
  condition:
    condition: template
    value_template: "{{ trigger.payload_json.action in ['rotate_right', 'rotate_left'] }}"
  action:         
    - service: light.turn_on
      data:
        entity_id: light.yourlight
    - service: light.turn_on
      data_template:
        entity_id: light.yourlight
        brightness: >-
          {% if trigger.payload_json.angle | float > 0 %}
          {{  states.light.yourlight.attributes.brightness | int + (trigger.payload_json.angle | int) / 2 }}
          {% else %}
          {{  states.light.yourlight.attributes.brightness | int + (trigger.payload_json.angle | int) / 2 }}
          {% endif %}  

And I use this to change my color (tap funktion of the cube)

- alias: Cube - tap
  initial_state: 'on'
  trigger:
    platform: mqtt
    topic: 'zigbee2mqtt/0x00XXXX'
  condition:
    condition: template
    value_template: "{{ 'tap' == trigger.payload_json.action }}"
  action:
    - service: light.turn_on
      data:
        effect: random
        entity_id: light.yourlight

a bit sad the cube is doesn't provide its real side when rotating it.
I'm using some kind of automation to retrieve the side from the last mqtt message containing side or to_side but it's not perfect.

I also find the cube to be far from reactive enough. I guess I need to use a sniffer to see if it's an issue with the cube, the network or with zigbee2mqtt

Is there a way to print a value of the trigger payload into a notification?

This does not work:

- alias: Aqara - Battery Cube
  initial_state: 'on'
  trigger:
    platform: mqtt
    topic: 'zigbee2mqtt/0x00XXXXX'
  condition:
    condition: template
    value_template: "{{ trigger.payload_json.battery | int < 10 }}"
  action:
    - service: notify.ios_mynotify
      data:
        message: "Value: {{ trigger.payload_json.battery }}"  

Whats the right way to do this?

@h4nc
Thank you for your reply. But the angle thingy is still not working for me. When I put in your code, it turns to "brightness 1" no matter how often or how far I turn the cube.
The angle is reported as a signed float, so maybe it has to to with int and float being added? (I come from C, and really have no Idea how to deal with these types in Python and YAML...
Here is my code, maybe you could have anotehr look:

- alias: Cube Schlafzimmer
  initial_state: 'on'
  trigger:
    platform: mqtt
    topic: 'zigbee2mqtt/cube1'
  condition:
    condition: template
    value_template: "{{ trigger.payload_json.action in ['rotate_right', 'rotate_left'] }}"
  action:         
    - service: light.turn_on
      data:
        entity_id: light.innr_hai
    - service: light.turn_on
      data_template:
        entity_id: light.innr_hai
        brightness: >-
          {% if trigger.payload_json.angle | float > 0 %}
          {{  states.light.innr_hai.attributes.brightness | int + (trigger.payload_json.angle | int) / 2 }}
          {% else %}
          {{  states.light.innr_hai.attributes.brightness | int - (trigger.payload_json.angle | int) / 2 }}
          {% endif %} 
    - service: light.turn_on
      data:
        entity_id: light.innr_fux
    - service: light.turn_on
      data_template:
        entity_id: light.innr_fux
        brightness: >-
          {% if trigger.payload_json.angle | float > 0 %}
          {{  states.light.innr_fux.attributes.brightness | int + (trigger.payload_json.angle | int) / 2 }}
          {% else %}
          {{  states.light.innr_fux.attributes.brightness | int - (trigger.payload_json.angle | int) / 2 }}
          {% endif %} 

Did you try to use float instead of int?
Also I don't devide the angle value by two, so maybe you want to try to delete "/ 2" and give it a another shot.
Also I would only try those things on only one light for trouble-shouting.

I'll get another dimmable light in the room I use the cube in.

Does someone here use the cube to dimm two different lights?

How the does the automation you use look like to use it for two lights? Do you use it at different sides?

Adding some lines to the above automation, will add the function to turn off the lights. Here is the complete automation (you can add multiple of these automations, just change accordingly; cubemode...):

- alias: Cube - Licht - rotate
  initial_state: 'on'
  trigger:
    platform: mqtt
    topic: 'zigbee2mqtt/Zigbee Cube'
  condition:
    - condition: template
      value_template: "{{ trigger.payload_json.action in ['rotate_right', 'rotate_left'] }}"
    - condition: state
      entity_id: input_select.cube_mode
      state: 'Licht'  
  action:         
    - service: light.turn_on
      data:
        entity_id: light.trainingsraum
    - service: light.turn_on
      data_template:
        entity_id: light.trainingsraum
        brightness: >-
          {% if trigger.payload_json.angle | float > 0 %}
          {{  states.light.trainingsraum.attributes.brightness | int + (trigger.payload_json.angle | int) * 2 }}
          {% else %}
          {{  states.light.trainingsraum.attributes.brightness | int + (trigger.payload_json.angle | int) * 2 }}
          {% endif %}
    - condition: template
      value_template: "{{ states.light.trainingsraum.attributes.brightness == 0 }}"
    - service: light.turn_off
      data:
        entity_id: light.trainingsraum

Adding this to your config.yaml:

input_select:
  cube_mode:
    name: Cube Mode
    options:
      - Licht
      - Kugellicht
    initial: Licht 
    icon: mdi:cube-outline

and you will be able to switch modes of the cube. To switch the cubes I use this automation:

- alias: Cube - Toggle Cube Mode - tap
  trigger:
    platform: mqtt
    topic: 'zigbee2mqtt/Zigbee Cube'
  condition:
    condition: template
    value_template: "{{ 'tap' == trigger.payload_json.action }}"
  action:
     service: input_select.select_option
     data_template:
      entity_id: input_select.cube_mode
      option: >
            {% if states.input_select.cube_mode.state == "Licht" %} Kugellicht 
            {% elif states.input_select.cube_mode.state == "Kugellicht" %} Licht
            {% endif %} 

Inspired my this thread:
https://community.home-assistant.io/t/rotate-to-the-left-or-right-in-xiaomi-magic-cube/24990/43

Adding some lines to the above automation, will add the function to turn off the lights. Here is the complete automation (you can add multiple of these automations, just change accordingly; cubemode...):

- alias: Cube - Licht - rotate
  initial_state: 'on'
  trigger:
    platform: mqtt
    topic: 'zigbee2mqtt/Zigbee Cube'
  condition:
    - condition: template
      value_template: "{{ trigger.payload_json.action in ['rotate_right', 'rotate_left'] }}"
    - condition: state
      entity_id: input_select.cube_mode
      state: 'Licht'  
  action:         
    - service: light.turn_on
      data:
        entity_id: light.trainingsraum
    - service: light.turn_on
      data_template:
        entity_id: light.trainingsraum
        brightness: >-
          {% if trigger.payload_json.angle | float > 0 %}
          {{  states.light.trainingsraum.attributes.brightness | int + (trigger.payload_json.angle | int) * 2 }}
          {% else %}
          {{  states.light.trainingsraum.attributes.brightness | int + (trigger.payload_json.angle | int) * 2 }}
          {% endif %}
    - condition: template
      value_template: "{{ states.light.trainingsraum.attributes.brightness == 0 }}"
    - service: light.turn_off
      data:
        entity_id: light.trainingsraum

Adding this to your config.yaml:

input_select:
  cube_mode:
    name: Cube Mode
    options:
      - Licht
      - Kugellicht
    initial: Licht 
    icon: mdi:cube-outline

and you will be able to switch modes of the cube. To switch the cubes I use this automation:

- alias: Cube - Toggle Cube Mode - tap
  trigger:
    platform: mqtt
    topic: 'zigbee2mqtt/Zigbee Cube'
  condition:
    condition: template
    value_template: "{{ 'tap' == trigger.payload_json.action }}"
  action:
     service: input_select.select_option
     data_template:
      entity_id: input_select.cube_mode
      option: >
            {% if states.input_select.cube_mode.state == "Licht" %} Kugellicht 
            {% elif states.input_select.cube_mode.state == "Kugellicht" %} Licht
            {% endif %} 

Inspired my this thread:
https://community.home-assistant.io/t/rotate-to-the-left-or-right-in-xiaomi-magic-cube/24990/43

Hi, I loved this but I don't know what it means " Licht" y " Kugellicht" Can you translate it into Spanish and English?

That鈥檚 just an example. You can change the names for the modes in input select and than select those in the automation.

Licht (german) = light

Is it possible to control the volume of android TV using this device>

If you are able to control the volume your TV with home assistant, it will also be possible to use it with the cube and an automation.

Use the example above and try to change it to your needs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jerrychong25 picture jerrychong25  路  4Comments

jeroenterheerdt picture jeroenterheerdt  路  3Comments

andreasbrett picture andreasbrett  路  4Comments

CodeFinder2 picture CodeFinder2  路  4Comments

alwashe picture alwashe  路  4Comments