-
Notifications
You must be signed in to change notification settings - Fork 6
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
Comments
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, |
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. For now I created an automation to get the expected behavier, but maybe you have a better idea.
|
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 |
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. |
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. |
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. |
To summarize/confirm my understanding:
- when there is no power, relay is off (and lights/locks as well as they
are connected to NO)
- when pi is powered up, nothing changes as mcp23017 outputs are all high-Z
and don't pull relay input low
- when HA starts up, it enables mcp23017 outputs and set output level to
0/device reset value -> relay is on
- 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: true'
Yes
Yes
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.
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. |
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 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. |
Haven't tried a separate power supply for the relay board yet, will try that tomorrow. |
You could also try without the relay board (to remove its potential influence) and measure mcp23017 output/check against HA state. |
... 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. |
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. |
I expected the same behavier as the GPIO component.
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. |
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. |
Did you measured mcp23017 output in that case/without relay board (HA should report 'on' while mcp23017 output should be 0V) ? |
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. |
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. |
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:
|
My interpretation of your measurements btw: HA reboot (invert_logic=true)
RPi power cycle (invert_logic=true)
-> Initial state value is not taking invert_logic into account |
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' ? |
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) |
That would be great, thanks for your fast replies and help troubleshooting this, almost pulled my hair out on this. |
As a temp workaround you can already force this by inserting following line before L215 (enabling pin as output) in the same file |
Incorrect initial switch state when invert_logic attribute is true.
Great work! thank you very much :-) |
My pleasure/enjoy :-) ! |
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. |
Yep, see Feature requests, give me a few weeks :-) |
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 |
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! |
@megapearl Thanks for the feedback, I just released v1.1.0 |
@jpcornil-git Looks fine! |
Thanks for the feedback, appreciated ! |
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... |
There are a few states in a relay system: B. After power up/before mcp23017 initialization C. After mcp23017 initialization Ideally you want:
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:
|
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:
How to troubleshoot?
The text was updated successfully, but these errors were encountered: