Linking Xiaomi smart switch to Deconz

You’re right, i found in this page https://github.com/dresden-elektronik/deconz-rest-plugin/wiki/Supported-Devices
all the supported device and their events
The Xiaomi Smart switch doesnt have hold but triple and quaduple press!

1 Like

There are two different Xiaomi Smart Switches (or buttons).

The round one (original version) and the square one (new Aqara version). The round one expose long press, the square one does not.

But you have to realize there is no such thing as a “long press” event. There is the “pushed down” event (1000), and the “released” (1002) event. You will have to make the logic to decide when the time difference between “push” and “release” qualify as a “long press”.

If you search the forum you will find example automations for the round switch/button. I use a appdaemon script that works fine for dimming lights up and down.

The square button does not have a “pushed down” event so it can not expose a “long press”.

1 Like

Great that you found that!

Excellent tip!

Great, I have the round one.
Searching in the forum I found this Finally - a cheap WIRELESS switch that dims! Xiaomi Switch Gen1 but in the automation is used long_click_press as click type, I think the that the switch is used with the xiaomi gateway.
Can you post your script @aherbjornsen?

@B1G I use the one referenced here:

Thanks,
I installed AppDaemon and configured but that script it doesn’t work… i only changed the light name with mine

Yes, I see that I had to change it a bit to get it to work. Mainly I think the 1000 and 1002 in the “button_pressed” function has to be swapped. My script looks like this:

import appdaemon.appapi as appapi
import time

class dimming(appapi.AppDaemon):

  def initialize(self):
    self.listen_event(self.button_pressed, "deconz_event", id = self.args["switch"] )
    self.table_hold = False
    self.table_dim = False
    self.light = self.args["light"]

  def button_pressed(self, event_name, data, kwargs):
    self.click_type=data["event"]
    if self.click_type == 1004:
      self.toggle(self.light)
    elif self.click_type == 1002:
      if self.table_hold == False or self.get_state(self.light) == "off":
        self.table_pressed = False
        self.toggle(self.light)
      self.table_hold = False
    elif self.click_type == 1000:
      self.table_pressed = True
      time.sleep(0.3)
      self.table_hold = True
      if self.table_pressed:
        self.log("long press")
        self.long_press()
        self.table_dim = not self.table_dim
     
  def long_press(self):
    if self.table_dim == False and self.table_hold == True and self.get_state(self.light) == "on":
      while self.table_hold:
        self.new_brightness = self.get_state(self.light, "brightness") + 25
        if self.new_brightness >= 255:
          self.new_brightness = 255
        self.turn_on(self.light, brightness = self.new_brightness)
      self.table_hold = False  

    elif self.table_dim == True and self.table_hold == True and self.get_state(self.light) == "on":
      while self.table_hold:
        self.new_brightness = self.get_state(self.light, "brightness") - 25
        if self.new_brightness <= 0:
          self.new_brightness = 6
        self.turn_on(self.light, brightness = self.new_brightness)
      self.table_hold = False

And then I put this in the Appdaemon config:

mydimmer:
  class: dimming
  module: dimmer
  switch: lumisensor_switch
  light: light.kitchen

it doesn’t work… at this point I think it’s an appdaemon misconfiguration…
the add-on log is this when I start it

 Hass.io Add-on: AppDaemon3 v1.0.1
 Python Apps and HADashboard using AppDaemon 3.x for Home Assistant
 From: Community Hass.io Add-ons
 By: Franck Nijhof <[email protected]>
-----------------------------------------------------------
[cont-init.d] 00-banner.sh: exited 0.
[cont-init.d] 01-log-level.sh: executing... 
Log level is set to INFO
[cont-init.d] 01-log-level.sh: exited 0.
[cont-init.d] 02-updates.sh: executing... 
INFO: You are running the latest version of this add-on
[cont-init.d] 02-updates.sh: exited 0.
[cont-init.d] 03-version-requirements.sh: executing... 
INFO: Supervisor version requirements checks passed.
[cont-init.d] 03-version-requirements.sh: exited 0.
[cont-init.d] 20-init-configuration.sh: executing... 
[cont-init.d] 20-init-configuration.sh: exited 0.
[cont-init.d] 21-compiled-dir.sh: executing... 
[cont-init.d] 21-compiled-dir.sh: exited 0.
[cont-init.d] 30-auto-password.sh: executing... 
[cont-init.d] 30-auto-password.sh: exited 0.
[cont-init.d] 31-ha-url.sh: executing... 
WARNING: You are using an non-recommended Home Assistant URL!
WARNING: Setting the "ha_url" option in your AppDaemon config to
WARNING: "http://hassio/homeassistant" is recommended!
[cont-init.d] 31-ha-url.sh: exited 0.
[cont-init.d] 50-compiled-symlink.sh: executing... 
[cont-init.d] 50-compiled-symlink.sh: exited 0.
[cont-init.d] 80-system-packages.sh: executing... 
[cont-init.d] 80-system-packages.sh: exited 0.
[cont-init.d] 81-python-packages.sh: executing... 
[cont-init.d] 81-python-packages.sh: exited 0.
[cont-init.d] done.
[services.d] starting services
starting version 3.2.4
[services.d] done.
2018-05-07 16:17:14.452804 INFO AppDaemon Version 3.0.1 starting
2018-05-07 16:17:14.453452 INFO Configuration read from: /config/appdaemon/appdaemon.yaml
2018-05-07 16:17:14.457345 INFO AppDaemon: Starting Apps
2018-05-07 16:17:14.465165 INFO AppDaemon: Loading Plugin HASS using class HassPlugin from module hassplugin
2018-05-07 16:17:14.507375 INFO AppDaemon: HASS: HASS Plugin Initializing
2018-05-07 16:17:14.508392 INFO AppDaemon: HASS: HASS Plugin initialization complete
2018-05-07 16:17:14.509482 INFO Dashboards are disabled
2018-05-07 16:17:14.510004 INFO API is disabled
2018-05-07 16:17:15.220223 INFO AppDaemon: HASS: Connected to Home Assistant 0.68.1
2018-05-07 16:17:16.032912 INFO AppDaemon: Got initial state from namespace default
2018-05-07 16:17:17.626183 INFO AppDaemon: Reading config
2018-05-07 16:17:17.637165 INFO AppDaemon: /config/appdaemon/apps/apps.yaml added or modified
2018-05-07 16:17:17.637836 INFO AppDaemon: /config/appdaemon/apps/apps.yaml added or modified
2018-05-07 16:17:17.638658 INFO AppDaemon: App 'dim_app' added
2018-05-07 16:17:17.640031 INFO AppDaemon: Adding /config/appdaemon/apps to module import path
2018-05-07 16:17:17.642092 WARNING AppDaemon: No app description found for: /config/appdaemon/apps/._dim.py - ignoring
2018-05-07 16:17:17.642955 INFO AppDaemon: Loading App Module: /config/appdaemon/apps/dim.py
2018-05-07 16:17:17.677700 INFO AppDaemon: Initializing app dim_app using class dimming from module dim
2018-05-07 16:17:17.680640 INFO AppDaemon: App initialization complete
2018-05-07 16:27:17.730422 WARNING AppDaemon: Excessive time spent in utility loop: 1088.0ms
2018-05-07 16:37:19.657600 WARNING AppDaemon: Excessive time spent in utility loop: 931.0ms

my appdaemon.yaml

secrets: /config/secrets.yaml
log:
  logfile: STDOUT
  errorfile: STDERR
appdaemon:
  threads: 10
  app_dir: /config/appdaemon/apps
  plugins:
    HASS:
      type: hass
      ha_url: myduckdns
      ha_key: !secret http_password

app.yaml

dim_app:
  module: dim
  class: dimming
  switch: lumisensor_switch
  light: light.marco

and dim.py

import appdaemon.appapi as appapi
import time

class dimming(appapi.AppDaemon):

  def initialize(self):
    self.listen_event(self.button_pressed, "deconz_event", id = self.args["switch"] )
    self.table_hold = False
    self.table_dim = False
    self.light = self.args["light"]

  def button_pressed(self, event_name, data, kwargs):
    self.click_type=data["event"]
    if self.click_type == 1002:
      if self.table_hold == False or self.get_state(self.light) == "off":
        self.table_pressed = False
        self.toggle(self.light)
      self.table_hold = False
    elif self.click_type == 1000:
      self.table_pressed = True
      time.sleep(0.3)
      self.table_hold = True
      if self.table_pressed:
        self.log("long press")
        self.long_press()
        self.table_dim = not self.table_dim
     
  def long_press(self):
    if self.table_dim == False and self.table_hold == True and self.get_state(self.light) == "on":
      while self.table_hold:
        self.new_brightness = self.get_state(self.light, "brightness") + 25
        if self.new_brightness >= 255:
          self.new_brightness = 255
        self.turn_on(self.light, brightness = self.new_brightness)
      self.table_hold = False  

    elif self.table_dim == True and self.table_hold == True and self.get_state(self.light) == "on":
      while self.table_hold:
        self.new_brightness = self.get_state(self.light, "brightness") - 25
        if self.new_brightness <= 0:
          self.new_brightness = 6
        self.turn_on(self.light, brightness = self.new_brightness)
      self.table_hold = False

Do I need something else to run the app?
I’m running HA with ssl

found the problem.
Needed some changes using appdaemon 3
my working script now is this:

import appdaemon.plugins.hass.hassapi as hass
import time

class dimming(hass.Hass):

  def initialize(self):
    self.listen_event(self.button_pressed, "deconz_event", id = self.args["switch"] )
    self.table_hold = False
    self.table_dim = False
    self.light = self.args["light"]

  def button_pressed(self, event_name, data, kwargs):
    self.click_type=data["event"]
    if self.click_type == 1002:
      if self.table_hold == False or self.get_state(self.light) == "off":
        self.table_pressed = False
        self.toggle(self.light)
      self.table_hold = False
    elif self.click_type == 1000:
      self.table_pressed = True
      time.sleep(0.3)
      self.table_hold = True
      if self.table_pressed:
        self.log("long press")
        self.long_press()
        self.table_dim = not self.table_dim
     
  def long_press(self):
    if self.table_dim == False and self.table_hold == True and self.get_state(self.light) == "on":
      while self.table_hold:
        self.new_brightness = self.get_state(self.light, attribute="brightness") + 25
        if self.new_brightness >= 255:
          self.new_brightness = 255
        self.turn_on(self.light, brightness = self.new_brightness)
      self.table_hold = False  

    elif self.table_dim == True and self.table_hold == True and self.get_state(self.light) == "on":
      while self.table_hold:
        self.new_brightness = self.get_state(self.light, attribute="brightness") - 25
        if self.new_brightness <= 0:
          self.new_brightness = 6
        self.turn_on(self.light, brightness = self.new_brightness)
      self.table_hold = False
2 Likes

Sorry to drag up an old topic, but I just got my conbee and setup the deconz hassio plugin.

I paired my Xiaomi switch but it doesn’t show in either Phoscon or the old Web app - is this how it’s supposed to be ?
It seems paired as I see press messages in the addon debug log in hassio, but I don’t know how to get those messages from my hassio node red.
I would have expected the device to show in either Phoscon or the old web app so that I can at least name the device… little confused!

Some devices aren’t visible in the GUI yet. And in home assistant they are only available as events, unless they report battery status, then that will also be created.

Thanks, I can now see the events in hass via node red, but there doesn’t seem to be any battery status created.
I checked the same buttons when paired with the Xiaomi gateway and they report battery status (I have the small round buttons).

Also, how do I name the devices, I can see in node red the id for the switch is lumisensor_switch - how do I set this to something more relevant?

The names are from deconz. All devices getting entities you can just override with entity registry. But for events you need to change the name inside of deconz

Thanks, but that’s what I mean - how is that done in deconz when I don’t see the devices in either webui?

Is there some other method to interact with deconz?

You do it over the API. There is a service for deconz in hass you can use

Thank you - I actually had a play around with the deconz.configure service last night, but I could not get it to work…
I don’t understand how to find out the current ‘field’ value for my switch, but I’m guessing that once I know that I can call configure service specify the field, and then set the data ‘name’ to something friendly?

I tried to hit the IP of the deconz addon on 8080/8443 and specify /api as the url, but it doesn’t return…
What am I missing here?

Have you read the documentation? https://www.home-assistant.io/components/deconz/

I have, but I don’t know how I get the value for ‘field’… and entity is (as far as I can tell) for the home assistant generated entity - which I don’t have, since it’s just firing events when I press the button.

I’m guessing the ‘field’ is something like ‘/sensor/lumisensor_switch’ ? - but I tried that yesterday and it didn’t work…

No field in you case should be something like /sensors/1 or similar.

Enable debug for the component and look in the logs for what deconz id the device has