Esp D1 mini - with relay shield - state of relay is reversed

Hello guys,
I could use some help please. I have the following code in ESPHome for doorbell. The code is from Franck, I adjusted it for esp d1 with relay shield. The relay works in opposite way as it is stated in log. For example when the relay is on (led on relay is on) the state of doorbell chime in log is OFF, and when the relay is off, state is ON. The state of switch (virtual - in config) corresponds to relay state.
Is there any way to change the code so that the state will be ON, when the relay is ON. I know the wiring of doorbell chime in relay should be NO (open)…
I’m quite a noob, not even an engeneer or any near, and I’m trying to figure it out for few days now what is wrong.

Thank you in advance!

esphome:
  name: d1doorbell
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_pass

logger:

api:

ota:

web_server:
  port: 80

time:
  - platform: homeassistant
    id: homeassistant_time

text_sensor:
  - platform: version
    name: Doorbell ESPHome Version

  - platform: wifi_info
    ip_address:
      name: Doorbell IP
    ssid:
      name: Doorbell SSID
    bssid:
      name: Doorbell BSSID

sensor:

  - platform: uptime
    name: Doorbell_d1 Uptime
  - platform: wifi_signal
    name: Doorbell_d1 WiFi Signal
    update_interval: 60s

globals:
  - id: chime
    type: bool
    restore_value: true
    initial_value: 'true'

switch:
  - platform: restart
    name: Doorbell_d1 Restart
  - platform: gpio
    id: relay
    inverted: True
    name: Doorbell_d1 Chime
    pin: 
      number: D1 
      mode: OUTPUT
  - 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:
  - platform: gpio
    id: button
    name: Doorbell_d1 Button
    pin:
      number: D4 
      mode: INPUT_PULLUP
      inverted: true
    filters:
      - delayed_on: 65ms
      - delayed_off: 65ms
    on_press:
      then:
        if:
          condition:
            - switch.is_on: chime_active
          then:
            - switch.turn_on: relay
    on_release:
      - switch.turn_off: relay

Remove the line inverted: True from this:

switch:
  - platform: restart
    name: Doorbell_d1 Restart
  - platform: gpio
    id: relay
    inverted: True
    name: Doorbell_d1 Chime
    pin: 
      number: D1 

Thank you @tom_l ! It works, now I have to test it with doorbell chime :slight_smile:

1 Like