Switch slides to OFF after a few seconds?

I am currently using Node red, but others have convinced me to give Home Assistant a try.
I added mqtt and switches to my configuration.yaml file, and I can turn the lights on by clicking on the switch slider. However, a couple of seconds later, the slider goes off, yet the light remains on. I can’t turn the light off from Home Assistant, but I can from Node Red.

Any tips on where to look would be appreciated.

Here’s part of my configuration.yaml:

#MQTT
mqtt:
  broker: 192.168.1.124
  discovery: true
  discovery_prefix: homeassistant
  
switch: !include switches.yaml

And, here is my switches.yaml file:

- platform: mqtt
  name: "Room Node"
  state_topic: "room/light/status"
  command_topic: "room/light"
  payload_on: "on"
  payload_off: "off"
  optimistic: false
  qos: 0
- platform: mqtt
  name: "Studio Light"
  state_topic: "studio/light/status"
  command_topic: "studio/light"
  payload_on: "on"
  payload_off: "off"
  optimistic: false
  qos: 0

This is normally because HA is not receiving the state_topic message to acknowledge that the light has changed. It assumes it hasn’t so puts this switch back to its original state.

Check that the state_topic: and payload match the status being received after the light is turned on. And that HA is still connected to the broker and subscribed to all topics.

Thanks. Makes sense, but I am monitoring the mqtt traffic and my state_topic payload is being sent by the lights.

Here’s the code in my light node:
#define NODENAME “room”
const char *cmdTopic = NODENAME “/light”;
const char *statusTopic = NODENAME “/light/status”;

I have MQTTLens (please tell me if you have a preferred MQTT monitor program- I am not thrilled with MQTTLens). Anyway, MQTTLens shows topic: ‘room/light/status’ has a payload of “on” as soon as the light is turned on.

Could I be missing something in my .yaml file?
- platform: mqtt
name: “Room Node”
state_topic: “room/light/status”
command_topic: “room/light”
payload_on: “on”
payload_off: “off”
optimistic: false
qos: 0

I normally use mosquitto_sub, but I don’t think it does anything different.

I can’t see anything wrong with your configuration. The only thing I can think of trying is to remove the quotes from the payload. The documentation has them in, but my configuration doesn’t, and that seems to work fine - so

 payload_on: on
 payload_off: off

If not, you can turn on logging to debug in HA, to get information in the logs about what is happening to the message (if it is being received)

logger:
  default: warning
  logs:
      homeassistant.components.mqtt: debug

You might also subscribe to $SYS/broker/publish/# as this contains data about the broker. If the broker isn’t sending messages out, then it doesn’t think that HA has subscribed.

1 Like

thanks @gpbenton.
its an old topic but you helped me figure out what i did wrong :wink:

Hello need some help here …

i think my HA is not subscribed to any topic…
how can i make it talk to MQTT

Start a new thread.
Ask specific questions.
Show what you have tried.
Start a new thread.

1 Like

thnaks i manged

Well, how did you fix it- for the benefit of others who may search the forums for a similar issue.

1 Like

i think where i took the sonoff tmpalte was not good , i took somthing from this thread and it worked