Control Mqtt switch from third device

Hi,
I’m still learning and I’m pretty new. I’m working on simple project I want to control relay via Mqtt with wemos d1 device, and everything works fine when I used homeassistant web-server.


But later I decide control my relay from android phone using MQTT Dash apps.
I get only relay states indication to my MQTT Dash it means that I have communication between my phone and MQTT broker, but when I press button on my phone, relay just for one cycle change state but in next cycle relay to return in previous state.

My configurator data:

switch:
  platform: mqtt
  name: "Kitchen"
  state_topic: "homeassistant/switch1"
  command_topic: "homeassistant/switch1/set" #Topic sur lequel on publie l'état de l'interrupteur
  payload_on: "ON"   # A vous de choisir le message envoyé lorsque l'interrupteur est allumé 
  payload_off: "OFF" # et éteint
  state_on: "ON"
  state_off: "OFF"
  optimistic: true   # Mettez à true pour maintenir l'état
  qos: 0
  retain: true
  value_template: '{{ value.x }}'

Maybe someone can help me to solve this problem

Question for you: why do you have optimistic: true?

The optimistic feature is useful for devices that do not report their state. If set to true, Home Assistant does not wait for a reply (state_on or state_off) from the device and assumes the device’s state corresponds to whatever payload you sent (payload_on or payload_off). In other words, Home Assistant artificially sets the state based on the payload it sent.

If you know the device (Wemo D1) reports its state, then there’s no need for the optimistic feature. Set optimistic: false then repeat your test with the MQTT Dash app.

1 Like

Thank you for answer. I have optimistic “true” because I don’t know what does it mean. I change it to false, but my Wemos don’t sent back state report to mqtt broker. I’m not sure how to do it, I tried use this line:

client.publish(“homeassistant/switch1”,“ON”);

but it looks like broker don’t get right message.

What kind of firmware is it using? Tasmota? Espurna? ESP Easy? Or is it custom firmware?

I don’t install any firmware to my wemos. I use just regular wemos d1 mini board and PubSubClient library.

Does the PubSubClient report the device’s state? You set state_topic to homeassistant/switch1 which implies you are expecting the device to report its state. Is state_topic correct?

PubSubClient (client.publish(“homeassistant/switch1”,“ON”)) send message “ON” to homeassistant/switch1 where state_topic is homeassistant/switch1. I checked and State_topic is correct.

Then you do not need to use the optimistic feature.

Set optimistic: false and restart Home Assistant.

Use Home Assistant to turn the device on and off. Confirm it reports the correct state before proceeding to experiment with MQTT Dash.

now optimistic: false when I press toggle button the relay switches ON but toggle button state immediately return to state OFF. However, the relay remains to ON state.

Did you restart Home Assistant after setting optimistic to false? If not, do it now and repeat the tests.

If you did restart Home Assistant, the question now is “What is making Home Assistant set the state back to OFF?”

If I was faced with this question, I would use an MQTT client to independently observe the state topic: homeassistant/switch1 to confirm it never changes. However, if it does change, then something is publishing state-changes to it.

For debugging MQTT issues, I use the following MQTT clients: MQTT.fx and MQTT-Spy. You can also use mosquitto_sub.

This normally occurs when the state message is either not sent, or is not interpreted correctly. Having optimistic: true hid this problem because with that set it does not expect a state message. However, if you are using another MQTT client to control the switch, it is necessary for HA to know the state of the switch.

HA @123 says, you need to investigate the messages actually being sent using another client before you can go further.

Everyone thanks for trying to help. The problem was with this line of code value_template: '{{ value.x }}' in configuration.yaml file. So I don’t know what does it mean, maybe someone can explain it to me, but when I remove this line from code the system behaves as it should.