Linking Xiaomi smart switch to Deconz

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

Thanks - I tried /sensors/1 and it didn’t error, so i’m assuming that worked, but I can’t confirm until I get home…
Seems that ‘id’ in the event context isn’t the same as the ‘field’ id - that was throwing me!

Hi I also have a round switch, WXKG01LM, and trying to integrate it in HASS, I always dealt with entity_id never with event.

In my case the integration in HASS it did create a battery sensor which I renamed
sensor.switch_office_battery_level

where do I get the ID of my switch? In phoscon app I do not see a place where there is an ID

Also which are the events of that switch: 1001, 1002 …???

  - alias: 'Toggle lamp from dimmer'
    initial_state: 'on'
    trigger:
      platform: event
      event_type: deconz_event
      event_data:
        id: remote_control_1
        event: 1002
    action:
      service: light.toggle
      entity_id: light.lamp

If you get the battery sensor then you will find the event id as an attribute to the sensor

Yep, its there. event_id. Thanks

1 Like

Just for information for people I’ve had a couple of issues since the latest update of deconz add-on (2.05.57) and home assistant hassos (0.85.1). I have hopefully fixed the problems now but thought it might be useful to put it on here as some of the solutions weren’t readily available.

After fixing the port issue when upgrading to latest deconz version, my xiaomi switch suddenly stopped responding. After much searching about I found the deconz event name had changed from lumisensor_switch to lumi_sensor_switch. Not sure why but perhaps something to do with how latest version of HASSOS deals with entities but I didn’t think it would be an issue for an event fired in deconz.

Anyway that solved my issue (or so I thought) - the switch was responding but another problem started to occur. The light that I use the button as a trigger to turn on suddenly started ghosting. I automatically thought this might be something to do with sonoff relay and mqtt known issues but my fix for that did nothing. So I looked more closely at history.

Light was toggling on and off at the same minute on the hour every hour. In the logs it showed that my automation that is triggered by the xiaomi button pressed was being triggered every hour. Sine there is not atime trigger in that automation only answer was deconz was triggering the event??

I have since updated the automation to listen for event 1000 rather than 1002 and the strange behaviour has stopped. No more “ghosting” since 8.30 this morning.

I haven’t seen any info related to a change of events but perhaps I haven’t been looking in the right places. Anyway hopefully this has solved my issue and may help anyone else that uses this button with similar problem

Thanks for your time

For event id naming change it is this comment in HASS 0.85 release notes

Slugify changed, which can impact entity ID creation if the entities had names with either a - or characters outside of A-Z and the integration has no unique IDs. We now better handle the characters and substitute it with an alternative instead of removing that character.

For the recurring notification there is an issue about that (which should also be solved for 2.05.58 Xiaomi switches hourly report resends the last button event. · Issue #1165 · dresden-elektronik/deconz-rest-plugin · GitHub