Problem controlling a MQTT switch from REST interface

I have decentralized pool water control system, where a pyhton script executes on a headless Rpi to control water filling in my pool. Since I want to monitor water filling and also have the possibility to manually fill water from my HA (running on a separate Rpi), the actual I/O port controlling the water tap, is controlled by my HA over MQTT. I have mqtt_io running on my detached Rpi controlling various GPIO pins on the remote Rpi. By this I can montitor and control I/O on the detached Rpi from HA. This works great.

The detached python scripts implements the control loop measuring the water level, and once it detects low water it decides to start filling water. The script issues a POST request on the REST interface that updates corresponding HA entity, and this should result in a MQTT command that turns on the water tap. However, it seems like the POST request just updates the HA entity. No MQTT command is sent out to actually to control the switch on the remote Rpi.

I can clearly see this behaviour by manually turning on the water in HA. That operation immediatly sends a MQTT command that turns on the water. My python script monitors the status of the switch by a GET request, understands the water tap is on and immediatly decides to turn it off since the water level is high enough. Consequently, the switch on my lovelace card turns off within a couple of seconds. However, in this case no MQTT off command is sent to my remote Rpi. And by this the water tap is kept open…

So it looks a POST request targeting a MQTT entity does not trigger corresponding MQTT command. But the entity state is updated in HA. In my opinion, this looks like a bug or at least a very strange behaviour.

Edit: You might wonder why I have implemented the control loop in a separate python script. This is for historic reasons. I developed this as a standalone system. It includes some advanced filtering and security mechanisms, and I don’t want to reimplement this again in HA. Just use HA as a frontend for the script.

I assume you do a POST /api/states/<entity_id> ?

You should use the entity service, like you would do in an automation: POST /api/services/<domain>/<service>, e.g./api/services/switch/turn_on

That’s how this is done. And it works fine to update the HA identity. The problem the associated MQTT device/entity is not updated when the update is made through the REST interface by a POST request. If I do the update from the GUI, everything works as expected.

But I will take a grip on this by moving the water control to HA. I’ve learned there is support for filtered sensors, so it will not be that hard.

Edit: I was wrong. I used payload = {state: ‘on’} in the POST request. Will try your proposal. Anyway it is strange the entity in HA is updated, but not the device.

You’re using POST /api/services/switch/turn_on?
Your “HA entity” is a MQTT switch, right?

I made a change and added turnOn() and turnOff() methods using the services interface. And now it works! Now I understand the difference between just changing entity state and actually impacting the device. Thanks!