Xiaomi Cube automation

I’m having some trouble to automate the Xiaomi aqara [MFKZQ01LM] cube with zigbee2mqtt (https://www.zigbee2mqtt.io/devices/MFKZQ01LM.html).
Try to launch different light scenes depending on the cube action. What should I do to make it work (currently automation does’nt react).

- id: '1584206716256'
  alias: Cube bedroom
  description: light automation
  trigger:
  - platform: mqtt
    topic: zigbee2mqtt/cube_bedroom
  condition:
  - condition: template
    value_template: 'value_template: ''{{ "flip90" == trigger.payload_json.action
      or "flip180" == trigger.payload_json.action or "tap" == trigger.payload_json.action
      }}'
  action:
  - data:
      data_template:
      - entity_id: scene_id >- {% if state_attr(sensor.cube_bedroom_action,
          action) == flip90 %} scene.bedroom1 {% elif state_attr(sensor.cube_bedroom_action,
          action) == flip180 %} scene.bedroom2 {% elif state_attr(sensor.cube_bedroom_action,
          action) == tap %} scene.bedroom3 {% endif %}
    service: scene.turn_on

Hi @dede34fr,

I see some typos in the automation, you could try the following:

- id: '1584206716256'
  alias: Cube bedroom
  description: light automation
  trigger:
  - platform: mqtt
    topic: zigbee2mqtt/cube_bedroom
  condition:
  - condition: template
    value_template: {{ "flip90" == trigger.payload_json.action
      or "flip180" == trigger.payload_json.action or "tap" == trigger.payload_json.action
      }}
  action:
  - data:
      data_template:
      - entity_id: >-
          {% if trigger.payload_json.action == 'flip90' %} scene.bedroom1 
          {% elif trigger.payload_json.action == 'flip180' %} scene.bedroom2
          {% elif trigger.payload_json.action == 'tap' %} scene.bedroom3 
          {% endif %}
    service: scene.turn_on

However, I invite you to take a look to ControllerX an AppDaemon app that handles controller events and lets binding them to lights, media players and call services.

This would be the configuration in apps.yaml for your needs:

cube_bedroom:
  module: controllerx
  class: CallServiceController
  controller: sensor.cube_bedroom_action
  integration: z2m
  mapping:
    flip90:
      service: scene.turn_on
      data:
        entity_id: scene.bedroom1
    flip180:
      service: scene.turn_on
      data:
        entity_id: scene.bedroom2
    tap:
      service: scene.turn_on
      data:
        entity_id: scene.bedroom3

As you can see the configuration is much cleaner since it is centered to be just for button events, whereas HA automations are generic based.

If you have any questions, let me know :slight_smile:

Could I use the “CustomLightController”? Should I use the “MFKZQ01LMLightController” class instead? What’s the difference. By the way these 2 generate an appdaemon error like this:

2020-03-20 13:43:05.154329 INFO AppDaemon: Initializing app cube_bedroom using class CustomLightController from module controllerx
2020-03-20 13:43:05.302952 INFO cube_bedroom: ControllerX v2.4.3
2020-03-20 13:43:05.311740 WARNING cube_bedroom: ------------------------------------------------------------
2020-03-20 13:43:05.312472 WARNING cube_bedroom: Unexpected error running initialize() for cube_bedroom

Hi @dede34fr,

I will summarise in here the differences between the different type of controllers.

  • CustomLightController: It allows to map the event buttons with predefine actions, such as, turning light(s) on/off, toogle, change color, brightness. Here is an example from the documentation:
example_app:
  module: controllerx
  class: CustomMediaPlayerController
  controller: my_magic_cube_id
  integration:
    name: deconz
    type: gesture
  media_player: media_player.livingroom_speaker
  mapping:
    1: play_pause # Shake
    8: click_volume_down # Rotate left
    7: click_volume_up # Rotate right
    3: next_track # Flip90
    4: previous_track # Flip180

This uses the CustomMediaPlayerController which is the same as the light one, but for media players. In this case I am mapping the different events with predefined actions like play/pause, skip song, etc. You can find the predefined actions in here

  • MFKZQ01LMLightController: This controller is the easiest to configure, but it gives a default behaviour to the cube. This is a configuration example of my current setup:
office_light_cube:
  module: controllerx
  class: MFKZQ01LMLightController
  controller: sensor.office_cube_controller_action
  integration: z2m
  manual_steps: 5
  light: light.office
  • CallServiceController: This one is the one I used in the example I gave you. This allows to bind the events to HA service calls.

I hope this helps you clarify the options you have.

Regarding the error you are getting, I might need more information, since the error itself is not described in the section you showed. Could you maybe send all the AppDaemon logs?

Thank you :slight_smile: