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.

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.