Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Status of switches wrong at startup Homeassistant #6

Closed
megapearl opened this issue Mar 10, 2022 · 38 comments
Closed

Status of switches wrong at startup Homeassistant #6

megapearl opened this issue Mar 10, 2022 · 38 comments

Comments

@megapearl
Copy link

megapearl commented Mar 10, 2022

Hi,

Upon startup the status of the switches are set to ON, while the status should have been off.
Physical the switches are off.
The binary_sensors do not have this problem.

configuration.yaml snippet:

switch:
  - platform: mcp23017
    i2c_address: 0x21
    pins:
      0: Frontdoor Lock
      1: Hallway Light
      2: Frontdoor Light
      3: Livingroom Light Table
      4: Livingroom Light Wall
    invert_logic: true
#    scan_interval: 1

binary_sensor:
  - platform: mcp23017
    i2c_address: 0x20
    pins:
      0: Livingroom Wall 1
      1: Livingroom Wall 2
      2: Livingroom Wall 3
      3: Livingroom Wall 4
      4: Livingroom Bar 1
      5: Livingroom Bar 2
      6: Livingroom Bar 3
      7: Livingroom Bar 4
      8: Hallway Light
      9: Frontdoor Light
      10: Frontdoor Pushbutton
      11: Frontdoor Phone
    invert_logic: true
#    scan_interval: 1
#    bouncetime: 4
#    pull_mode: DOWN

How to troubleshoot?

@jpcornil-git
Copy link
Owner

Hi @megapearl,

I see that you set invert_logic to true in your switch configuration, therefore HA state will be True/On when mcp23017 output is False/Off/0.

When you power up/cycle mcp23017, outputs will be set to 0 (device's reset value), HA will always read device state during tartup and report True/On if you set invert_logic to True.

Cheers,
jpc

@megapearl
Copy link
Author

megapearl commented Mar 10, 2022

Hi @jpcornil-git,

When I dash 'invert_logic: true' out under switch:, the lights turned on and door opens upon startup of homeassistant, and all of the switches are still on, and the logic is inverted, switch on, light off.
How to get expected behavier? The mcp23017 is hooked up to several relays (3.3v to optocoupler switches on 5v to coil = relay on) which are connected to the lock and lights.

For now I created an automation to get the expected behavier, but maybe you have a better idea.

- id: '20220310df01'
  alias: Switch off all relais on startup
  trigger:
  - platform: homeassistant
    event: start
  action:
  - service_template: switch.turn_off
    entity_id:
      - switch.frontdoor_lock
      - switch.hallway_light
      - switch.frontdoor_light
      - switch.livingroom_light_table
      - switch.livingroom_light_wall

@jpcornil-git
Copy link
Owner

Do you have a schematic of the circuitry connected to mcp23017 ?

It looks like a 0 at mcp23017 output translates into output active in your case. If this is correct, you can't change this as mcp23017 reset value is 0 by design, i.e. there will alwas be a period of time when the output will be active/before e.g. HA automation will swap values to desired behavior.

I can have a look at our schematic and suggest a change to add an inverting stage

@megapearl
Copy link
Author

megapearl commented Mar 10, 2022

Yes,

I use a Raspberry Pi 4 which is connected to an IO Plus (2x mcp23017) https://www.abelectronics.co.uk/kb/article/1052/driving-relays-or-higher-loads-with-the-io-pi which is connected to https://www.kiwi-electronics.nl/nl/twee-kanaals-5v-relais-module-911 that last one is 'active low' https://forums.raspberrypi.com/viewtopic.php?t=36225 which is connected to the lights and locks.

Strange thing is it is only in software the relay's are on (and only on startup) physicly they are off.

@jpcornil-git
Copy link
Owner

You meant the following mcp23017 board https://www.abelectronics.co.uk/p/54/io-pi-plus connected to https://www.kiwi-electronics.nl/nl/twee-kanaals-5v-relais-module-911 (active low) I suppose.

You can invert hardware by using the other output of the relay board (one in NO/Normaly Open and the other one NC/Normal Closed).

If I understand correctly how the boards are interconnected, you should have: mpc23017 board (0) -> Relay (on)

I'm not sure I understand correctl our last sentence but one potential issue here for our application is that when there is no power, the relay is off and when you power everything up it turns on (because mcp23017 will drive low/reset value), i.e. there is always a state change at power up.

@megapearl
Copy link
Author

megapearl commented Mar 10, 2022

Yes,

When there's no power (5v and 3.3v) the relay is off, the lights and locks are indeed connected to the NO contacts.
I can reverse that, and connect them to the NC contact but then when the raspberry pi is off (no power) the door is unlocked and the lights are on, until I boot up the pi.

@jpcornil-git
Copy link
Owner

jpcornil-git commented Mar 10, 2022 via email

@megapearl
Copy link
Author

megapearl commented Mar 10, 2022

When rebooting the pi using ssh and 'reboot' and setting is at 'invert_logic: true'

To summarize/confirm my understanding:

  • when there is no power, relay is off (and lights/locks as well as they
    are connected to NO)

Yes

  • when pi is powered up, nothing changes as mcp23017 outputs are all high-Z
    and don't pull relay input low

Yes

  • when HA starts up, it enables mcp23017 outputs and set output level to
    0/device reset value -> relay is on

No, relays stay off, but in homeassistant the switch says it is on.

After first startup in homeassistant when I switch it off in homeassistant nothing happens (because the relay never turned on in the first place), and relays stays off, in homeassistant the switch goes off, when I switch it on again in homeassistant the relays and the light switch on, and everything works as expected. I can then switch the relais on or off using the switches in homeassistant.

  • what you see in HA after power is 0/off as well (with invert_logic set to
    false)

Correct ?

When rebooting the pi using ssh and 'reboot' and setting is at 'invert_logic: false'

The relays stay off until homeassistant is started, then they turn on and the lights and locks physically turn on but in homeassistant switches are in the off state.
When I turn on the switch in home assistant the relay and lights go off and when I turn the switch off in homeassistant the relay and lights go on.

@megapearl
Copy link
Author

megapearl commented Mar 10, 2022

After some further testing I noticed that there is a difference by power cycling the pi (pull the plug and replug it) and rebooting the pi using ssh and 'reboot'.

When power cycling the pi (pulling the plug and replug it) and starting homeassistant (invert_logic: true) the relays stays off until homeassistant start up, then they turn on, and the switch in homeassistant is in the on state, as expected.

What I'm trying to acomplish is that if there's no power to the relays the lights stays off, and the locks stay locked, no matter if I reboot the pi or reboot homeassistant.

note:
The mcp23017 board and the relays draw their power from the 5v pin of the pi.

@jpcornil-git
Copy link
Owner

The only difference between a power cycle and a SW reboot is that mcp23017 is not reset with the latter and therefore output won't be 0 (reset value) but the last value set. HA will also read that value from the device upon start up, i.e. never force a default value.

Therefore rebooting the pi or restarting HA should not affect mcp23017 output as long as it stays powered (no device reset)

If your mcp23017 and relay board is powered from the rpi, you can never have a scenario where the fomer is not powered but the latter well ...

Did you tried to use a separate power for the relay board (if all relays switch on when HA starts they may draw too much current from RPI supply and e.g. create a voltage drop that may affect mcp23017 state)

Just want to make sure also that you are using the latest release of the code.

@megapearl
Copy link
Author

megapearl commented Mar 10, 2022

Haven't tried a separate power supply for the relay board yet, will try that tomorrow.
I'm using v1.0.0 of your component installed via HACS in homeassistant.
I noticed there are 2 versions, a 'main' one and a 'v1.0.0' one, switched to the 'main' one now, which is '3975c54'.

@jpcornil-git
Copy link
Owner

You could also try without the relay board (to remove its potential influence) and measure mcp23017 output/check against HA state.

@jpcornil-git
Copy link
Owner

... and only difference between main and released version is the manifest file (smbus2 version requirement changed from == to >=).

You are using the correct version in both cases, not an old one.

@megapearl
Copy link
Author

You could also try without the relay board (to remove its potential influence) and measure mcp23017 output/check against HA state.

The behavier is the same, I disconnected the relay boards, and restarted homeassistant, upon startup (with invert_logic: true) the switches are all on in homeassistant, when I connect the relay boards after startup I expected all the relays go ON but they stay off.

@megapearl
Copy link
Author

megapearl commented Mar 11, 2022

I expected the same behavier as the GPIO component.
I switched to a mcp23017 because the GPIO pins (configured as input) all broke after some months (stuck in high).

binary_sensor:
  - platform: rpi_gpio
    ports:
      16: Backdoor Pushbutton
    invert_logic: true
    bouncetime: 4

switch:
  - platform: rpi_gpio
    ports:
      21: Backdoor Lock
    invert_logic: true

With above setup the status of the relays is correct, and do not switch in software at startup, or soft and hard rebooting the pi.

@megapearl
Copy link
Author

So it looks like the status of the mcp23017 is read too soon or too late upon startup of homeassistant or invert_logic = true is triggering the state of the relays upon startup homeassistant.

@jpcornil-git
Copy link
Owner

You could also try without the relay board (to remove its potential influence) and measure mcp23017 output/check against HA state.

The behavier is the same, I disconnected the relay boards, and restarted homeassistant, upon startup (with invert_logic: true) the switches are all on in homeassistant, when I connect the relay boards after startup I expected all the relays go ON but they stay off.

Did you measured mcp23017 output in that case/without relay board (HA should report 'on' while mcp23017 output should be 0V) ?

@jpcornil-git
Copy link
Owner

I expected the same behavier as the GPIO component. I switched to a mcp23017 because the GPIO pins (configured as input) all broke after some months (stuck in high).

binary_sensor:
  - platform: rpi_gpio
    ports:
      16: Backdoor Pushbutton
    invert_logic: true
    bouncetime: 4

switch:
  - platform: rpi_gpio
    ports:
      21: Backdoor Lock
    invert_logic: true

With above setup the status of the relays is correct, and do not switch in software at startup, or soft and hard rebooting the pi.

This is because a gpio is high-Z after power-up and initialized with 5V (with invert_logic=true and 0V when false I guess) before being enabled while here, mcp23017 internal state is reused as init value (read by HA but not overwritten with a default value in order to avoid e.g. a light that was on to turn off/default value when HA is restarted).

Issue with a relay that is active low is that it is off when there is no power and turn on when power is available as mcp23017 POR value is 0.

In order to workaround this, I would have to add a new parameter ('init_value') that when present overwrite mcp23017 init_value of an output (rather than read it back/sync to it) before enabling it.

But before looking at this, I would like you to confirm that mcp23017 measured output value and HA state are always consistent.

@megapearl
Copy link
Author

megapearl commented Mar 11, 2022

Okay, removed all relay boards, and connected a multimeter to one of the outputs of the mcp23017, rebooted homeassistant (still with reverse_logic = true), I measure around 4.9v, switches in homeassistant are ON after startup, and I still measure 4.9v, when I switch them off in homeassistant, it's still 4.9v, when I turn them on in homeassistant I'm measuring 0v.
When I repeat this it is always exactly the same for me and consistent.

image

@megapearl
Copy link
Author

megapearl commented Mar 11, 2022

In the same configuration as above When I Power down the pi by pulling the plug, while powered down I measure 0.043v at the mcp23017 output, when booting up the pi I measure 0.210v, when home assistant starts (yes, still with invert_logic=true) I measure 0.000v, the switches in home assistant are OFF, when I switch the output on in homeassistant I still measure 0.000v, when I switch it off again I measure 4.930v, when I switch it on again I measure 0.000v.
When I repeat this it is always exactly the same for me and consistent.

image

The behavier with the relay boards connected is the same, so I'm pretty sure now that they do not draw too much current.

Manual of the PI says:

5v Power
Physical/Board pin 2
The 5v power pins are connected directly to the Pi's power input and will capably provide the full current of your mains adaptor, less that used by the Pi itself.

The pi is powered via POE adapter (48v to 5v)
When Idle (pi and homeassistant started up, all relays OFF)

image

When all relays are switched on:
image

So at 4.9v that is 1.24A total for 5 relays. (included power draw from pi itself)
The 5 relays total draw about 448mA in my case, which should't be a problem.

@jpcornil-git
Copy link
Owner

Thanks for this.

Could you modify line 216 within custom_components/mcp23017/switch.py file

From:

            self._state = self._device.get_pin_value(self._pin_number) 

To:

            self._state = self._device.get_pin_value(self._pin_number) ^ self._invert_logic

@jpcornil-git
Copy link
Owner

My interpretation of your measurements btw:

HA reboot (invert_logic=true)

Startup    ON  5V (NOK, 5V is likely the last value set, HA state should be OFF)
Switch off OFF 5V (OK)
Switch on  ON  0V (OK)

RPi power cycle (invert_logic=true)

before HA starts 0.043/0.210 (OK, gpio are inputs/not driving at that time)
Startup    OFF 0V (NOK, 0V is mcp23017 reset value, HA state should be ON)
Switch on  ON  0V (OK)
Switch off ON  5V (OK)
Switch on  ON  0V (OK)

-> Initial state value is not taking invert_logic into account

@megapearl
Copy link
Author

megapearl commented Mar 11, 2022

Thanks for this.

Could you modify line 216 within custom_components/mcp23017/switch.py file

From:

            self._state = self._device.get_pin_value(self._pin_number) 

To:

            self._state = self._device.get_pin_value(self._pin_number) ^ self._invert_logic

Done, added ^ self._invert_logic to line 216 of switch.py

Rebooted homeassistant, switches work as expected now, the current state is read on startup and display correctly in home assistant.

Power Cycled the pi, the relays stay off until home assistant finished booting, all relays switch on, physically and in home assistant, so status is now read correctly.

Is there any way to prevent switches all going ON at homeassistant startup with 'invert_logic=true' ?

@jpcornil-git
Copy link
Owner

Thanks for the confirmation, I will fix this/make a new release for HACS on sunday.

Wrt initial state, as I said above I could add a new option to initialize state to a fixed value rather than sync/read it back from the device (would stay the default as this allow HA or even RPi restart when mcp23017 is powered independently without affecting current state of connected devices)

@megapearl
Copy link
Author

That would be great, thanks for your fast replies and help troubleshooting this, almost pulled my hair out on this.

@jpcornil-git
Copy link
Owner

As a temp workaround you can already force this by inserting following line before L215 (enabling pin as output) in the same file
self._device.set_pin_value(self._pin_number, True)

jpcornil-git added a commit that referenced this issue Mar 13, 2022
Incorrect initial switch state when invert_logic attribute is true.
@jpcornil-git
Copy link
Owner

Just released v1.0.1 that includes the fix (cfr. commit 6418e90) -> update should be visible in HACS

You will still have to add the above line if you want to force an initial state different from the mcp23017 one (new feature for an upcoming release)

@megapearl
Copy link
Author

Great work! thank you very much :-)

@jpcornil-git
Copy link
Owner

My pleasure/enjoy :-) !

@darmach
Copy link

darmach commented Mar 14, 2022

You will still have to add the above line if you want to force an initial state different from the mcp23017 one (new feature for an upcoming release)

Would you consider adding this as a parameter? I'm using a cheap relay boards (16 relays, optoseparated with low level signal triggering) and I have to use invert_logic of course to workaround the low level trigger, but still upon power loss for example, the moment mcp gets initialized, I get low on all outputs, and all relays trigger.

@jpcornil-git
Copy link
Owner

Yep, see Feature requests, give me a few weeks :-)

@jpcornil-git
Copy link
Owner

jpcornil-git commented May 5, 2022

I found time to implement this today and just checked-in corresponding changes.

As described in the updated example, you can add hw_sync to your yaml configurationto to either synchronize initial value with hardware (true, default) as of today or to a fixed value (false, value=invert_logic). Config Flow scheme is also supported with a new switch option to offer the same capability (selectable per entity rather than per device for a yaml configuration)

-> could you give it a try (if you are using HACS, enable beta version et select "main") and let me know if it works for you as well ?

If OK, I'll tag it in order to make it available to everybody

@megapearl
Copy link
Author

Added hw_sync: false to the switches of mcp component in configuration.yaml, checked beta version and installed commit b056bb7 and is working fine for me, thanks!

image

@jpcornil-git
Copy link
Owner

@megapearl Thanks for the feedback, I just released v1.1.0
@darmach It would be great if you could confirm that it is working for you as well

@darmach
Copy link

darmach commented May 9, 2022

@jpcornil-git Looks fine!

@jpcornil-git
Copy link
Owner

Thanks for the feedback, appreciated !

@cshark87
Copy link

Great job! i'm using them too in my house. So as i read the best way to keep async on, but preventing all relays on after power loss, is to use pullup relays? I am using pull down at this point and have the issue "all on" on first boot...

@jpcornil-git
Copy link
Owner

jpcornil-git commented Nov 11, 2022

There are a few states in a relay system:
A. No power
defined by the Normally Open (NO) Normally Closed (NC) connection selected

B. After power up/before mcp23017 initialization
defined by your board type

C. After mcp23017 initialization
defined by HA/this integration

Ideally you want:

  • A to be a safe state for your application
  • A and B to be the same state (no toggling during power up/before HA is up)
    There should be no current flowing in the relay coil to achieve this
  • C state to be read from mcp23017 to preserve state when HA restart but power supply is not affected
    This asks C to be the MCP23017 default, i.e. 0

Therefore having a relay that has a pull down on its input (=> C=B=0) and no state change between A and B is the best option.

Questions to help you further:

  • When you say "all on" on first boot, do you mean that they all switch on at power up and stay on or that they toggle back to off (when HA is up) ?
  • What relay board are you using ?
  • What is you configuration for hw_sync and invert_logic ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants