About mqtt in hass

My dev-environment is ubuntu16.04, mosquitto version is 1.4.14.

I coded a demo which can publish a mqtt message when I click the button.

When I started hass first then started mosquitto, terminal showed me that “ERROR: Address already in use”, and couldn’t get the message which is published by my demo.

When I started mosquitto first then started hass, hass showed me the “mqtt.binary_sensor” component in configuration.yaml could not be set up ( invalid config ), and I do get the message which is published by my demo by mosquitto.

But I did succeed last year: a python file in raspberry pi which could publish current temperature by mqtt, then I add a “mqtt.binary_sensor” component in configuration.yaml and started mosquitto in ubuntu machine, and I did see hass can get the temperature data.

So my question is:

Does hass have a build-in mqtt client? I found port 1883 is used whatever start hass or mosquitto.

How could I use mqtt in hass? I did add mqtt in configuration but it didn’t work.

If you add only mqtt: in your configuration, the internal mqtt broker get’s started in hass.
You have to provide the broker parameter to use an external brocker.

Hass have a build in server, not only a client.

Either remove mqtt: in the config or configure your mqtt server under mqtt: to disable the build-in server.

But you’re right to use mosquitto, I would avoid the build-in server, it’s not one of the best…

binary_sensor:
  - platform: mqtt
    state_topic: "/garage/csb"
    name: "hcq-csb"
    device_class: occupancy
    payload_on: "true"
    payload_off: "false"
    value_template: "{{ value }}"

Maybe my expression is not clear. This is my mqtt config in configuration.yaml and I don’t have a
mqtt: config.So should I add a broker parameter (a machine which start mosquitto ) in this config?

binary_sensor:
      - platform: mqtt
        state_topic: "/garage/csb"
        name: "hcq-csb"
        device_class: occupancy
        payload_on: "true"
        payload_off: "false"
        value_template: "{{ value }}"

Maybe my expression is not clear. This is my mqtt config in configuration.yaml and I don’t have a
mqtt: config. What should I modify if I do not want to use the build-in server?

# Example configuration.yaml entry
mqtt:
  broker: 192.168.1.100
binary_sensor:
      - platform: mqtt
        state_topic: "/garage/csb"
        name: "hcq-csb"
        device_class: occupancy
        payload_on: "true"
        payload_off: "false"

remove value_template

I modified configuration.yaml as your advice and hass did get the message I sent. It said “no match payload” but I did define payload_on and payload_off and I did return “true” and “false” to hass. Where is wrong?

Did you use double quote for your true, false in configuration.yml? The binary_sensor/mqtt is pretty straightforward, the only problem I can think about is your self._payload_on (loaded from configuration.yml) was parsed as boolean instead of string, or maybe the different is between true and True?

            if self._template is not None:
                payload = self._template.async_render_with_possible_json_value(
                    payload)
            if payload == self._payload_on:
                self._state = True
            elif payload == self._payload_off:
                self._state = False
            else:  # Payload is not for this entity
                _LOGGER.warning('No matching payload found'
                                ' for entity: %s with state_topic: %s',
                                self._name, self._state_topic)
                return