ESPHome with Existing Arduino Programs

The inverting seemed to fix it as well as switching the pins. Now the only thing I think I am missing is being able to mute it.

I assume for being able to mute the doorbell I will want to use something like this

switch.is_on / switch.is_off Condition[

This condition passes if the given switch is on/off.

# in a trigger:
on_...:
  if:
    condition:
      switch.is_on: my_switch
      # same goes for is_off
    then:
    - script.execute: my_script

Well if that delay worked properly it was supposed to only pulse the relay, not leave it on. Under the binary_sensor where I put on_press: you can also put a new line for on_release: and do a similar action, but switch.turn_off for the relay

Well the relay turned on right after I uploaded the code and stayed on. Then I pushed the button and it turned off for 1/2 sec then turned back on. Basically it was on before I pushed the button even so it was backwards. No big deal I don’t think.

Just get rid of the invert I put for the output in the code I gave you. Remove - ‘Inverted: true’

It works fine without the inverted: true

I am still trying to figure out a way to mute the doorbell.

Also I took a look at the LEDs one. It seems pretty straight forward. The only thing I could not figure out was if I wanted to set a portion of the LEDs to one color and another portion to a different color. I don’t necessarily want them to change just remain two (or more) different colors.

I am starting to see how ESPHome works and I am starting to like it. It is definately a lot easier than writing the code in the Arduino software.

When you say you want to mute the bell, do you mean turn it off after pressing the button? Is the delay, then off command not working?

I am guessing I will have to do something with the lambda for the sound. But I am not sure how this would work. I have a switch setup in HA switch.doorbell_sound but unsure how to get this to work

filters:
      - lambda: |-
          if (id(other_binary_sensor).state) {
            return x;
          } else {
            return {};
          }

Isn’t the bell only sounding while the relay is energised?

No in my code I was able to flip a switch in HA so that if someone pushed the button the doorbell would not ring. So at night time I mute it or when the kids are taking naps so it doesn’t wake them up when we get a delivery.

Oh right! Now I get you. There’s a few ways to do that. You could use an input_boolean as a condition in the ESP automation. I’m on a plane about to take off, I’ll post the solution when I land

No problem. Have a good flight

Cheers :grin:. Ok, I’m back on the ground. Do you mind sharing your current code so I don’t end up changing things back to the wrong pins etc?

I think the easiest way to create a ‘mute’ function is going to be to remove the automation code from the ESP and do this in HA instead, that way we can easily incorporate a condition for an input_boolean.doorbell_mute to be off

EDIT: I found another way, a way I was hoping for so that the automation stays within the ESP (so it can function irrelevant of HA status). I’ll post below.

So create the input_boolean.doorbell_mute (or whatever you want to call it) in HA.

In your ESP code include the following:

text_sensor:
  - platform: homeassistant
    name: "Doorbell Mute"
    entity_id: input_boolean.doorbell_mute
    id: mute1

The in the ESP automation we created for the button push, include a condition:

binary_sensor:
  - platform: gpio
    name: "Doorbell button"
    icon: mdi:bell-circle-outline
    pin:
      number: D2  # I think you have since changed this pin
      mode: INPUT_PULLUP
    filters:
      - delayed_on: 10ms   # THIS DOES THE DEBOUNCE
    on_press:
      then:
        - if:
            condition:
              lambda: 'return id(text_sensor.mute1).state == off;'
            then:
              - switch.turn_on: relay1
              - delay: 700ms
              - switch.turn_off: relay1

…now I hope that is correct. Give it a try

i got that loaded in but getting this error. havent had time to look at it as i am away from home right now

src/main.cpp: In lambda function:
src/main.cpp:38:28: error: expected primary-expression before '.' token
return id(text_sensor.mute1).state == off;
^
src/main.cpp:38:34: error: 'id' was not declared in this scope
return id(text_sensor.mute1).state == off;
^
src/main.cpp:38:45: error: 'off' was not declared in this scope
return id(text_sensor.mute1).state == off;
^
Compiling .pioenvs/doorbell/lib95c/esphome-core/esphome/binary_sensor/pn532_component.cpp.o
src/main.cpp:39:3: warning: control reaches end of non-void function [-Wreturn-type]
});
^
*** [.pioenvs/doorbell/src/main.cpp.o] Error 1
========================== [ERROR] Took 11.26 seconds ==========================

I’ve been told we need to use home assistant_binary.sensor. I’ll look into it further a bit later, and I’ll try to fix that I’d error issue for you

@sparkydave

I worked on this a little today but haven’t gotten very far. I am still getting the similar error.

Here is my current code.

esphome:
  name: doorbell
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: !secret wifissid
  password: !secret wifipassword
  manual_ip:
    static_ip: XXX.XXX.XXX.XXX   # CHANGE THESE TO SUIT YOUR NETWORK
    gateway: XXX.XXX.XXX.XXX
    subnet: XXX.XXX.XXX.XXX

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: 'XXXXXX'

ota:
  password: 'XXXXXX'


binary_sensor:
  - platform: homeassistant
    name: "Doorbell Mute"
    entity_id: input_boolean.doorbell_mute
    id: mute1

  - platform: gpio
    name: "Doorbell button"
   # icon: mdi:bell-circle-outline
    pin:
      number: D4  # I think you have since changed this pin
      mode: INPUT_PULLUP
    filters:
      - delayed_on: 10ms   # THIS DOES THE DEBOUNCE
    on_press:
      then:
        - if:
            condition:
              lambda: 'return id(binary_sensor.mute1).state == off;'
            then:
              - switch.turn_on: relay1
              - delay: 700ms
              - switch.turn_off: relay1

switch:
  - platform: gpio
    name: "Doorbell Relay"
    id: relay1
    #icon: mdi:alarm-bell
    pin: 
      number: D2
      inverted: false

I’m using the same code for my led strips, I would love to see how you implement effects from BRUH’s code to esphome. I did flash one of my led strips with EspHome, but I am missing effects that are in BRUH’s original arduino code.

Ok so I think I have this working now for the doorbell. See code below. I have yet to try the LED strips yet but that is next. Not sure if I will get them exactly the same but I don’t think I need to.

binary_sensor:
  - platform: homeassistant
    name: "Doorbell Mute"
    entity_id: input_boolean.doorbell_mute
    id: mute1

  - platform: gpio
    name: "Doorbell button"
   # icon: mdi:bell-circle-outline
    pin:
      number: D4  # I think you have since changed this pin
      mode: INPUT_PULLUP
    filters:
      - invert:
      - delayed_on: 10ms   # THIS DOES THE DEBOUNCE
    on_press:
      then:
        - if:
            condition:
              binary_sensor.is_off: mute1
              #lambda: 'return id(binary_sensor.mute1).state == off;'
            then:
              - switch.turn_on: relay1
              - delay: 700ms
              - switch.turn_off: relay1

switch:
  - platform: gpio
    name: "Doorbell Relay"
    id: relay1
    #icon: mdi:alarm-bell
    pin: 
      number: D2
      inverted: false
3 Likes

That looks right, I think. Glad to hear it’s working.

check out my code snippet above to see some effects thrown in, which ones are you missing?