ebaauw / homebridge-rpi

Homebridge plugin for Raspberry Pi.
Apache License 2.0
308 stars 18 forks source link

Switches default to On after Pi reboot #54

Closed mikebannan closed 4 years ago

mikebannan commented 4 years ago

I've configured 2 Switch Devices (Up and Down) to close relays to buttons on a motor controller. These buttons should not be active at the same time.

On a Pi reboot (every time) and on HomeBridge restart (roughly half the time), both RPi Switches set themselves to On. (Their state at reboot/restart was Off.)

The On state is set regardless of the Reversed key (true/false).

My preference would be to have a configuration key to set the Default state of the Switch.

Additional Info: I tried solving this with HomeKit automations to turn Off the Down Switch when the Up Switch is On, and visa versa. But these automations are not activated by the On state following reboot/restart. The automations are correctly activated when I turn on either Switch in the Home app.

Log entries: [8/19/2020, 4:20:09 PM] [RPi] Initializing RPi platform... [8/19/2020, 4:20:09 PM] [RPi] homebridge-rpi v1.1.13, node 12.18.3, homebridge v1.1.2, homebridge-lib v4.7.14 [8/19/2020, 4:20:09 PM] Loading 2 accessories... [8/19/2020, 4:20:09 PM] [RPi] restored 3 accessories from cache [8/19/2020, 4:20:11 PM] [RPi] 192.168.1.17: connected to 192.168.1.17:8888 [8/19/2020, 4:20:11 PM] [RPi] 192.168.1.17: Raspberry Pi 4B v1.2 (BCM2711, 2GB) - 1000000035098276 [8/19/2020, 4:20:11 PM] [RPi] 192.168.1.17: localhost [8/19/2020, 4:20:11 PM] [RPi] 192.168.1.17 TV Up: set On from false to true [8/19/2020, 4:20:11 PM] [RPi] 192.168.1.17 TV Down: set On from false to true

Configuration: `

    {
        "name": "RPi",
        "hosts": [
            {
                "host": "192.168.1.17:8888",
                "devices": [
                    {
                        "device": "switch",
                        "name": "TV Up",
                        "gpio": 17,
                        "reversed": true
                    },
                    {
                        "device": "switch",
                        "name": "TV Down",
                        "gpio": 18,
                        "reversed": true
                    }
                ]
            }
        ],
        "platform": "RPi"
    }

`

ebaauw commented 4 years ago

I've configured 2 Switch Devices (Up and Down) to close relays to buttons on a motor controller. These buttons should not be active at the same time.

I don't think I can enforce that through Homebridge RPi. And even if I could, other programmes interacting with the GPIO pins would still be able to bypass it. I would suggest to wire the relays in series: the first relay to provide power to the second relay; the second relay to switch between the Up and Down devices. You might want to check out my experiments to control the speed of my ceiling fans, see https://github.com/ebaauw/ceiling-fan.

On a Pi reboot (every time) and on HomeBridge restart (roughly half the time), both RPi Switches set themselves to On. (Their state at reboot/restart was Off.)

That's very odd.

On boot, the Raspberry Pi resets the GPIO pins to low (which would map to On in HomeKit, when reserved is set). There's nothing Homebridge RPi can do to change that, but you can change this behaviour in /boot/config.txt, see https://www.raspberrypi.org/documentation/configuration/config-txt/gpio.md.

On startup, or rather on (re-) connecting to the pigpiod server, Homebridge RPi initialises the GPIO mode (input/output), but this does not change the GPIO output state. See also #32.

The On state is set regardless of the Reversed key (true/false).

Not sure what state you're referring to here: the GPIO state or the state of the HomeKit On characteristic? reversed only influences how the GPIO state (low, high) is translated to the HomeKit On characteristic and vice versa; it doesn't change the GPIO logic.

mikebannan commented 4 years ago

In the end, I actually just want the switch to default to Off.

Something in Homebridge is setting the Switch state to true. I've confirmed that this only occurs after Homebridge is started.

I tried a workaround by creating a boot service that runs before Homebridge starts and uses sed on the cachedAccessories file to replace the "on":true state for the switches to "on":false. The service works, and I can compare the state of the cachedAccessories pre- and post- Homebridge start: Pre-start, cachedAccessories has "on":false, and post-start it changes to "on":true. So Homebridge is getting the "on":true from somewhere, and then updating cachedAccessories.

It consistently happens with every reboot of the Pi. Confusingly, it does not happen with the restart of the Homebridge service itself.

ebaauw commented 4 years ago

Confusingly, it does not happen with the restart of the Homebridge service itself.

Homebridge doesn’t know if it’s started during boot, or simply restarted.

So Homebridge is getting the "on":true from somewhere, and then updating cachedAccessories.

Yes, from the actual GPIO state, which is reset to low when you boot the Pi. Because you’ve set reversed, this corresponds to on.

Note that cachedAccessories isn’t updated in realtime with every state change.

mikebannan commented 4 years ago

OK. I've unchecked the Reversed box. The initial Homebridge restart flipped the switches to On, but every restart and reboot thereafter the switches have remained Off, as expected.

(I don't recall how I thought Reversed solved an earlier problem I had. I somehow believed that the setting worked the opposite of what I'd expect. But things work exactly how I'd like them now. Thanks for your patience.)