Shelly 4PRO (stock firmware) - switch status via mqtt

I am trying to use some of the relays in a Shelly 4PRO in “detached mode”, i.e., to use the GPIO switches as sensors whose status is not related with that of the relays.

I can control all relays pretty easily via mqtt, but haven’t figured out how to get switch status yet.
My tentative sensor setup is:

  - platform: mqtt
    name: "Shelly4PRO Switch1 Status"
    state_topic: "shellies/shelly4pro-xxxxxx/input/0"
    availability_topic: "shellies/shelly4pro-xxxxxx/online"    

As a result, I get the shelly switch status sensor as always “unavailable”.
Has anyone tried to do this yet?

Thanks!

This means the availability topic hasn’t been sent. The availability topic is optional in HA, so you can just leave it out.

Thanks @gpbenton - I’ve removed the availability topic and now I can get the switch status.
However, the sensor now shows “Unknown” value since HA is started and until the first switch status change.

Is there a way HA can poll the initial switch status too?

There are actually two possible ways, either or both may work depending on the capabilities of the shelly, of which I know nothing.

  1. Tell the Shelly to send the state message with the retain flag set. This causes the MQTT broker to save the message and send it to any client that subscribes to that topic, such as when HA starts up.
  2. Configure a script that runs when HA starts up to send an mqtt message to the shelly to send its status. I’m afraid I don’t know what that command might be.

I want to get one of these. How did you integrate it into Home Assistant?