Phantom MQTT Light turning off?

I’m experiencing random light turning off’s commands on my MQTT light in my bedroom. This is the first MQTT connected light, so I have no doubt it’s something I’ve overlooked.

This is the log from my smarthome bridge log:

info: Subscribing to smartthings/Motion Sensor 1/tamper/set, smartthings/Front Door/acceleration/set, smartthings/Front Door/temperature/set, smartthings/Hall Motion Sensor/temperature/set, smartthings/Motion Sensor 1/temperature/set, smartthings/Bedroom Main Light/level/set, smartthings/Sensy/power/set, smartthings/Hall Motion Sensor/motion/set, smartthings/Motion Sensor 1/motion/set, smartthings/Front Door/threeAxis/set, smartthings/Motion Sensor 1/illuminance/set, smartthings/Sensy/switch/set, smartthings/Bedroom Main Light/switch/set, smartthings/Front Door/contact/set, smartthings/Contacts/notify/set, smartthings/System/notify/set, smartthings/Front Door/battery/set, smartthings/Hall Motion Sensor/battery/set, smartthings/SmartSense Presence/battery/set, smartthings/Motion Sensor 1/battery/set
info: Incoming message from MQTT: smartthings/Bedroom Main Light/level/set = 87
info: Skipping duplicate message from: smartthings/Bedroom Main Light/level/set = 87
info: Incoming message from MQTT: smartthings/Bedroom Main Light/switch/set = off

It appears in all instances it turns the light off after subscribing (re-subscribing).

My light.yaml File reads as:

- platform: mqtt
  name: "Bedroom Main Light"
  state_topic: "smartthings/Bedroom Main Light/switch/state"
  command_topic: "smartthings/Bedroom Main Light/switch/set"
  brightness_state_topic: "smartthings/Bedroom Main Light/level/state"
  brightness_command_topic: "smartthings/Bedroom Main Light/level/set"
  brightness_scale: 101
  payload_on: "on"
  payload_off: "off"
  retain: true
  optimistic: false

Any help with this would be much appreciated

Thanks

Jack

You need to confirm this, but I think you can’t use topic names with spaces. Replace spaces with underscores and reboot.

Problems at this point are normally due to the retain flag. When this is set for a message topic, the broker will send that message whenever a new client subscribes to the topic (or when an old client reconnects).

However, there isn’t really enough in the log there to see if this is actually the case, or if something else is happening.

Thanks for the response -
I have tried this with both retain flag as “True” and “False” and the issue still persists.

This may be really simple, and I apologise in advance if it is but how can I provide more log information?

Many Thanks

Jack

Thanks, j.assuncao - I haven’t tried that yet. I’ll give it a try when I’m back from work and update from there.

Thanks for your suggestion

Jack

If you set the retain flag false after it was true, then you need to clear the broker’s database of the message, otherwise it will still keep sending the last message sent with the retain flag. You do this by sending a retained message with a null payload to the topic. Using mosquitto_pub you can do it with

mosquitto_pub -t "smartthings/Bedroom Main Light/level/set" -r -n

You may need hostname, username and password parameters if that’s the way it is set up.

However, whether you need to do that depends on how you want the system to operate. Will the smarthings switch the light independent of HA, or is HA always in charge?

If HA is always going to be correct, then you probably want the retain flag set, so that when it reconnects it gets the last message sent by HA.

If smartthings is going to be able to turn the light independent of HA, then you need to turn retain off and clear the broker’s database.

@gpbenton I think this is the solution I’ve been looking for. By any chance, do you know if this can be done on hass.io without switching to debugging mode?

I have already suggested something in your other thread. Hassio Smartthings Mqtt