Physical switch does not update my MQTT topic. Trying to connect openHAB & Home Assistant using MQTT

Well, even with the “simple” approach you need to understand why the infinite loop happened. As explained in Marketplace MQTT Event Bus, you need to be careful to avoid a situation where a command is published which generates an update which generates a command and so on.

One way to avoid this is the device that owns the device only publishes updates and only subscribes to commands.

  • If HA publishes an update for some reason (it shouldn’t since it doesn’t own the device, it can’t know what state it’s in) OH will ignore it.
  • If HA publishes a command, OH will receive it, command the corresponding Item which goes to the device and eventually the Item get’s updated with a new state that OH publishes to the state topic. HA receives that state update and changes it’s state for that Item.
  • If the device changes state outside of either OH or HA, OH gets that update, publishes to the state topic, and HA updates.

So OH, as the owner, only publishes updates. HA as the partner only publishes commands, never state updates.
OH, as the owner, only subscribes to commands, HA as the partner only subscribes for updates, never commands.

That prevents the infinite loop.

I’m not pushing OH 4. Just pointing out that using the event bus is a little easier in OH 4 as I was able to merge the three separate rule templates into one. But the behavior is exactly the same.