HA, ESPHome, Switch state reports opposite of actual condition

Hi,

I have a 8 port switching relay bank for DC circuits, connected to a 30 pin ESP32 dev board. The relays are powered by an independent 5V supply. On startup, the relays are powered, but HA shows the state as “off”. Turning “on” the switches in HA shows their state as "on, but the relays turn off. Setting inverted: True causes the relays to work correctly (i.e., the relays only allow loads to function when switched “on” in HA) but HA logs show the state now to be “off”.

Is there a way to get all this to agree?

hi, Please share the esphome code and HA logs

No no no no no and no. Read this How to help us help you - or How to ask a good question - all of it but particularly point 12.

OK… on it.

OK, so to Nick’s point…

I am brand new to this, and while I have a decent background in Linux and electronics in general, I know next to nothing about HA or ESPHome. Let’s face it folks, the documentation for both HA and ESPHome are sparse and most documentation/support is provided by the Community. The previous threads I’ve searched don’t address this issue and/or have very little information about what was done to solve it. I don’t expect somebody to do the work for me… I’m hoping for a pointer in the right direction.

I am running HA OS 6.6 in a VM under Ubuntu 20.04.3 with KVM hypervisor. The HA OS image was provided by HA. The Supervisor is 2021.10.8, and Core is 2021.11.1

I thought I was clear about the problem: The function of the device and controller does not match the reported state. The device function is the opposite of the expected state. Correcting this function to the expected state by placing “inverted: True” in each pin/switch definition works for function - but the logs show the exact opposite of what is reality. The current state is what is reflected by the n3.yaml file below. The only change I made I saw in another thread… “inverted: True”. When that didn’t work, I went back to default - the yaml file below.

The current configuration has the relays and thir loads “ON” even though the HA dashboard switches are “OFF”. The logs also show them “OFF”; as evidenced by them showing as set to “ON” when you toggle them “ON” in HA. The logs then reflect them being toggled “OFF” when the HA dashboard is used to toggle them “OFF”. The problem, again, is that the HA switch toggle and resultant log entries are 180 degrees out of phase with the reality: the devices are “ON” when HA says they are “OFF” and vice versa (under the yaml used below) Using “inverted: True” in each pin definition results in correct function (loads and relays are “OFF” or “ON” when the HA dashboard says they should be), but the log shows the state as opposite of that.

All I wish to do is have the State reflect the Reality. I can use “inverted: True” to correct the function of my devices and the HA dashboard. Is this possible?

n3.yaml

esphome:
  name: n3
  platform: ESP32
  board: esp32dev

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "****"

ota:
  password: "****"

wifi:
  ssid: "****"
  password: "****"

# Optional manual IP
#  manual_ip:
#    static_ip: 192.168.0.102
#    gateway: 192.168.0.1
#    subnet: 255.255.255.0
#    dns1: 192.168.0.1

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "N3 Fallback Hotspot"
    password: "****"

captive_portal:

switch:
  - platform: gpio
    pin: GPIO15   #D15
    name: "SW-1" 
    id: SW1
  - platform: gpio 
    pin: GPIO2  #D2
    name: "SW-2"
    id: SW2
  - platform: gpio 
    pin: GPIO4  #D4
    name: "SW-3" 
    id: SW3
  - platform: gpio
    pin: GPIO16   #RX2
    name: "SW-4" 
    id: SW4
  - platform: gpio 
    pin: GPIO17  #TX2
    name: "SW-5"
    id: SW5
  - platform: gpio 
    pin: GPIO5  #D5
    name: "SW-6" 
    id: SW6
  - platform: gpio
    pin: GPIO18   #D18
    name: "SW-7" 
    id: SW7
  - platform: gpio 
    pin: GPIO19  #D19
    name: "SW-8"
    id: SW8

INFO Reading configuration /config/esphome/n3.yaml…
INFO Starting log output from n3.local using esphome API
INFO Successfully connected to n3.local
[00:50:31][I][app:099]: ESPHome version 2021.10.3 compiled on Nov 8 2021, 10:56:40
[00:50:31][C][wifi:490]: WiFi:
[00:50:31][C][wifi:352]: Local MAC: 94:B9:7E:DA:37:04
[00:50:31][C][wifi:353]: SSID: [redacted]
[00:50:31][C][wifi:354]: IP Address: 192.168.0.102
[00:50:31][C][wifi:356]: BSSID: [redacted]
[00:50:31][C][wifi:357]: Hostname: ‘n3’
[00:50:31][C][wifi:359]: Signal strength: -54 dB ▂▄▆█
[00:50:31][C][wifi:363]: Channel: 1
[00:50:31][C][wifi:364]: Subnet: 255.255.255.0
[00:50:31][C][wifi:365]: Gateway: 192.168.0.1
[00:50:31][C][wifi:366]: DNS1: 192.168.0.1
[00:50:31][C][wifi:367]: DNS2: 0.0.0.0
[00:50:31][C][switch.gpio:048]: GPIO Switch ‘SW-1’
[00:50:31][C][switch.gpio:049]: Pin: GPIO15
[00:50:31][C][switch.gpio:071]: Restore Mode: Restore (Defaults to OFF)
[00:50:31][C][switch.gpio:048]: GPIO Switch ‘SW-2’
[00:50:31][C][switch.gpio:049]: Pin: GPIO2
[00:50:31][C][switch.gpio:071]: Restore Mode: Restore (Defaults to OFF)
[00:50:31][C][switch.gpio:048]: GPIO Switch ‘SW-3’
[00:50:31][C][switch.gpio:049]: Pin: GPIO4
[00:50:31][C][switch.gpio:071]: Restore Mode: Restore (Defaults to OFF)
[00:50:31][C][switch.gpio:048]: GPIO Switch ‘SW-4’
[00:50:31][C][switch.gpio:049]: Pin: GPIO16
[00:50:31][C][switch.gpio:071]: Restore Mode: Restore (Defaults to OFF)
[00:50:31][C][switch.gpio:048]: GPIO Switch ‘SW-5’
[00:50:31][C][switch.gpio:049]: Pin: GPIO17
[00:50:31][C][switch.gpio:071]: Restore Mode: Restore (Defaults to OFF)
[00:50:31][C][switch.gpio:048]: GPIO Switch ‘SW-6’
[00:50:31][C][switch.gpio:049]: Pin: GPIO5
[00:50:31][C][switch.gpio:071]: Restore Mode: Restore (Defaults to OFF)
[00:50:31][C][switch.gpio:048]: GPIO Switch ‘SW-7’
[00:50:31][C][switch.gpio:049]: Pin: GPIO18
[00:50:31][C][switch.gpio:071]: Restore Mode: Restore (Defaults to OFF)
[00:50:31][C][switch.gpio:048]: GPIO Switch ‘SW-8’
[00:50:31][C][switch.gpio:049]: Pin: GPIO19
[00:50:31][C][switch.gpio:071]: Restore Mode: Restore (Defaults to OFF)
[00:50:31][C][logger:233]: Logger:
[00:50:31][C][logger:234]: Level: DEBUG
[00:50:31][C][logger:235]: Log Baud Rate: 115200
[00:50:31][C][logger:236]: Hardware UART: UART0
[00:50:31][C][captive_portal:150]: Captive Portal:
[00:50:31][C][ota:082]: Over-The-Air Updates:
[00:50:31][C][ota:083]: Address: n3.local:3232
[00:50:31][C][ota:086]: Using Password.
[00:50:31][C][api:134]: API Server:
[00:50:31][C][api:135]: Address: n3.local:6053
[00:50:31][C][api:139]: Using noise encryption: NO
[00:51:00][D][switch:013]: ‘SW-1’ Turning ON.
[00:51:00][D][switch:037]: ‘SW-1’: Sending state ON
[00:51:00][D][switch:013]: ‘SW-2’ Turning ON.
[00:51:00][D][switch:037]: ‘SW-2’: Sending state ON
[00:51:00][D][switch:013]: ‘SW-3’ Turning ON.
[00:51:00][D][switch:037]: ‘SW-3’: Sending state ON
[00:51:00][D][switch:013]: ‘SW-4’ Turning ON.
[00:51:00][D][switch:037]: ‘SW-4’: Sending state ON
[00:51:00][D][switch:013]: ‘SW-5’ Turning ON.
[00:51:00][D][switch:037]: ‘SW-5’: Sending state ON
[00:51:00][D][switch:013]: ‘SW-6’ Turning ON.
[00:51:00][D][switch:037]: ‘SW-6’: Sending state ON
[00:51:00][D][switch:013]: ‘SW-7’ Turning ON.
[00:51:00][D][switch:037]: ‘SW-7’: Sending state ON
[00:51:01][D][switch:013]: ‘SW-8’ Turning ON.
[00:51:01][D][switch:037]: ‘SW-8’: Sending state ON
[00:51:06][D][switch:017]: ‘SW-1’ Turning OFF.
[00:51:06][D][switch:037]: ‘SW-1’: Sending state OFF
[00:51:06][D][switch:017]: ‘SW-2’ Turning OFF.
[00:51:06][D][switch:037]: ‘SW-2’: Sending state OFF
[00:51:06][D][switch:017]: ‘SW-3’ Turning OFF.
[00:51:06][D][switch:037]: ‘SW-3’: Sending state OFF
[00:51:06][D][switch:017]: ‘SW-4’ Turning OFF.
[00:51:06][D][switch:037]: ‘SW-4’: Sending state OFF
[00:51:06][D][switch:017]: ‘SW-5’ Turning OFF.
[00:51:06][D][switch:037]: ‘SW-5’: Sending state OFF
[00:51:06][D][switch:017]: ‘SW-6’ Turning OFF.
[00:51:06][D][switch:037]: ‘SW-6’: Sending state OFF
[00:51:06][D][switch:017]: ‘SW-7’ Turning OFF.
[00:51:06][D][switch:037]: ‘SW-7’: Sending state OFF
[00:51:06][D][switch:017]: ‘SW-8’ Turning OFF.
[00:51:06][D][switch:037]: ‘SW-8’: Sending state OFF
[00:51:22][D][esp32.preferences:113]: Saving preferences to flash…

The log reflects the inverse of reality.

Oh you were clear about the problem, perhaps I didn’t clearly enough say that I was particularly directing you at point 12, formatting your code in the post. Looks good now.

ROFL! OK. Thanks for that :rofl: :rofl:

Could you point to the device you are controlling? A datasheet or an aliexpress page or something?

Sure! It is the 8-relay 5V model
[5V 12V 24V Relay Module With Optocoupler Relay Output 1 2 4 6 8 Way Relay Module For Arduino|Relays| - AliExpress]

It actually says it is activated on a low signal, so inverted: true is the correct config. Ignore what the log says, you’ll never look at it again.

1 Like

OK. Cool! Thanks so much! Will that have any effect on automations that rely on a State? I haven’t even started on automations yet, but am wondering if “If “on” (or “off”) do …” will behave properly.

Check the state in dev tools.

1 Like

Will do. Thanks again!