MQTT Broker Configuration Help

Hi I am running HASSOS on two different RPI3B, one is my production env and other one is development. Dev server running latest version 108.2 while prod is running version 101.3. I have below config. using a MQTT broker running on dev server and trying to monitor production HA status. I created a sensor on development server but getting status ‘Unknown’. Could someone guide and advise what is missing in bellows setup:

It might be as simple as you’re publishing under hassioprod but you’re looking for the topic hassosprod.

Also, I’m not sure if you have to put your payload string into quotes - I guess single or double, both should work.

And last but not least, it would be good to check, e.g. with MQTT Explorer, if the status message is actually published.

1 Like

I moved sensor entry from sensors.yaml to binary_sensors.yaml with the consideration that status can only be online and offline. Added payload_available and payload_not_available with online/offline values to automation.yaml but immediately prod server started throwing below error in home-assistant.log:
020-04-11 10:16:50 ERROR (MainThread) [homeassistant.components.automation] Publish Service Status: Error executing script. Invalid data for call_service at pos 1: extra keys not allowed @ data['payload_available']

I restored original configuration back. I can also see the below log entry appearing in MQTT tab in UI on my dev server:

Thanks for your prompt reply.

Dev server is receiving subscription every 5 sec but somehow sensor is not correctly getting value. When I issued sub command on dev server got below output:

I also forced offline message from publisher command prompt and is the same is also visible in above screen shot. Below is screenshot when I issued forced offline message from publisher on prod server:

Something not working with sensor the value on dev server for sensor.hassosprod is still ‘unknown’. I added the value_template in sensor definition to get value “Online” as below:

#sensors.yaml
  - platform: mqtt
    name: 'hassosprod'
    icon: mdi:wifi
    state_topic: 'hassosprod/status'
    value_template: '{{ value }}'
    payload_available: "Online"
    payload_not_available: "Offline"

I also changed “online” to “Online” and “offline” to “Offline” in all places assuming it may require InitCap but no luck sensor.hassosprod still shows unknown.

You likely should be using an mqtt binary sensor. You likely should not be using payload_available / payload_unavailable but instead use payload_on and payload_off.

My publisher is publishing message as below:

#automations.yaml
- id: Publish_Service_Status
  alias: 'Publish Service Status'
  initial_state: 'on'
  trigger:
    platform: time_pattern
    seconds: '/5'      
  action:
  - service: mqtt.publish
    data:
      topic: 'hassosprod/status'
      payload: "Online"

and subscriber binary sensor entry is as below:

#binary_sensors.yaml
  - platform: mqtt
    name: 'hassosprod'
    state_topic: 'hassosprod/status'
    value_template: '{{ value }}'
    payload_available: "Online"
    payload_not_available: "Offline"
    device_class: "connectivity"
    qos: 0

Currently the status for binary_sensor is “off”. In order to use payload_on/off, do I need to change publisher message to “ON” because transmitted payload is also “Online”? I am not able to understand why the value ‘Online’ is not getting assigned to binary sensor?
Thanks for your reply.

Again, don’t use payload_online and payload_offline. You should use payload_on and payload_off.

In the context of the template binary sensor, payload_online means something different.

Change your publisher to send “on”. Change your binary sensor to have:

payload_in: 'on'
payload_off: 'off'

(These might actually be the defaults, but it will be good to put in anyway.)

HOWEVER - you don’t have (or haven’t listed) a mechanism for the mqtt topic to ever have an ‘off’ state. So your binary sensor will always be ON. To work around this, on production system’s MQTT config, I’d add the directives to have a LWT (last will & testament) topic. Your binary sensor should then watch this. When production HA starts, a message will be posted to that topic. When production HA’s MQTT connection dies (presumably because it is offline), the LWT message will be posted which the binary sensor can detect.

A binary_sensor only has two states : ‘on’ and ‘off’. You can’t assign ‘Online’ and ‘Offline’ to a binary_sensor.

Still no luck, binary sensor still shows “off” state means not receiving the “on” value.

My yaml files now looks as below:

#automations.yaml on publisher
- id: Publish_Service_Status
  alias: 'Publish Service Status'
  initial_state: 'on'
  trigger:
    platform: time_pattern
    seconds: '/5'      
  action:
  - service: mqtt.publish
    data:
      topic: 'hassosprod/status'
      payload: "on"

Subscribe side:

#binary_sensors.yaml on Subscriber
  - platform: mqtt
    name: 'hassosprod'
    state_topic: 'hassosprod/status'
    value_template: '{{ value }}'
    payload_on: "on"
    payload_off: "off"
    qos: 0

Is the template above correct?
Subscriber side I can see a message being received every 5 sec:

If you modify the publisher :

#automations.yaml on publisher
- id: Publish_Service_Status
  alias: 'Publish Service Status'
  initial_state: 'on'
  trigger:
    platform: time_pattern
    seconds: '/5'      
  action:
  - service: mqtt.publish
    data:
      topic: 'hassosprod/status'
      payload: "ON"

Then the subscriber can simply be :

  - platform: mqtt
    name: 'hassosprod'
    state_topic: 'hassosprod/status'
    off_delay: 5

I changed publisher message to “ON”, subscriber binary sensor entry to as described above by removing payload_on/off, value_template and added off_delay tag but outcome still remains the same. What does off_delay option do?

What I am not able to understand, how a received “ON” value gets assigned to binary sensor state? Thanks for your prompt reply.

You always test with the terminal add-on, maybe you should go to developer tools ->mqtt, enter # and start listening. I think the problem will show there.

Nothing happens when I tried to listen to ‘hassosprod/#’, ‘hassosprod/status’, ‘#’ and also tried all 3 strings without single quotation marks from MQTT add-on Listen to a Topic but there was no response it just kept waiting however response is there when I listen the same from SSH subscriber command.

I also added back payload_on/off options to binary_sensor configuration and no value_template but still no success.

Thanks Francis for quick response.

That is the problem. HA is not listening to your broker, so whatever you try with your sensor will not work until you get that fixed.

I found the problem, it was with MQTT integration and I set the information (IP/user/password) about the different broker (running on prod server) instead of the local broker running. Moment I removed integration and added back with correct information, the status for binary_sensor came “on”.

#automations.yaml on publisher

- id: Publish_Service_Status
  alias: 'Publish Service Status'
  initial_state: 'on'
  trigger:
    platform: time_pattern
    seconds: '/5'      
  action:
  - service: mqtt.publish
    data:
      topic: 'hassosprod/status'
      payload: "ON"

#binary_sensor.yaml on subscriber
  - platform: mqtt
    name: 'hassosprod'
    state_topic: 'hassosprod/status'
#    value_template: '{{ value }}'
    payload_on: "ON"
    payload_off: "OFF"
    off_delay: 6

I intentionally set sensor off delay to 6 as the data is coming every 5 sec.

I made the MQTT integration error because I have been trying to monitor my prod server from dev and dev server from prod. For that I need to integrate two MQTT instances one running on local and other one on a remote server but current integration allows only one MQTT instance to be configured on one homeassistant host.

Thanks everyone my current issue is resolved. and I need to look for different solution for the other challenge.