Configuring an MQTT topic as a temperature sensor

Hi,

I am publishing temperature from a Raspberry Pi via MQTT, and hoping to use it to switch automations.

I’ve installed the Mosquitto broker in Home Assistant, and my attempts to publish to it seem to work:

$ ./publish-temperature.py 
Publishing to MQTT topic: home/cabinet
Message: {"temperature": "45.5"}

I’ve described the sensor in sensors.yaml:

- platform: mqtt
  name: "Cabinet Temperature"
  state_topic: "home/cabinet"
  unit_of_measurement: '°C'
  value_template: "{{ value_json.temperature }}"  

… and linked it in configuration.yaml:

sensor: !include sensors.yaml

The sensor appears, but never seems to be populated with data:

sensor

I have a suspicion I’m missing something obvious here but for the life of me I can’t figure out what it is. Anyone have any suggestions?

That all looks ok to me. Are you sure you are publishing to the topic?

Have you inspected the broker with something like MQTT Explorer?

@tom_l Pretty sure.

If I publish manually from my laptop using the MQTT CLI tool:

$ mqtt pub --topic home/cabinet --message '{"temperature": 42.42}' --host homeassistant.local --verbose
Client '[email protected]' sending CONNECT MqttConnect{keepAlive=60, cleanStart=true, sessionExpiryInterval=0}
Client '[email protected]' received CONNACK MqttConnAck{reasonCode=SUCCESS, sessionPresent=false, assignedClientIdentifier=auto-03AEAB20-D744-BE30-8C15-F0C596FBA2AC, restrictions=MqttConnAckRestrictions{receiveMaximum=65535, maximumPacketSize=268435460, topicAliasMaximum=10, maximumQos=EXACTLY_ONCE, retainAvailable=true, wildcardSubscriptionAvailable=true, sharedSubscriptionAvailable=true, subscriptionIdentifiersAvailable=true}} 
Client '[email protected]' sending PUBLISH ('{"temperature": 42.42}') MqttPublish{topic=home/cabinet, payload=22byte, qos=AT_MOST_ONCE, retain=false}
Client '[email protected]' received PUBLISH acknowledgement MqttPublishResult{publish=MqttPublish{topic=home/cabinet, payload=22byte, qos=AT_MOST_ONCE, retain=false}}

Weirdly, though, MQTT Explorer is drawing a blank:

I’ve also tried giving up on JSON altogether, and just posting raw numerical values. With this sensor configuration:

- platform: mqtt
  name: "Cabinet Temperature"
  state_topic: "home/cabinet/temperature"
  unit_of_measurement: '°C'

… and posting raw numbers:

$  mqtt pub --topic home/cabinet/temperature --message 42.42 --host homeassistant.local --verbose
Client '[email protected]' sending CONNECT MqttConnect{keepAlive=60, cleanStart=true, sessionExpiryInterval=0}
Client '[email protected]' received CONNACK MqttConnAck{reasonCode=SUCCESS, sessionPresent=false, assignedClientIdentifier=auto-043109D1-E75D-AA7C-748C-9F0BE28CAE78, restrictions=MqttConnAckRestrictions{receiveMaximum=65535, maximumPacketSize=268435460, topicAliasMaximum=10, maximumQos=EXACTLY_ONCE, retainAvailable=true, wildcardSubscriptionAvailable=true, sharedSubscriptionAvailable=true, subscriptionIdentifiersAvailable=true}} 
Client '[email protected]' sending PUBLISH ('42.42') MqttPublish{topic=home/cabinet/temperature, payload=5byte, qos=AT_MOST_ONCE, retain=false}
Client '[email protected]' received PUBLISH acknowledgement MqttPublishResult{publish=MqttPublish{topic=home/cabinet/temperature, payload=5byte, qos=AT_MOST_ONCE, retain=false}}

… I get the same problem. Nothing in MQTT Explorer, and nothing in my sensor trace.

Check the broker log, looks like you might have an authentication issue preventing messages being published to the topic.

I was wondering the same thing, but there’s nothing obvious in the log:

[21:18:06] INFO: Setup mosquitto configuration
[21:18:06] WARNING: SSL not enabled - No valid certs found!
[21:18:06] INFO: No local user available
[21:18:06] INFO: Initialize Hass.io Add-on services
[21:18:06] INFO: Initialize Home Assistant discovery
[21:18:07] INFO: Start Mosquitto daemon
1609755487: Loading config file /share/mosquitto/acl.conf
1609755487: mosquitto version 1.6.3 starting
1609755487: Config loaded from /etc/mosquitto.conf.
1609755487: Loading plugin: /usr/share/mosquitto/auth-plug.so
1609755487: |-- *** auth-plug: startup
1609755487:  ├── Username/password checking enabled.
1609755487:  ├── TLS-PSK checking enabled.
1609755487:  └── Extended authentication not enabled.
1609755487: Opening ipv4 listen socket on port 1883.
1609755487: Opening ipv6 listen socket on port 1883.
1609755487: Opening websockets listen socket on port 1884.
1609755487: Warning: Mosquitto should not be run as root/administrator.
1609755520: New connection from 172.30.32.1 on port 1883.
[INFO] found homeassistant on local database
1609755521: New client connected from 172.30.32.1 as 2N9pxioubQmQyUG4nsPAEK (p2, c1, k60, u'homeassistant').
1609755585: New connection from 192.168.1.102 on port 1883.
1609755585: New client connected from 192.168.1.102 as auto-D5CAC399-B2F5-F00B-E039-17A31CF39F3E (p5, c1, k60).
1609755585: Client auto-D5CAC399-B2F5-F00B-E039-17A31CF39F3E disconnected.

Also, I’ve followed the instructions and configured read and write for all topics for anonymous users:

acl

Ah! Now it’s showing up in MQTT Explorer.

Still nothing in Home Assistant, though.

Any chance you have more than one MQTT server running? I see a number of references to ‘localhost’. You might try hard coding the IP address of MQTT server to a real IP address.

Good idea @dproffer but I’m afraid the problem persists.

$ ./publish-temperature.py Publishing to MQTT topic: home/cabinet Message: {"temperature": "45.5"}

Have you tried sending your value as a float instead of a string? Remove the quotations mark around the value and see if HA picks it up as a number it can plot.

@schonke Yup, tried both strings and floats. All my most recent attempts via the MQTT CLI tool were as floats, and they graph as expected in MQTT Explorer.

So to recap, as I’ve tried a few different things at this point :slight_smile:

  1. I can publish raw numbers (not JSON) directly to the topic home/cabinet/temperature using the MQTT CLI tool.
  2. The broker is the Mosquitto MQTT broker, installed as a Home Assistant add-on.
  3. I can see the raw numbers, graphed, in the MQTT Explorer, connected to Mosquitto.
  4. I can see the entity (a sensor) in Home Assistant.
  5. But the graph of the sensor values is empty in Home Assistant.

Have you configured the home assistant MQTT integration?

It’s in the configuration / integrations menu.

Yup. Reconfigured manually, just in case:


… and republished an event, but the problem persists.

On a hunch I tried testing the broker via the CLI tool, but it seems fine:

$  mqtt test --host homeassistant.local
MQTT 3: OK
	- Maximum topic length: 65535 bytes
	- QoS 0: Received 10/10 publishes in 7.40ms
	- QoS 1: Received 10/10 publishes in 36.49ms
	- QoS 2: Received 10/10 publishes in 51.79ms
	- Retain: OK
	- Wildcard subscriptions: OK
	- Shared subscriptions: OK
	- Payload size: >= 100000 bytes
	- Maximum client id length: 65535 bytes
	- Unsupported Ascii Chars: ALL SUPPORTED
MQTT 5: OK
	- Connect restrictions: 
		> Retain: OK
		> Wildcard subscriptions: OK
		> Shared subscriptions: OK
		> Subscription identifiers: OK
		> Maximum QoS: 2
		> Receive maximum: 65535
		> Maximum packet size: 268435460 bytes
		> Topic alias maximum: 10
		> Session expiry interval: Client-based
		> Server keep alive: Client-based

I also tried using the UI to validate my configuration, and it reckons the configuration is valid.

So now I’m totally out of ideas :frowning: I’m sure this is PEBKAC on my part, but having worked through docs, tutorials, etc. I can’t see what I’m doing wrong.

You CANNOT use homeassistant as the username for the broker. It is a reserved name used by discovery. I created a mqttuser with a password in mine and added that user (as well as the homeassistant user) to the ACL file. I do exactly the same kind of thing with a few different machines publishing to the broker and then using an mqtt sensor…

I think if you use a HA user it will fix the issue…

1 Like

Ah! I’d only used that because it was the default, @DavidFW1960.

So, next I:

  1. Created an mqtt user with a password, as a non-administrator.
  2. Reconfigured the MQTT integration to use the mqtt user.

Unfortunately the problem still persists; the sensor trace is empty.

Interestingly when I try listening to and publishing MQTT events from the UI, nothing appears in either the Networking or the Console tabs in my browser … it just fails silently. Not sure if this is related but it seems odd.

So if it’s a HomeAssistant user, you don’t need to add it to the addon.
What is your Mosquitto Broker config?
Mine is:

logins: []
anonymous: false
customize:
  active: true
  folder: mosquitto
certfile: fullchain.pem
keyfile: privkey.pem
require_certificate: false

Then my accesscontrollist:

user mqttuser
topic readwrite #
user homeassistant
topic readwrite #

I use mosquitto_pub to publish to a topic in the broker and use an mqtt sensor to read in the data.

Ah, I was using anonymous access. I’ve updated my broker config and accesscontrollist to be the same as yours. That now works; attempts to publish to the topic fail unless I’m authenticated:

$ mqtt pub --topic home/cabinet/temperature --message 45.42 --host homeassistant.local
CONNECT failed as CONNACK contained an Error Code: NOT_AUTHORIZED.
$ mqtt pub --topic home/cabinet/temperature --message 45.42 --host homeassistant.local --verbose --user mqtt --password REDACTED
Client '[email protected]' sending CONNECT MqttConnect{keepAlive=60, cleanStart=true, sessionExpiryInterval=0, simpleAuth=MqttSimpleAuth{username and password}}
Client '[email protected]' received CONNACK MqttConnAck{reasonCode=SUCCESS, sessionPresent=false, assignedClientIdentifier=auto-752B937A-63CC-5D40-6AA2-54C5D7B0D6CB, restrictions=MqttConnAckRestrictions{receiveMaximum=65535, maximumPacketSize=268435460, topicAliasMaximum=10, maximumQos=EXACTLY_ONCE, retainAvailable=true, wildcardSubscriptionAvailable=true, sharedSubscriptionAvailable=true, subscriptionIdentifiersAvailable=true}}
Client '[email protected]' sending PUBLISH ('45.42') MqttPublish{topic=home/cabinet/temperature, payload=5byte, qos=AT_MOST_ONCE, retain=false}
Client '[email protected]' received PUBLISH acknowledgement MqttPublishResult{publish=MqttPublish{topic=home/cabinet/temperature, payload=5byte, qos=AT_MOST_ONCE, retain=false}}

The same problem exists, though - no values are turning up on the sensor.

How’s your sensor configured? Mine is:

- platform: mqtt
  unique_id: b519255c-5374-4737-b85f-a0c0ca989d6e
  name: "Cabinet Temperature"
  state_topic: "home/cabinet/temperature"
  unit_of_measurement: '°C'
  force_update: true

What does that mean? It is publishing to the topic or not?

My Sensor:

  - platform: mqtt
    name: "Artisan Software Version Installed"
    state_topic: "artisan_version"
    icon: mdi:coffee
    value_template: "{{ value_json.state }}"

Ah… I think you are missing the value_template…

Only if I’m authenticated - what I meant by that was that the authentication is working :slight_smile:

I had thought I wouldn’t need one if posting raw temperature values to home/cabinet/temperature.

So I’ve switched to this sensor config:

- platform: mqtt
  unique_id: b519255c-5374-4737-b85f-a0c0ca989d6e
  name: "Cabinet Temperature"
  state_topic: "home/cabinet"
  unit_of_measurement: "°C"
  force_update: true
  value_template: "{{ value_json.temperature }}"

… and am now posting JSON to home/cabinet:

$ mqtt pub --topic home/cabinet --message '{"temperature": 42.42}' --host 192.168.1.203 --user mqtt --password REDACTED --verbose
Client '[email protected]' sending CONNECT MqttConnect{keepAlive=60, cleanStart=true, sessionExpiryInterval=0, simpleAuth=MqttSimpleAuth{username and password}}
Client '[email protected]' received CONNACK MqttConnAck{reasonCode=SUCCESS, sessionPresent=false, assignedClientIdentifier=auto-743C39E9-E983-E47B-536A-C39E4FFBDA51, restrictions=MqttConnAckRestrictions{receiveMaximum=65535, maximumPacketSize=268435460, topicAliasMaximum=10, maximumQos=EXACTLY_ONCE, retainAvailable=true, wildcardSubscriptionAvailable=true, sharedSubscriptionAvailable=true, subscriptionIdentifiersAvailable=true}} 
Client '[email protected]' sending PUBLISH ('{"temperature": 42.42}') MqttPublish{topic=home/cabinet, payload=22byte, qos=AT_MOST_ONCE, retain=false}
Client '[email protected]' received PUBLISH acknowledgement MqttPublishResult{publish=MqttPublish{topic=home/cabinet, payload=22byte, qos=AT_MOST_ONCE, retain=false}}

… but there’s still nothing showing up on the sensor.