Wrong Light Status

I use this very basic code to turn lights on or off ( by using esphome); however if the light status in home assistant is on, the light is off and vice versa. Is there any way to make the light statue in home assistant and esp home to be the same as the real light status?

the code:
esphome:
name: test
friendly_name: Test

esp8266:
board: nodemcuv2

Enable logging

logger:

Enable Home Assistant API

api:
encryption:
key: “xxxx”

ota:
password: “xxxx”

wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password

Enable fallback hotspot (captive portal) in case wifi connection fails

ap:
ssid: “xxx”
password: “xxx”

captive_portal:

output:
- platform: gpio
id: “First_LED”
pin: D2

light:

  • platform: binary
    output: “First_LED”
    name: “Main Light”

Welcome to the HA forum!

First off: Please format your code to make it more readable. Indentation matters in yaml and it’s hard to spot errors when code isn’t formatted. Select the code and click on the </> symbol - or insert ´´´ before and after the code.

Secondly: I don’t know what kind of light you’re using, but you can try to add inverted: true in the output component:

output:
  - platform: gpio
    id: “First_LED”
    pin: D2
    inverted: true
1 Like

Thank you so much, it is a normal lamp connected to a relay

your solution works just fine, thank you so much

I have another problem if you could thankfully help me to solve it. Once the esp is connected to power, the esp turns on the relay for mile seconds then turns off again. how can i it make it directly turns off without it turning on for mile seconds at the beginning?

That usually has to do with the pin on the esp. Some pins are bootstrap pins and others are high on boot etc.

You can check recommended pins here:

I tried Pins D2 and D1 as the article recommended to usew these pins with relays but the same problem still exists

Both pins are low at boot, so it all sounds to me like you have chosen to use the NC configuration on the relay. You could try the other option of using NO on the relay instead. If you do, also remember to set reverse: false in the output component in your yaml.

Read more about NO (Normally Open) and NC (Normally Closed) here:

I am Using a relay module like this one, the led at the module lights up when the status on home assistant was OFF (before adding the reverse line). nothing is hooked up to the relay right now but i know if the esp is sending power to the relay from the led indicator at the relay board as I mentioned. The reverse line fixed that but the flicker in the beginning is what i can’t fix yet

Relay Module

To start with, make sure your relay module is wired correctly. When you use it with 3.3V MCU (like yours is), you need to remove that jumper.

Jd-vcc to 5V and Gnd next to it to Gnd

On the other pinheader Vcc to 3.3V and IN pin to Esp Gpio.

If you use it with the jumper, it’s sinking 5V to your Esp Gpio pin and your 3.3V board is not happy with it, at least in a long run.

1 Like

When your wiring is correct, try to get rid of that pulse on boot.

switch:
  - platform: gpio
    id: relay
    restore_mode: RESTORE_DEFAULT_OFF
    pin:
      number: D2
      inverted: True

Should the code be like this?
captive_portal:

switch:

  • platform: gpio
    id: “relay”
    restore_mode: RESTORE_DEFUALT_OFF
    pin: D2
    inverted: true

light:

  • platform: binary
    switch: “relay”
    name: “Second Light”

I don’t know your whole setup and I’m on mobile. Just give it a try.
Correct that misspelling RESTORE_DEFUALT_OFF

Once again; please format your code, as outlined in my previous post.

And yet again, try doing what I posted above.

You have two wires from the lamp going into the relay; leave the middle one where it is, but swap the other over to the other port. Look how it’s done on the page I linked to above.

Once you’ve done that, use this code:

output:
  - platform: gpio
    id: “First_LED”
    pin: D2
    inverted: false

light:
    platform: binary
    output: “First_LED”
    name: “Main Light”

the lamp is not yet connected to the to the relay module,
i just know if the relay is on from from the led indicator on the module board

You seem reluctant to try what I’m suggesting, so nevermind. I’m out.

Of course not, i am thankful for your support, but i don’t always just understand some of the things that you recommends to me for my lack of experience. I also think that i might not be always to make my point clear for helpers like you to understand (also for my inexperience). If we could communicate on another platform (may be a call where i can share my setup or any other way you are comfortable with), so we can understand each other better. I am really thankful and trying to do everything you recommend. I do appreciate your time and effort dear :fist_right: :fist_left: