Automating my Xiaomi stuff

Super new user here with appdaemon and have a question on listening for an Xiaomi event - the click (or double-click) of my round button.

Here’s how I do it in HA:

- alias: Test 1B Button Double Click
  initial_state: 'on'
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d000186d98f
      click_type: double
  action:
    - service: light.turn_on
      entity_id: light.gateway_light_34ce008d5720
      data:
        rgb_color: [4,26,255]
        brightness: 255    

Here’s where I am with appdaemon:

import appdaemon.appapi as appapi

#
# Light Automations
#
# Args:
#

class Lights(appapi.AppDaemon):

  def initialize(self):
        self.listen_event(self.switch_test, event = "click")

  def switch_test(self,entity, attribute, old, new, kwargs):
        self.log("clicked it!")

It doesn’t work :disappointed:

Is there some documentation or examples of appdaemon automations with the Xiamoi sensors?

Thanks in advance for any help you can provide this noooooob.

1 Like

There are examples in the docs here

In your case, this works



import appdaemon.appapi as appapi


class click(appapi.AppDaemon):

    def initialize(self):
        self.log("initialize")
        self.listen_event(self.switch_test, "click")

    def switch_test(self,event_name, data, kwargs):
        self.log("clicked it!", level="WARNING")

Edit:
You can get the data from the event in the callback using

data['entity_id']

or

data['click_type']
2 Likes

Thank you - this was vey helpful.

Did you get it working? Can you share code for xiaomi with appdaemon? Thanks.

Here are some automations that I have for Xiaomi stuff - hopefully it will help you to get started:

import appdaemon.appapi as appapi
import globals
import flags

#
# Xiaomi Stuff
#
# Args:
#

class xiaomi(appapi.AppDaemon):

    def initialize(self):
          self.listen_event(self.x_button1_single, "click", entity_id = globals.x_switch_1, click_type = "single")
          self.listen_event(self.x_button1_double, "click", entity_id = globals.x_switch_1, click_type = "double")
          self.listen_event(self.x_button2_single, "click", entity_id = globals.x_switch_2, click_type = "single")
          self.listen_state(self.garage_door, globals.entry_garage, new="on")
          self.listen_event(self.cube_rotate, "cube_action", action_type="rotate", entity_id = globals.cube)
          self.listen_event(self.cube_tap, "cube_action", action_type="tap_twice", entity_id = globals.cube)
          self.listen_event(self.cube_flip90, "cube_action", action_type="flip90", entity_id = globals.cube)
          self.togglec = 1
          self.togglec2 = 1

    def x_button1_single(self,event_name, data, kwargs):
          self.log("button 1 press...")
          self.toggle("light.hg_office_oh_lights")

    def x_button1_double(self,event_name, data, kwargs):
          self.log("button 1 double click...")

    def x_button2_single(self,event_name, data, kwargs):
          self.log("button 2 press...")
          if self.entities.light.hannahs_br.state == "off":
            self.turn_on("light.hannahs_br")
            self.turn_on("light.hannahs_br")
          else:
            self.turn_off("light.hannahs_br")
            self.turn_off("light.hannahs_br")

    def garage_door(self, entity, attribute, old, new, kwargs):
         self.log("garage entry door")
         self.call_service("xiaomi_aqara/play_ringtone",gw_mac = globals.x_gw_office, ringtone_id = 10025,ringtone_vol = 2)

    def cube_rotate(self,event_name, data, kwargs):
         self.log("cube rotating")
         self.log(data["action_value"])
         if self.entities.light.office_desk.state !="off":
           self.cur_brightness=self.entities.light.office_desk.attributes.brightness
         else:
           self.cur_brightness=0
         self.log(self.cur_brightness)
         self.cube_value=data["action_value"]
self.new_brightness = (self.cube_value * 3) + self.cur_brightness
         self.log("new bright " + str(self.new_brightness))
         if self.new_brightness >= 255:
           self.new_brightness = 255
         elif self.new_brightness <= 0:
           self.new_brightness = 0
         if self.new_brightness == 0:
           self.turn_off("light.office_desk")
         else:
           self.turn_on("light.office_desk", brightness=self.new_brightness)

    def cube_tap(self,event_name, data, kwargs):
         #self.toggle("light.office_desk")
         if self.togglec2 == 1:
           self.togglec2 = 0
           self.set_state(globals.motion_detector_garage,state="on")
         else:
           self.togglec2 = 1
           self.set_state(globals.motion_detector_garage,state="off")

    def cube_flip90(self,event_name, data, kwargs):
       self.log("cube flip90")
       #if self.get_state(globals.motion_detector_garage) == "off":
         #self.log("it's off - setting garage motion on")
         #self.set_state(globals.motion_detector_garage,state="on")
       #else:
         #self.log("it's on - setting garage motion off")
         #self.set_state(globals.motion_detector_garage,state="off")
       if 1 == 2:
         self.call_service("xiaomi_aqara/play_ringtone",gw_mac = globals.x_gw_office, ringtone_id = 10025,ringtone_vol = 10)
         #self.log(flags.timer_heater)
         flags.timer_heater = "active"
         #self.log(flags.timer_heater)
         if self.togglec == 1:
           self.togglec = 0
           self.set_state(globals.garage_1,state="on")
         else:
           self.togglec = 1
           self.set_state(globals.garage_1,state="off")

Let me know if you have any questions - good luck!

Tom

2 Likes

Thank you. And in this globals you store values? Can you share that also? And what this flags module do? i’m flooding you with questions. Sorry. :slight_smile:

globals.py - just a place to store global variables to make it easier to reference and change actual names. It looks like this:

# Global variables
#motion_detector_garage = "binary_sensor.motion_sensor_158d0001d6e053"
#motion_detector_garage = "binary_sensor.motion_sensor_158d0001a66245"
motion_detector_garage = "group.garage_motion"
motion_detector_fr = "binary_sensor.motion_sensor_158d0001d6e1d1"
motion_detector_office = "binary_sensor.motion_sensor_158d00016c3b52"
motion_detector_kitchen = "binary_sensor.motion_sensor_158d0001a66245"
humidity_family_room = "sensor.humidity_158d0001ab7154"
humidity_nest = "sensor.nest_thermostat_humidity"
deck_temperature = "sensor.temperature_158d0001826dec"
family_room_temperature = "sensor.temperature_158d0001ab7154"
humidifier_switch = "switch.strip_cord_4"
christmas_tree = "switch.strip_cord_2"
#
x_gw_office = "34ce008d5720"
x_gw_dinette = "286c07f0abd8"
#
minimote1 = "zwave.aeotec_dsa03202_minimote"
minimote2 = "zwave.aeotec_dsa03202_minimote_2"
minimote3 = "zwave.aeotec_dsa03202_minimote_3"
minimote4 = "zwave.aeotec_dsa03202_minimote_4"
#
x_switch_1 = "binary_sensor.switch_158d000186d98f"
x_switch_2 = "binary_sensor.switch_158d0001561050"
#
garage_1 = "binary_sensor.door_window_sensor_158d0001a4d0ba"
garage_2 = "binary_sensor.door_window_sensor_158d00019e0db4"
garage_3 = "binary_sensor.door_window_sensor_158d00019e0d96"
#
entry_garage = "binary_sensor.door_window_sensor_158d00018354bc"
entry_front = "binary_sensor.door_window_sensor_158d0001a5ec46"
entry_deck = "binary_sensor.door_window_sensor_158d00019ff2b2"
#
cube = "binary_sensor.cube_158d00010ae769"
#
office_heater = "switch.wemo_mini"
#
motion_fdoor_camera = "binary_sensor.bi_fdoor"
motion_fgarage_camera = "binary_sensor.bi_fgarage"
motion_byard_camera = "binary_sensor.bi_byard"
motion_byard_critter_cam = "binary_sensor.bi_critcam"
motion_fyard_critter_cam = "binary_sensor.bi_fcritcam"
#
toms_phone = "device_tracker.tomsiphone2"
sues_phone = "device_tracker.suesiphone"
abbys_phone = "device_tracker.abbysiphone"
julias_phone = "device_tracker.iphone"
hannahs_phone = "device_tracker.hannahsiphone2"
#

flags.py is similar - and I am not using any longer.

Tom

2 Likes

All this helps. Thanks again.

1 Like

Hi go this working for all states, but wondering do you know what or how you trigger the Alert function on the cube?

Try to shake the table where cube lies, drop it on couch, just touch it.