Xiaomi Sherlock Smartlock

Hi, I don’t get it how that is working, the position of the reed switch and the magnet is always the same even when rotating, what’s the trigger …?

I’m guessing the rotation of the internal reed switch is enough to switch it from ‘closed’ to ‘open’. They need to be quite aligned…

The trigger of a Xiaomi door sensor is when the reed switch and the magnet move apart (open) or move closer (close).

With the rotation of the handle they both rotate together, so there is no trigger … I do not understand

@burzka 's handle (the black cover) doesn’t rotate… only the internal key/reed switch.

mmhhh ok, I see, that can work, The switch will then go crazy on/off/on/off multiple times before landing to a fix position. If the sequence of on/off is different between lock/unlock that can work.

I wish to see the code how to realize that, unfortunately I do not use node-red (I did in the past but was for me an aggravation for maintaining it)

This working for me with the reed switch. No node red required…

Have two automations like this in your automation file.

- id: '1755'
  alias: Smart door unlocked
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d0001bf8643
    from: 'off'
    to: 'on'
    for:
      seconds: 1
  action:
  - service: notify.google_assistant
    data_template:
      message: "unlock"
- id: '1756'
  alias: Smart door locked
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d0001bf8643
    from: 'off'
    to: 'on'
  action:
  - service: notify.google_assistant
    data_template:
      message: "locked"

Put a template switch in your config.

switch:
  - platform: template
    switches:
      smartlock:
        friendly_name: "smartlock"
        value_template: "{{states.automation.smart_door_locked.attributes.last_triggered > states.automation.smart_door_unlocked.attributes.last_triggered }} "
        turn_on:
          service: script.locked
        turn_off:
          service: script.unlocked

For a unlock motion (longer) while both are triggered, the unlocked automation is triggered after the locked one, so looking at which was last triggered seems to get you a decent state indicator.

Note that I’m not really doing anything just yet with the switch template services, they are basically dummies. You don’t need the notifications either, they’re for my testing. I’ll report back later with reliability and consolidated solution.

TemplateSwitch

1 Like

BTW instead of that ugly (IMO) white magnet, you can use a normal (thin) magnet (maybe even the magnet inside the plastic enclosure, never opened it though), paint it in black with a marker, attach it with 3M adhesive, and will be almost invisible

works perfectly for me, thanks mate!

1 Like

To understand first position of door sensor (let’s say when is unlocked) the binary_sensor has to be OFF (magnet away from reed) or ON (magnet near reed)?

I think you could do it a few ways. But I think it is best that when the lock is in its “resting state”, the door sensor is “closed” (near).

The lock state is basically inferred from the length of time that the sensor “is not closed” when it changes from closed to open, rather than the specific sensor state.

@ahzazou , @michel72 & @Klagio .

I’m about to get started on the hacked bluetooth button modification to complete my solution.
I would prefer to just wire it into my pi (don’t use a esp8266), but I understand this isn’t reliable.
So the esp8266 is the best option (with 10k resistor)?

Could you guys please comment on reliability you are achieving and confirm what you think is the best solution?

Yes that’s my experience: with the pi would work 50% of the time. With an ESP8266 its more 95% of the time. Remember to use a proper (equal or more then) 2A+ power supply for the ESP8266.

I recommend to use @ahzazou code or ESPhome. For ESPhome is this (you will need to change switch into lock somehow (I didn’t do it yet), its a bit better in the sense that it gives you also WiFi Signal strength and you can get notified if the ESP8266 disconnects from WiFi (with an automation, I did not do yet), and you can play, if you like, with ESP8266 LED, and you can make changes OTA.

esphome:
  name: 12_door
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "xx"
  password: "xx"
  manual_ip:
    static_ip: 192.168.1.xx
    gateway: 192.168.1.yy
    subnet: 255.255.255.0 
# Enable logging
logger:

# Enable Home Assistant API
api:

ota:


web_server:
  port: 80

switch:
  - platform: gpio
    pin:
      number: D0
      inverted: yes
    id: relay
  - platform: template
    name: "close_12_home_door"
    icon: "mdi:gate"
    turn_on_action:
    - switch.turn_on: relay
    - delay: 500ms
    - switch.turn_off: relay
  - platform: template
    name: "open_12_home_door"
    icon: "mdi:gate"
    turn_on_action:
    - switch.turn_on: relay
    - delay: 500ms
    - switch.turn_off: relay
    - delay: 100ms
    - switch.turn_on: relay
    - delay: 500ms
    - switch.turn_off: relay

sensor:
  - platform: wifi_signal
    name: "12 BLE Bridge WiFi Signal"
    update_interval: 60s
  - platform: uptime
    name: "12 BLE Bridge Uptime"
    
text_sensor:
  - platform: version
    name: "12 ble bridge ESPHome Version"
1 Like

I don’t understand it much (my limitation) , I guess I will need to make tests myself

Hi Klagio,

Can you please submit a picture with a closeup of the wiring/soldering you did for the Bluetooth button and the ESP8266?

1 Like

I have them in enclosure and a bit hard to reach, butr there are clear photos in this thread

Here you go can’t be more clear then this

In the ESP in super simple.

Put the positive of the battery to the 3,3V, the negative to GND, and the other end of the resistor to D0 (D0 is GPIO16 in nodemcu) so also the blue LED will blink when you lock/unlock.

3 Likes

I am new to ESP8266 / ESP32.

Should I get the ESP32?

Any recommended online sellers? (Gearbest, Ali Express etc)

Both these below work. As for ESP32 you don’t need Bluetooth, actually could cause you interference (Wifi +2 Bluetooth all nearby, bad idea) avoid it. And you don’t need 36 GPIO, just 1 :smiley:

1 Like

Hello, your solution seems to be documented the best and I’m willing to give it a go and try on my lock. Can you please upload yaml file on your gut as well. I mean the part of configuration.yaml that you are using to control the lock.

Finally i found this video “The Hook up” explaining how can battery consumption can be lowered dramatically. https://youtu.be/2kLZ7DlP9KU would love to see this updated with lover power consumption in mind.

Thank you for your efforts.

1 Like

Hi,

Summary of my implementation and consolidating some of this thread for others:

  • Use a hacked fob, ESP32 and ESPhome add-on for Hassio lock/unlock control: Do the hacked fob onto a ESP32/ESP8266 like some others to get Hassio lock/unlock control.

    • Pop apart the fob. Mine came apart ok, but some others have struggled. The face definitely pries off. This post may help: Xiaomi Sherlock Smartlock - #83 by ahzazou
    • Wire it like Klagio and @ahzazou say in this post. Xiaomi Sherlock Smartlock - #233 by Klagio . I hate, and suck at soldering, but I struggled through it. Watch a few YouTube videos for soldering tips.
    • Use Esphome hassio add-on. Use @Klagio 's code (thanks). Here: Xiaomi Sherlock Smartlock - #230 by Klagio. Check online for your GPIO16 pin.
    • I was really unfamiliar with most of the above, but if I can do it, you can too!
    • A ESP8266 may be better than a ESP32. But I wanted to use the fancier ESP32 and maybe use it for BLE presence detection too.
  • Use a Reed switch positioned within the lock for state tracking:

    • Position a reed switch (xiaomi door sensor etc) within the lock to get a reliable state reading (locked/unlocked). I abandoned using a Xiaomi motion/vibration sensor after @burzka cracked a solid reed switch solution. Here: Xiaomi Sherlock Smartlock - #216 by burzka
    • The positioning of mine is a bit unique to my lock type/set-up, but burzka’s solution should work for all?
    • Then for state tracking, (and I’m not sure if this is the same for everyone’s locks, so you may need to tinker), the main logic here is that it takes longer for the lock to do it’s ’unlock’ sequence, than a “lock” sequence. See code.

Some unique features of mine:

  • I can still manually turn/unlock/unlock the lock from inside (like andreyxc, but not as cool), I grasp and turn the stainless steel part.

My challenges:

  • Only a thumb-turn on the inside for me, like in andreyxc 's video. Vertical is locked. Rotate 90 degrees clockwise to unlock (horizontal). Thumb turn width quite wide which meant I couldn’t just reverse the thing-me-bob. I had to build a custom coupling thingo. So quite a lot of modding for it to work on my door. I went through a few iterations, but I got there. See vid.

  • This was my first time using an ESP32 or similar. Total novice to that. ESPhome was pretty easy to get set up, instructions are pretty good. A few hiccups, but some restarts/power cycles seemed to have sorted it.

  • I cut my finger while whittling the custom coupler thingo from wood and walked bleeding down the street to get 4 stiches. Stuuupid😊.

  • The fob only work like 75% of the time? This is even using a spare one manually, the light flashes, but door lock doesn’t respond. The work around is in the script to check if the action was actually effective and try again if not. This actually happened in my vid, so you can see that in action.

Thanks to all those mentioned in the thread.

Video of it working: https://www.youtube.com/watch?v=6M4HUpGbGYk&feature=youtu.be

A quick vid more focused on the mod I made : https://www.youtube.com/watch?v=IV49bL67ZRQ&feature=youtu.be

Main materials for custom coupler:

For state tracking:
In your automation file:

- id: '1755'
  alias: Door was just unlocked
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d0001bf8643
    from: 'off'
    to: 'on'
    for:
      seconds: 1
  action:
- id: '1756'
  alias: Door was just locked
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d0001bf8643
    from: 'off'
    to: 'on'
  action:

For switch which gets it’s state from the automations (I preferred to use a switch template in the end, but you could use the template lock if you like)

switch:
  - platform: template
    switches:
      smartlock:
        friendly_name: "Smart Lock"
        value_template: "{{states.automation.door_was_just_locked.attributes.last_triggered > states.automation.door_was_just_unlocked.attributes.last_triggered }} "
        turn_on:
          service: script.lock_door
        turn_off:
          service: script.unlock_door
        icon_template: >-
          {% if is_state('switch.smartlock', 'on') %}
            mdi:lock
          {% else %}
            mdi:lock-open
          {% endif %}

or for the template lock

  - platform: template
    name: smart lock
    value_template: "{{states.automation.door_was_just_locked.attributes.last_triggered > states.automation.door_was_just_unlocked.attributes.last_triggered }} "
    lock:
      service: script.lock_door
    unlock:
      service: script.unlock_door

And the scripts to fire the ESPhome switches as per Klagios code, which re-fire a few times if they fail.

lock_door:
  sequence:
  - service: switch.turn_on
    entity_id: switch.close_12_home_door
  - delay: '00:00:03'
  - condition: template
    value_template: "{{states.automation.door_was_just_locked.attributes.last_triggered < states.automation.door_was_just_unlocked.attributes.last_triggered}}"
  - service: switch.turn_on
    entity_id: switch.close_12_home_door
  - delay: '00:00:03'
  - condition: template
    value_template: "{{states.automation.door_was_just_locked.attributes.last_triggered < states.automation.door_was_just_unlocked.attributes.last_triggered}}"
  - service: switch.turn_on
    entity_id: switch.close_12_home_door
  - delay: '00:00:03'
  - condition: template
    value_template: "{{states.automation.door_was_just_locked.attributes.last_triggered < states.automation.door_was_just_unlocked.attributes.last_triggered}}"
  - service: switch.turn_on
    entity_id: switch.close_12_home_door

    
unlock_door:
  sequence:
  - service: switch.turn_on
    entity_id: switch.open_12_home_door
  - delay: '00:00:8'
  - condition: template
    value_template: "{{states.automation.door_was_just_locked.attributes.last_triggered > states.automation.door_was_just_unlocked.attributes.last_triggered}}"
  - service: switch.turn_on
    entity_id: switch.open_12_home_door
  - delay: '00:00:8'
  - condition: template
    value_template: "{{states.automation.door_was_just_locked.attributes.last_triggered > states.automation.door_was_just_unlocked.attributes.last_triggered}}"
  - service: switch.turn_on
    entity_id: switch.open_12_home_door
  - delay: '00:00:8'
  - condition: template
    value_template: "{{states.automation.door_was_just_locked.attributes.last_triggered > states.automation.door_was_just_unlocked.attributes.last_triggered}}"
  - service: switch.turn_on
    entity_id: switch.open_12_home_door

Added: 2019-04-27

  1. Made steampunk ESP32 case out of $3 solar light bulb from local crap-store and hung it near the door. I drilled a hole in the bottom for ventilation. Someone tell me if this is a bad idea. I like it! You can keep an eye on the flashing fob led and see the next feature.

  2. Added visual LED indicator for locked/unlocked. From what I can see so far I can only do blue led on/off with what’s onboard my ESP32. Append this to Klagio’s early code under switch:

switch:
  - platform: gpio
    pin:
      number: GPIO2
      inverted: yes
    id: led
  - platform: template
    name: "esp32 led off"
    icon: "mdi:lightbulb"
    turn_on_action:
    - switch.turn_on: led
  - platform: template
    name: "esp32 led on"
    icon: "mdi:lightbulb"
    turn_on_action:
    - switch.turn_off: led
  1. Added a Xiaomi button near my apartment intercom/and where my “grab the things before you go out the door” are so that can I toggle the lock (guests are coming up etc, long press), or I’m leaving (unlock the door). That’s pretty straight forward, but here you are anyway:
#Toggle lock on xiaomi button long press
- id: '17566'
  alias: Toggle Door Lock
  trigger:
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch_158d0001e145a5
      click_type: long_click_press
  action:
    - service: switch.toggle
      entity_id: switch.smartlock
  1. Added auto-lock after door closes. This uses another (different) reed switch/door sensor mounted in the traditional way. The door locks 15 seconds aftert the door closes. Again, pretty straight forward, but there you go:
#Auto lock front door after the DOOR actually closes
- id: '17567'
  alias: Auto Lock Door 15s
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d00031b360f
    from: 'on'
    to: 'off'
    for:
      seconds: 15
  action:
    - service: script.lock_door

Next up:

  • See if I can use the same or my spare ESP32 to also do some BLE presence detection looking for my Mi band 3 and/or phone. (step out of elevator, approach door, door unlocks), like @michel72 seems to be doing here: Xiaomi Sherlock Smartlock - #199 by michel72 (could you share success/details please?).

[Updated 2019-08-01]: I got this working using ESPHome and the same ESP32 that the fob is attached to, but the detection lag was too long to detect me as I apprach door. I went down a ‘Monitor’ path (find that thread).

  • It would be nice if the esp32 can be a somewhat dedicate device with a short scan interval so my Pi doesn’t get stressed out?

  • Update: I am super excited that the esp32 BLE scanner can see my Mi Band 3. Detecting these has been in demand and a big hassle for quite few people, so I’ll share how that goes in a few other threads if it is fruitful.

  • Monitor for reliability.
    [Updated 2019-08-01]: It has been very solid! happy!

[Updated 2020-05-14]: Battery light just started flashing red. meaning I got an amazing 12 months from a single charge!

8 Likes

For the ones who have the vertical key and do not want to modify the lock. I used this mod, just cutting the key and solder it at 90 degrees.

2 Likes