stjohnjohnson / smartthings-mqtt-bridge

Bridge between SmartThings and MQTT
https://hub.docker.com/r/stjohnjohnson/smartthings-mqtt-bridge/
MIT License
363 stars 241 forks source link

MQTT API Question #95

Closed mschwartz closed 7 years ago

mschwartz commented 7 years ago

It seems to me the MQTT API is a bit confounding and might be done better.

Right now, you get something like

smartthings/Light/switch on

And you send the same thing to turn that switch on.

The issue is you will then receive the message you sent because you're subscribed/istening.

My suggestion is to have incoming messages something like:

smartthings/status/Light/switch on

For incoming messages

And you send:

smartthings/set/Light/switch on

To control the device.

This way, you won't get the "set" message that you sent.

This suggestion allows you to listen on

smartthings/status/#

And you only get true status messages

stjohnjohnson commented 7 years ago

Yup! We do have that feature. It's turned off by default for backwards compatibility, but you just need to set the command_suffix and state_suffix: https://github.com/stjohnjohnson/smartthings-mqtt-bridge#configuration

mschwartz commented 7 years ago

Awesome. I'm all over that!

mschwartz commented 7 years ago

This project inspired me:

https://github.com/autodomo

I need to change the name, though.

mschwartz commented 7 years ago

I changed the name to RoboDomo.

So the url is now: https://github.com/robodomo

Thanks for your work!

mschwartz commented 7 years ago

Sorry, the feature isn't as I want :(

smartthings/Kitchen Lights/switch/status off

Isn't want I want

I want:

smartthings/Kitchen Light/status/switch off

parsing is trivial:

const THING = 'Kitchen Lights'
const TOPIC = `smartthings/${THING}/status/'
MQTT.subscribe(TOPIC + '#')
MQTT.onMessage((topic, message) => {
    const thing = topic.substr(TOPIC.length) // =-> switch, level, whatever
    ...
})
mschwartz commented 7 years ago
    componentDidMount() {
        MQTT.subscribe(this.status_topic + 'switch', this.onStateChange)
        MQTT.subscribe(this.status_topic + 'level', this.onStateChange)
    }
mschwartz commented 7 years ago
    onStateChange(topic, newState) {
        const newValue = {},
              key     = topic.substr(this.status_topic.length)

        newValue[key] = newState
        this.setState(newValue)
    }
mschwartz commented 7 years ago

I figured out a trick. I'm hoping all this helps someone else who stumbles onto this ticket.

no state_suffix, but use a command_prefix

That JS code above that I posted works fine without the /state/ present in the topic.

To send, just MQTT.publish(this.set_topic + 'switch/set', 'on')

Not too ugly :)

stjohnjohnson commented 7 years ago

Glad you found a workaround!