MQTT Switch examples - to help me figure out what I'm doing wrong

I have a Pimoroni Automation Hat on a Pi, not my home assistant machine. Writing Python to get it to things is quite simple. I’ve been trying to integrate it in to HA this weekend using MQTT but I’ve had no luck. I’m not even getting past the first hurdle.

I’ve set up this switch

switch:
  - platform: mqtt
    unique_id: shed_outside
    name: "Shed Switch"
    state_topic: "shed/light/outside"
    command_topic: "shed/light/outside/set"
    payload_on: "ON"
    payload_off: "OFF"
    state_on: "ON"
    state_off: "OFF"
    optimistic: false
    qos: 0
    retain: true

And I’ve written code on the Automation Hat which subscribes to those topics. If I manually publish shed/light/outside/set with a payload of “ON” the light goes on. If I click the switch in the HA GUI, nothing happens. In fact, if I subscribe to shed/# there’s nothing published when I toggle the switch. So I think I’m missing something in the switch config but after wasting too much of my weekend on it I thought I’d ask if anyone has a working example I can look at.

Thanks

Steve

Except for the

retain: true

I don’t see anything wrong with your code. Retain: true causes the command to be retained, which can have undesired side effects.

Do you publish that from HA or an external program?

Either. I know my MQTT server is working as there’s a load of other services using it fine. It seems to be something about this switch:

So as far as you can tell, if I turn the switch on and off HAss should publish ON/OFF to shed/light/outside/set - which is what I would expect.