Trying to build a doorbell

Hi All,

Last year I found Frenck’s blog regarding the doorbell sensor (https://frenck.dev/diy-smart-doorbell-for-just-2-dollar/)

Just what I was/am looking for!!
Ordered the parts, and upon receiving them I started the steps in the blog.
All is well until I validate the (copy+paste) code on the blog.

At that moment the errors come flowing:


Failed config

switch.template: [source /config/esphome/doorbell.yaml:81]
  platform: template
  name: Doorbell Chime Active
  id: chime_active
  restore_state: False
  turn_on_action:  [source /config/esphome/doorbell.yaml:86]
    
    'then' is a required option for [0].
    - [source /config/esphome/doorbell.yaml:86]
      
      [globals.set] is an invalid option for [0]. Please check the indentation.
      globals.set:  [source /config/esphome/doorbell.yaml:87]
        id: chime
        value: true
  turn_off_action:  [source /config/esphome/doorbell.yaml:90]
    
    'then' is a required option for [0].
    - [source /config/esphome/doorbell.yaml:90]
      
      [globals.set] is an invalid option for [0]. Please check the indentation.
      globals.set:  [source /config/esphome/doorbell.yaml:91]
        id: chime
        value: false
  lambda: return id(chime);

It looked like some indentation issue, but I can’t seem to find it.
Tried reaching out to Frenck, but he’s obviously too busy to answer my n00b answer :slight_smile:

Now 3 months along the line, I wanted to look into it again.
The issue lies within the templating used in the config…

Is there anyone out there that can explain to me (like I’m a 2 year old ;)) what’s wrong?
This is advanced yaml config templating I’m not ready for :wink:

You could try to tag @frenck in here.

Sometimes with copy pasting code, the indention of the code gets all messy. Validate what you pasted in your yaml files for errors : https://yamlvalidator.com/

That is so creative… a relay coil as a ‘sensor’ for the ac current instead of a hall sensor. I was hoping to also see the 5v bus being powered off the bell transformer, but that relay trick more than makes up for that.

1 Like

Spaces might be the problem, sure. However, I think there might be a legit bug with the code. Like, there wasn’t at the time it was written, but it wouldn’t be the first time syntax has changed between releases.

I took a look at Frenck’s code from the blog post and I think there might be an issue, as per the error message you’ve got.

The turn_on_action: command should normally be followed by a then: command, indented one level in. The following statements are then indented another level in.

So the problem section should probably look like this:


  - platform: template
    name: Doorbell Chime Active
    id: chime_active
    restore_state: false
    turn_on_action:
      then:
        - globals.set:
            id: chime
            value: 'true'
    turn_off_action:
        then:
          - globals.set:
            id: chime
            value: 'false'
    lambda: |-
      return id(chime);

I should say that I haven’t tested this at all, I’m just spitballing based on what I see. Also, don’t copy and paste what I just did, because copying and pasting it out of notepad++ screwed up my formatting and I have no idea if it’ll survive - somehow some tabs were introduced. So look for that in your code too.

I’m just saying that the error message you got isn’t about tabs and spaces. It’s about the fact that turn_on_action: needs a then: statement after it.

Best of luck, let us know if you succeed, because if this fixes it then we can message Frenck and offer him the updated code for his blog.

EDIT: I’ve just realised that it’s possible that the then: bit might follow instead of lead, as in:


    turn_on_action:
      - globals.set:
        id: chime
        value: 'true'
      then:

… and just leave the rest of the then: statement empty. Try both, see what works. Perhaps then: used to be optional, and became mandatory.

The then: tag is still optional with the most recent version 1.14.3 and (if you want to include it) it has to come before the desired actions, obviously. So @DeeBeeKay your first example is fine (syntax-wise).

I too saw this doorbell project and gave it a try, with a lot of help from kind people on this forum it now works a charm…

here is my yaml file (missing the wifi info and stuff )

esphome:
  name: front_doorbell
  platform: ESP8266
  board: esp01_1m

# wifi info goes here

# end of wifi info


captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

# Enable Web server.
web_server:
  port: 80

# Sync time with Home Assistant.
time:
  - platform: homeassistant
    id: homeassistant_time


# Sensors with general information.
sensor:
  # Uptime sensor used in following formatted uptime sensor
  - platform: uptime
    name: "Doorbell Do Not Use"
    id: doorbell_do_not_use
    internal: true
    update_interval: 600s

  # WiFi Signal sensor.
  - platform: wifi_signal
    name: Doorbell WiFi Signal
    update_interval: 600s


# Text sensors with general information.
text_sensor:
  # Expose ESPHome version as sensor.
  - platform: version
    name: Doorbell ESPHome Version
    
  # # Expose WiFi information as sensors.
  # - platform: wifi_info
  #   ip_address:
  #     name: Doorbell IP
  #   ssid:
  #     name: Doorbell SSID
  #   bssid:
  #     name: Doorbell BSSID

# Sensor to display uptime in Days/hours/minutes      
  - platform: template
    name: "Doorbell Uptime"
    lambda: |-
      uint32_t dur = id(doorbell_do_not_use).state;
      int dys = 0;
      int hrs = 0;
      int mnts = 0;
      if (dur > 86399) {
        dys = trunc(dur / 86400);
        dur = dur - (dys * 86400);
      }
      if (dur > 3599) {
        hrs = trunc(dur / 3600);
        dur = dur - (hrs * 3600);
      }
      if (dur > 59) {
        mnts = trunc(dur / 60);
        dur = dur - (mnts * 60);
      }
      char buffer[17];
      sprintf(buffer, "%ud %02uh %02um %02us", dys, hrs, mnts, dur);
      return {buffer};
    icon: mdi:clock-start
    update_interval: 600s



# Global to store the on/off state of the chime
globals:
  - id: chime
    type: bool
    restore_value: true
    initial_value: 'true'

# Exposed switches.
switch:
  # Switch to restart the doorbell.
  - platform: restart
    name: Doorbell Restart

  # Switch to turn on/off the chime.
  - platform: gpio
    id: relay
    inverted: true
    name: Doorbell Chime
    pin: GPIO0

  # Switch to turn on/off chime when
  # doorbell button is pushed.
  #
  # It creates a "virtual" switch based
  # on a global variable.
  - platform: template
    name: Doorbell Chime Active
    id: chime_active
    restore_state: false
    turn_on_action:
      - globals.set:
          id: chime
          value: 'true'          
    turn_off_action:
      - globals.set:
          id: chime
          value: 'false'
    lambda: |-
      return id(chime);
      
# Binary sensor representing the
# Doorbell button push.
binary_sensor:
  - platform: gpio
    id: button
    name: Doorbell Button
    pin:
      # Connected to GPIO on the ESP-01S.
      number: GPIO2
      mode: INPUT_PULLUP
      inverted: true
    filters:
      # Small filter, to debounce the button press.
      - delayed_on: 25ms
      - delayed_off: 25ms
    on_press:
      # Only turn on the chime when it is active, switch is then turned off to prevent shorting the system
      then:
        if:
          condition:
            - switch.is_on: chime_active
          then:
            - switch.turn_on: relay
            - delay: 100ms
            - switch.turn_off: relay
    on_release:
      # On release, turn of the chime
      - switch.turn_off: relay

I have an uptime sensor in days, hours and mins instead of just seconds just to see if it would work…
also due to my doorbell i have it turn on then off with a press, the release of the button does nothing.

hope it helps

edit: I also now have an automation so that when my doorbell button is pressed my alexa devices anounce that there is someone at the door, later additions to the automation are to pause the TV, show the CCTV feed for the door on the Tv and turn on the hall lights

1 Like

Thank you for posting your yaml, but also for the automation suggestions! Those are all great ideas.

1 Like

Crap! I’ve missed all these excellent replies and assistance even though I’ve had notifications switched on :frowning:

@Rdoull I’ve copied/pasted your yaml, hoping to find a quick fix and then reverse engineer it to learn what the … I’m doing. But even when yaml validator says it’s correct, esphome throws the errors around like it’s candy.

@DeeBeeKay I’ll look into your tips tomorrow. Thanks for that.

@truglodite @frenck lists a step down regulator on his tutorial page, maybe that fits your needs?

1 Like

I already have a bell sensor I made with arduino and a hall sensor (the hard way admittedly). I was just commenting on the beauty of simplicity here. Yeah if I was powering it off the bell power, I’d probably just use a rectifier+caps+regulator… I was being sarcastic about that not being part of Frenck’s circuit (imho, ac/dc conversion design is boring compared to that relay trick… new way to think of relays for me).

1 Like

Or I can just update ESPHome to the latest version and all of the errors are gone…

I had the HASSIO addon, but somehow it lost it’s connection to the hassio repository.
I noticed when visiting the esphome site and it notified me of a version update of esphome. Since I just updated all my addons, I decided to check. 1.12.3 :open_mouth:

Added the esphome repository and updated. Now the code validates :cry:

So… off to creating the fysical connections… Right after I fix my Sonoff that won’t come online after updating the ESP firmware of that one…
Everybody, thanks!

1 Like

does anybody know how this can be implemented using wemos d1 mini and 5v relay switch ? also would be nice to know the which wire go where between bell, wemos and relay.

1 Like

Hey @techy , This works for me.
U can use this yaml file, flash it to you D1 mini and stack the relay shield on that Wemos D1 mini. Ground your puch button to the gnd of the relay shield and the postive to pin D2 of the shield (i changed it in de code to D2).

I also made de IP static, you can change that ore leave it away.

good luck!!!

esphome:
  name: doorbell
  platform: ESP8266
  board: d1_mini

# WiFi connection, correct these
# with values for your WiFi.
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: true
  manual_ip:
   static_ip: 192.168.0.62
   gateway: 192.168.0.1
   subnet: 255.255.255.0

# Enable logging.
logger:

# Enable Home Assistant API.
api:

# Enable over-the-air updates.
ota:

# Enable Web server.
web_server:
  port: 80

# Sync time with Home Assistant.
time:
  - platform: homeassistant
    id: homeassistant_time

# Text sensors with general information.
text_sensor:
  # Expose ESPHome version as sensor.
  - platform: version
    name: Doorbell ESPHome Version
  # Expose WiFi information as sensors.
  - platform: wifi_info
    ip_address:
      name: Doorbell IP
    ssid:
      name: Doorbell SSID
    bssid:
      name: Doorbell BSSID

# Sensors with general information.
sensor:
  # Uptime sensor.
  - platform: uptime
    name: Doorbell Uptime

  # WiFi Signal sensor.
  - platform: wifi_signal
    name: Doorbell WiFi Signal
    update_interval: 60s

# Global to store the on/off state of the chime
globals:
  - id: chime
    type: bool
    restore_value: true
    initial_value: 'true'

# Exposed switches.
switch:
  # Switch to restart the doorbell.
  - platform: restart
    name: Doorbell Restart

  # Switch to turn on/off the chime.
  - platform: gpio
    id: relay
    inverted: false
    name: Doorbell Chime
    pin: D1

  # Switch to turn on/off chime when
  # doorbell button is pushed.
  #
  # It creates a "virtual" switch based
  # on a global variable.
  - platform: template
    name: Doorbell Chime Active
    id: chime_active
    restore_state: false
    turn_on_action:
      - globals.set:
          id: chime
          value: 'true'
    turn_off_action:
      - globals.set:
          id: chime
          value: 'false'
    lambda: |-
      return id(chime);

# Binary sensor representing the
# Doorbell button push.
binary_sensor:
  - platform: gpio
    id: button
    name: Doorbell Button
    pin:
      # Connected to GPIO on the ESP-01S.
      number: D2
      mode: INPUT_PULLUP
      inverted: true
    filters:
      # Small filter, to debounce the button press.
      - delayed_on: 30ms
      - delayed_off: 10ms
    on_press:
      # Only turn on the chime when it is active.
      then:
        if:
          condition:
            - switch.is_on: chime_active
          then:
            - switch.turn_on: relay
    on_release:
      # On release, turn of the chime.
      - switch.turn_off: relay

Thanks for the code. Can you share the picture of actual setup ? I want to see which wire goes where between door bell chime, Wemos and relay. My relay is standalone it does not come with shield.

I was hoping to also see the 5v bus being powered off the bell transformer

I used one of the 2-relay modules that works off of AC or DC (https://www.amazon.com/gp/product/B07GTKHCST, for example). The doorbell switches are now just binary sensors on GPIOs using the device ground, and the relays (which have an interlock so they don’t activate at the same time) switch the power from the doorbell transformer to the bell itself depending on if the front or rear doorbell button was pressed. That allows me to set the maximum on and off time for the now-virtual button (and frequency it can be pressed). Mostly to keep impatient people from hitting it a bunch, but also… Once a few years ago, someone delivering a package mashed the button hard and broke it, so when I got home a day later, my poor cats had all gone insane from that doorbell ringing constantly all that time. If the button stays held down now, it’s the same as if someone just pressed it for 3/4 second. :wink:

The main drawback with my current approach is that normal lighted doorbell switches don’t work, but I’m toying with the idea of pulling another wire or two to power an LED glued in place of the neon bulb for the one which is lit. :man_shrugging:

I recently changed my doorbell from a newer digital model, to an old school solenoid mechanical one. I was using a hall sensor (allegro brand breakout, wired to an arduino nano which makes an HA serial sensor), and that digital bell unit wasn’t drawing enough current at times, or it would not draw current for long enough to register. With the old school solenoid bell, I am now getting a very strong and reliable bell signal to HA… old lighted button still works since it is now using a 50+year old standardized circuit design (IMHO a much more charming natural tone than that ‘mp3 bell’ thing). Like you, I also added some timing and rising edge logic to ignore folks who fidget with the button, or even break buttons (sheesh… should report that guy lol).

Still, the hall sensor is not even in the same league of ‘maker cool’ as the relay idea. :wink:

Thanks for the pics. I am assuming all this setup is located inside chime box where wires from transformer and door bell meets ? what is that white portion ? Is that actually your transformer ? How did you power wemos using 5v ? My problem is i don’t have any electrical outlet near chime box where i can plug my wemos.

That grey box where your transformer is mounted will have 120V in it. You could tap that for 5V if you are comfy with mains wiring. Technically though, doing that is likely to violate building code (can’t have a 5V line inside a mains jbox). Otoh, it can be executed safely if you are careful. If not, a competent electrician can easily add an outlet near that box for minimal cost.

Alternatively, there are ways to derive 5VDC from that 24VAC transformer. It will be marginal, and not sure if the current draw may starve the solenoid and mute the bell. With a big enough capacitor I imagine this would be feasible.

Ive just got back on here… missed alot of info too. have you go tthis working yet? to be honest I have no idea what I am doing either… I punch buttons on this thing in from=nt of me and hope it works :wink: but I did get a load of help on this forum through various posts.

if you still have the errors ( which I doubt given the time) let me know what they are, I may have hit them as well :wink:

i still haven’t got it working. my plan is to use 5v relay switch and wemos d1 mini, put everything inside the speaker box attached to the wall ( where bell and transformer wires meet). can you help with that please ? i also have ac to dc buck converter i ordered from ebay. thanks