First Attempt -- does this config look okay? ESP8622 (v2) MQTT Key Fob

So I have never used esp anything let alone esphome… For my first attempt, I intend on making a wirelessly controlled key fob for my car (They disabled the 3g modem last year)

So I am just curious if this yaml config looks okay or not… My intention is to have the esp8266 listen to an MQTT server and when it sees a topic change, perform said action…

I have broken apart a fob and soldered wires onto the push buttons. I intend to connect the wires to the esp gpio when I get it and hopefully this program will take care of business.

Note – the signal required for the push button is a logical low.

name: "MKX Keyfob"
# Optional variables:
retain: true
discovery: true
availability:
  topic: mkxremote/availability
  payload_available: online
  payload_not_available: offline
#If you enable MQTT and you do not use the “native API” for Home Assistant, you must remove the api: line from your ESPHome configuration
# Example configuration entry
mqtt:
  broker: you.to.org
  username: SumDumGi
  password: NoT2DaY
  birth_message:
    topic: mkxremote/availability
    payload: online
  will_message:
    topic: mkxremote/availability
    payload: offline
  shutdown_message:
    topic: mkxremote/availability
    payload: offline
  on_message:
     - topic: mkxremote/lock
       then:
         - button.press: lock_button
         - mqtt.publish:
           topic: mkxremote/lock/confirmed
           payload: "We recieved the lock command!"
     - topic: mkxremote/unlock
       then:
         - button.press: unlock_button
         - mqtt.publish:
           topic: mkxremote/unlock/confirmed
           payload: "We recieved the unlock command!"
     - topic: mkxremote/start
       then:
         - button.press: start_button
         - mqtt.publish:
           topic: mkxremote/start/confirmed
           payload: "We recieved the start command!"
- topic: mkxremote/stop
       then:
         - button.press: stop_button
         - mqtt.publish:
           topic: mkxremote/stop/confirmed
           payload: "We recieved the stop command!"
           
           
#list IO pins as switches and later template?? perhaps I can just template... I dont know
switch:
  - platform: gpio
    pin: D1 #GPIO5
    name: "Lock MKX"
    id: lock_mkx
  - platform: gpio
    pin: D2 #GPIO4
    name: "Unlock MKX"
    id: unlock_mkx
  - platform: gpio
    pin: D3 #GPIO0
    name: "Start MKX"
    id: start_mkx
###Start templating for simulating button presses on key fob
button:
  - platform: template
    name: "MKX Door Lock"
    id: lock_button
    on_press:
      - switch.turn_on: lock_mkx
      - delay: 0.1s
      - switch.turn_off: lock_mkx
  - platform: template
    name: "MKX Door Unlock"
    id: unlock_button
    on_press:
      - switch.turn_on: unlock_mkx
      - delay: 0.1s
      - switch.turn_off: unlock_mkx
  - platform: template
    name: "MKX Remote Start"
    id: start_button
    on_press:
      - switch.turn_on: lock_mkx
      - delay: 0.1s
      - switch.turn_off: lock_mkx
      - delay: 0.1s
      - switch.turn_on: start_mkx
      - delay: 0.1s
      - switch.turn_off: start_mkx
      - delay: 0.1s
      - switch.turn_on: start_mkx
      - delay: 0.1s
      - switch.turn_off: start_mkx
  - platform: template
    name: "MKX Remote Stop"
    id: stop_button
    on_press:
      - switch.turn_on: unlock_mkx
      - delay: 0.1s
      - switch.turn_off: unlock_mkx
      - delay: 0.1s
      - switch.turn_on: start_mkx
      - delay: 0.1s
      - switch.turn_off: start_mkx
      - delay: 0.1s
      - switch.turn_on: start_mkx
      - delay: 0.1s
      - switch.turn_off: start_mkx

The indent is wrong on the topic line.

Apart from that and the missing base config (esp_home: and esp32: or esp8266 section, and the network config which I assume you left out on purpose?) this looks like a good start.

Thanks for taking a look! Kinda wish I could use the editor in esphome without the device but I’ll have it soon.
I’ll fix the indents & you would be correct, I left those out (wifi and chipset info) on purpose - I’m pretty sure esphome fills that in when I first pair the device

Thanks again!
Chris

–correction… you can add a device in esphome for coding without having the device – I didnt even try, only watched videos lol

So after adding it to the dashboard (still without the device) I found a bunch of things that needed tweaking / removing… anyways, here’s the updated code – had lots of indent issues apparently, struggled with the mqtt.publish indentation =P

esphome:
  name: mkxremote
  friendly_name: MKXRemote

esp8266:
  board: nodemcuv2

# Enable logging
logger:
ota:
  password: "~~~~~~~~~~~~"
wifi:
  networks:
  - ssid: ~~~~~~~~~~~~~
    password: "~~~~~~~~~~"
  - ssid: ~~~~~~~~
    password: "~~~~~~~~~"

mqtt:
  broker: ~~~~~~~~~~
  username: ~~~~~~~
  password: ~~~~
  birth_message:
    topic: mkxremote/availability
    payload: online
  will_message:
    topic: mkxremote/availability
    payload: offline
  shutdown_message:
    topic: mkxremote/availability
    payload: offline
  #Watch for commands 
  on_message:
    - topic: mkxremote/lock
      then:
        - button.press: lock_button
        - mqtt.publish:
            topic: "mkxremote/lock/confirmed"
            payload: "We recieved the lock command!"
    - topic: mkxremote/unlock
      then:
        - button.press: unlock_button
        - mqtt.publish:
            topic: mkxremote/unlock/confirmed
            payload: "We recieved the unlock command!"
    - topic: mkxremote/start
      then:
        - button.press: start_button
        - mqtt.publish:
            topic: mkxremote/start/confirmed
            payload: "We recieved the start command!"
    - topic: mkxremote/stop
      then:
        - button.press: stop_button
        - mqtt.publish:
            topic: mkxremote/stop/confirmed
            payload: "We recieved the stop command!"

#list IO pins as switches and later template?? perhaps I can just template... I dont know
switch:
  - platform: gpio
    pin: D1 #GPIO5
    name: "Lock MKX"
    id: lock_mkx
  - platform: gpio
    pin: D2 #GPIO4
    name: "Unlock MKX"
    id: unlock_mkx
  - platform: gpio
    pin: D3 #GPIO0
    name: "Start MKX"
    id: start_mkx
    
###Start templating for simulating button presses on key fob
button:
  - platform: template
    name: "MKX Door Lock"
    id: lock_button
    on_press:
      - switch.turn_on: lock_mkx
      - delay: 0.1s
      - switch.turn_off: lock_mkx
  - platform: template
    name: "MKX Door Unlock"
    id: unlock_button
    on_press:
      - switch.turn_on: unlock_mkx
      - delay: 0.1s
      - switch.turn_off: unlock_mkx
  - platform: template
    name: "MKX Remote Start"
    id: start_button
    on_press:
      - switch.turn_on: lock_mkx
      - delay: 0.1s
      - switch.turn_off: lock_mkx
      - delay: 0.1s
      - switch.turn_on: start_mkx
      - delay: 0.1s
      - switch.turn_off: start_mkx
      - delay: 0.1s
      - switch.turn_on: start_mkx
      - delay: 0.1s
      - switch.turn_off: start_mkx
  - platform: template
    name: "MKX Remote Stop"
    id: stop_button
    on_press:
      - switch.turn_on: unlock_mkx
      - delay: 0.1s
      - switch.turn_off: unlock_mkx
      - delay: 0.1s
      - switch.turn_on: start_mkx
      - delay: 0.1s
      - switch.turn_off: start_mkx
      - delay: 0.1s
      - switch.turn_on: start_mkx
      - delay: 0.1s
      - switch.turn_off: start_mkx

Also, here’s the test stage of the fob – all the wires are just soldered to the output side of the push buttons and the ground is attached to the negative of the battery
fob

Well, finally got all my pieces… Wired up a dev board and deployed the program. Everything works well and the MQTT messages I had setup were irrelevant. All I needed was the switch / buttons and I send my commands via Home Assistant. This weekend I will switch to a D1 Mini and solder all my connections to a blank PCB board.

If anyone has a recommendation on how I can remove the switches from the yaml config, I’d love to hear about it – Currently I have disabled them in Home Assistant which is fine as well but I feel like its unnecessary coding.

Current Code Deployed

esphome:
  name: mkxremote
  friendly_name: MKXRemote

esp8266:
  board: nodemcuv2

# Enable logging
logger:
ota:
  password: "~~~~~~~~~~~~~~~~~~~"
wifi:
  networks:
  - ssid: ~~~~~~
    password: "~~~~~~~~~~~~"
    hidden: True
    priority: 0
  - ssid: ~~~~~~~~
    password: "~~~~~~~~~"
    priority: 1

mqtt:
  broker: ~~~~~~~~~
  username: ~~~~~~~~
  password: ~~~~~~~
  birth_message:
    topic: mkxremote/availability
    payload: online
  will_message:
    topic: mkxremote/availability
    payload: offline
  keepalive: 13s

#list IO pins as switches and later template?? perhaps I can just template... I dont know
switch:
  - platform: gpio
    pin: D1 #GPIO5
    inverted: True
    name: "Lock MKX"
    id: lock_mkx
  - platform: gpio
    pin: D2 #GPIO4
    inverted: True
    name: "Unlock MKX"
    id: unlock_mkx
  - platform: gpio
    pin: D3 #GPIO0
    inverted: True
    name: "Start MKX"
    id: start_mkx
    
###Start templating for simulating button presses on key fob
button:
  - platform: template
    name: "MKX Door Lock"
    id: lock_button
    on_press:
      - switch.turn_on: lock_mkx
      - delay: 0.3s
      - switch.turn_off: lock_mkx
  - platform: template
    name: "MKX Door Unlock"
    id: unlock_button
    on_press:
      - switch.turn_on: unlock_mkx
      - delay: 0.3s
      - switch.turn_off: unlock_mkx
  - platform: template
    name: "MKX Remote Start"
    id: start_button
    on_press:
      - switch.turn_on: lock_mkx
      - delay: 0.3s
      - switch.turn_off: lock_mkx
      - delay: 0.3s
      - switch.turn_on: start_mkx
      - delay: 0.3s
      - switch.turn_off: start_mkx
      - delay: 0.3s
      - switch.turn_on: start_mkx
      - delay: 0.3s
      - switch.turn_off: start_mkx
  - platform: template
    name: "MKX Remote Stop"
    id: stop_button
    on_press:
      - switch.turn_on: unlock_mkx
      - delay: 0.3s
      - switch.turn_off: unlock_mkx
      - delay: 0.3s
      - switch.turn_on: start_mkx
      - delay: 0.3s
      - switch.turn_off: start_mkx
      - delay: 0.3s
      - switch.turn_on: start_mkx
      - delay: 0.3s
      - switch.turn_off: start_mkx

Mark your switches as internal: true if you don’t want them shown in HA.

You can achieve the same result by leaving the name: out

I’m looking to do something very similar but with a household alarm remote. Would it be possible to go into a little more detail about the hardware side of things? Some closeup picts of the wiring would be really helpful if you could get them. I promise I’ll post a writeup when I get my setup working for other to see as well.

Unfortunately, I do not have any pictures from where the fob was opened and where I soldered the wires. However, the process was fairly simple… I know which button performs which action and I used a multimeter to ohm out the connection terminals on the microswitch. Once I knew how the switch itself functioned, I identified the input side and the output side of the switch. In my case, the negative from the battery was on one side of the micro switch. — I only did this for 3 buttons (Lock / Unlock / Start – omitted trunk and panic)
I have 5 wires going into the fob from the esp module:
– 3v / ground / lock / unlock / start
I removed the battery altogether and have the esp module supply power only when I send a command (this keeps anyone from being able to drive the car since I leave the fob hidden inside)
The esp module sends a low output for each wire (since that is what the keyfob looks for)

Once the output is toggled, it completes the circuit on the fob as if I actually pressed the button.
Here is the current code I have setup in espHome for reference

esphome:
  name: mkxremote
  friendly_name: MKXRemote
esp8266:
  board: d1_mini
ota:
  password: "##SomePassword##"
wifi:
  manual_ip:
      static_ip: 192.168.30.22
      gateway: 192.168.30.1
      subnet: 255.255.255.0
      dns1: 1.1.1.1
      dns2: 192.168.10.15
  networks:
  - ssid: Some.Hot.Spot
    password: "SomePassword"
    hidden: True
    priority: 0
  - ssid: Some.Other.Wifi
    password: "Some.Other.PW"
    priority: 1
text_sensor:
  - platform: wifi_info
    ssid:
      name: MKX WiFi iD
mqtt:
  broker: some.mqtt.server
  username: user
  password: password
switch:
  - platform: restart
    name: "MKX Reboot Remote"
    id: mkx_reboot_remote
  - platform: gpio
    pin: D1 #GPIO5
    inverted: True
    name: "Lock MKX"
    id: lock_mkx
    internal: true #Don't show switch in home assistant / Web interface
  - platform: gpio
    pin: D2 #GPIO4
    inverted: True
    name: "Unlock MKX"
    id: unlock_mkx
    internal: True
  - platform: gpio
    pin: D3 #GPIO0
    inverted: True
    name: "Start MKX"
    id: start_mkx
    internal: True
  - platform: gpio
    pin: D6
    name: "MKX Enable Remote"
    id: mkx_enable_remote
lock:
  - platform: template
    name: "MKX Lock"
    lock_action:
      - switch.turn_on: mkx_enable_remote
      - delay: 1s
      - switch.turn_on: lock_mkx
      - delay: 0.3s
      - switch.turn_off: lock_mkx
      - delay: 5s
      - switch.turn_off: mkx_enable_remote
    unlock_action:
      - switch.turn_on: mkx_enable_remote
      - delay: 1s
      - switch.turn_on: unlock_mkx
      - delay: 0.3s
      - switch.turn_off: unlock_mkx
      - delay: 5s
      - switch.turn_off: mkx_enable_remote
    optimistic: True
    assumed_state: True
cover:
  - platform: template
    name: "MKX Window"
    open_action:
      - switch.turn_on: mkx_enable_remote
      - delay: 1s
      - switch.turn_on: unlock_mkx
      - delay: 0.3s
      - switch.turn_off: unlock_mkx
      - delay: 0.3s
      - switch.turn_on: unlock_mkx
      - delay: 8s
      - switch.turn_off: unlock_mkx
      - delay: 5s
      - switch.turn_off: mkx_enable_remote
    close_action:
      - switch.turn_on: mkx_enable_remote
      - delay: 0.5s
      - switch.turn_on: lock_mkx
      - delay: 8s
      - switch.turn_off: lock_mkx
      - delay: 5s
      - switch.turn_off: mkx_enable_remote
    optimistic: true
    assumed_state: True
button:
  - platform: template
    name: "MKX Remote Start"
    id: mkx_start_button
    on_press:
      - switch.turn_on: mkx_enable_remote
      - delay: 1.5s
      - switch.turn_on: lock_mkx
      - delay: 0.5s
      - switch.turn_off: lock_mkx
      - delay: 0.5s
      - switch.turn_on: lock_mkx
      - delay: 0.5s
      - switch.turn_off: lock_mkx
      - delay: 0.5s
      - switch.turn_on: start_mkx
      - delay: 0.5s
      - switch.turn_off: start_mkx
      - delay: 0.5s
      - switch.turn_on: start_mkx
      - delay: 0.5s
      - switch.turn_off: start_mkx
      - delay: 5s
      - switch.turn_off: mkx_enable_remote
  - platform: template
    name: "MKX Remote Stop"
    id: mkx_stop_button
    on_press:
      - switch.turn_on: mkx_enable_remote
      - delay: 1.5s
      - switch.turn_on: unlock_mkx
      - delay: 0.5s
      - switch.turn_off: unlock_mkx
      - delay: 0.5s
      - switch.turn_on: start_mkx
      - delay: 0.5s
      - switch.turn_off: start_mkx
      - delay: 0.5s
      - switch.turn_on: start_mkx
      - delay: 0.5s
      - switch.turn_off: start_mkx
      - delay: 5s
      - switch.turn_off: mkx_enable_remote

I’m sure I can clean it up some more but I am satisfied… and the only reason for the static address stuff was because I wanted to be able to remotely access the optional built in webpage… It didn’t work since the hotspot wouldn’t let me forward ports =( – oh well, MQTT always works =]

Hey- did you ever get this working? I’m trying to do this with the same keyfob which is used for my F-150. Wondering which pins you used on the buttons when soldering wires to them.

Yeah, it works great. I would change a couple things if I was starting from scratch though.

  1. Wire the trunk opener / closer
  2. Add a wire to the status LED (green flash - remote start worked / red - failed) Then I could repeat the command if it failed

Anyways, to answer you question specifically… I do not know which pins you will require. I used a multimeter to follow the circuits. I checked with the Ohm Meter and also checked for DC voltage under different states (button pressed / released) Multimeters are fairly inexpensive and have a rather small learning curve. I would highly suggest investing in one and taking a little time to get acquainted with it.

ended up getting it working this morning. used your advice with the multimeter and learned how the switch worked. copied your code… didn’t know you had to keep 3.3v to the switches until pressed.

thanks man this was frustrating me for weeks.

1 Like