Problem with rpi gpio binary sensor

I’m trying to use a rpi gpio pin and a 120v relay to monitor the AC power in the house (the rpi, router, and cable modem are behind a ups, so it should be able to remain up to notify if the mains power goes out). I’m using half of a dpdt relay and the pin is high when the power is off, and low when the power is on

It is working as expected, except when I restart Home Assistant. The sensor always seems to show “off” after a restart, until I cycle the power (unplug and plug the relay back in) which is not ideal.

Is there some kind of initialization that I need to do. Or is gpio 21 (pin 40) some kind of special pin?

  - platform: rpi_gpio
      21: AC Power

The implementation of the GPIO binary sensor on the Pi is absolute rubbish. There are many reports of it not responding to correct state changes and more than one issue open that have never been addressed. GPIO outputs (switches) are fine.

I gave up and used a separate Pi running an mqtt client to monitor my wired inputs.