MQTT sensor with multiple sensordata in one topic

HI,
I have a dragino LPS8N successfully running with two LHT65N. I happily got it to run without exposing any data to any cloud.

My config.yaml for one of them as an example looks like:

mqtt:
  sensor:
# Dragino Gateway dragino-27xxx8 sensors
# Sensor-2
    - name: "dragino-27xxx8-018DDDE3-temp-2"
      state_topic: "homeassistant/dragino-27xxx8/018DDDE3/data"
      value_template: '{{ value_json.TempC_SHT }}'
      unit_of_measurement: '°C'
      unique_id: dragino-27xxx8-018DDDE3-temp-2
    - name: "dragino-27xxx8-018DDDE3-hum-2"
      state_topic: "homeassistant/dragino-27xxx8/018DDDE3/data"
      value_template: '{{ value_json.Hum_SHT }}'
      unit_of_measurement: '%'
      unique_id: dragino-27xxx8-018DDDE3-hum-2
    - name: "dragino-27xxx8-018DDDE3-bat-stat-2"
      state_topic: "homeassistant/dragino-27xxx8/018DDDE3/data"
      value_template: '{{ value_json.Bat_status }}'
      unit_of_measurement: 'kg'
      unique_id: dragino-27xxx8-018DDDE3-bat-stat-2

The sensor data payload looks like

{"Bat_status":3,"TempC_SHT":-18.91,"SNR":8.2,"Hum_SHT":55.5,"EXT":"Illumination Sensor","RSSI":-75,"ext_sensor":1,"ILL_lux":65535,"BatV":2.959}

The last is the battery status, which has int value from 0-3, indicating the battery status from poor to perfect. I know, that this is not measured in “kg”, but I wanted to see it, “somehow”.

My question: As I configured it, it seems to be 4 sensors to HA, but in fact, it is one sensor, (018DDDE3) which has multiple values.

I have the feeling, that I could add such one sensor much smarter, than I do it, but I did not understand how to “glue” it together, I think my main problem is to understand how the payload can be extracted within HA.

What recommended way is there to read this data and give it more senseful units_of_measurement e.g. or how to add a device class to this…

many thanks
Juergen

I can’t answer your question, but I am attempting to setup the same configuration as you. How did you setup the Dragino LPS8N with LHT65N sensors? Did you find a guide or have any tips?

TIA!

Hi @dv8 ,
it is really a long story to do this. The basic ideas behind are
1.) get mosquittpo broker on HA to run
2.) Set LPS8N to use local MQTT upload to HA broker MQTT-> MQTT Client
3.) Set LPS8N to use ABP End node
get MQTT Explorer for debugging at http://mqtt-explorer.com/
4.) Have a TTL to USB device to change the Sensor behaviour to not use the cloud.
You find the AT commands than here:
LHT65N LoRaWAN Temperature & Humidity Sensor Manual - DRAGINO

and how to add a sensor to TTL/USB (down at the bottom)

Than if connected to a Sensor:
1.) type password: (123456)
2.) Set Sensor to ABP Network Join Mode with
AT+NJM=0
Than get the needed Sensor codes/key for putting them into LPS8N at LoRa → ABP Decryption → at the places
Dev ADDR | APP Session Key | Network Session Key | Decoder

Decoder is Dragino_LHT65
You get Device Address with
AT+DADDR=?
APP Session Key with
AT+APPSKEY=?
Network Session Key with
AT+NWKSKEY=?
on each sensor. Add this information to the form in LPS8N

THAN press the button on the sensor for 3 seconds till the LED blinks 5 time (IMHO it boots than) I did not catch, when the sensors deliver the data and show up. The deliver data every 20 minutes. Maybe(!) you have to boot LPS8N as well, I donno. It “somehow” worked, never found out which boot was really needed.

You than can play around with the MQTT on HA and MQTT Explorer to see what is going on.

On the LPS8N Shell you can run

mosquitto_pub -h 192.168.90.6 -p 1883 -u mqtt-user -P SECRET_MQTT_Password -t /channel/663 -m temp=23

where 192.168.90.6 should be the HA server address you have at home. mqtt-user is the mqtt user that is allowed to send data to mqtt. You than should find with mqtt explorer the channel /channel/663 in HA stating the temp

The channels the topic format, you put in at LPS8N under MQTT → MQTT (in the publish area) client is
homeassistant/CLIENTID/CHANNEL/data
KEEP the CASE as above. CLIENTID and CHANNEL will be replaced by LPS8N ONLY is written in capitals (took me 2 days to get it)
CHANNEL is than the sensor ID and CLIENT is the LPS8N-ID
at my home it looks like
dragino-271293408/018asdf4/data in HA MQTT you should see this with MQTT Explorer

Debugging:
Setup the LPS8N, and do the mosquitto_pub command on the LPS8N to see if anything arrives at HA.
If you see it continue with the sensors.

This are the most obstacles I found in my 10 days journey, having no clue with MQTT and no clue with HA before.

I have everything written down, but it is german only, and at the moment I am struggling with other things since the last HA update.

I hope it helps
Juergen

2 Likes

just add device class

    - name: "dragino-27xxx8-018DDDE3-temp-2"
      state_topic: "homeassistant/dragino-27xxx8/018DDDE3/data"
      value_template: '{{ value_json.TempC_SHT }}'
      unit_of_measurement: '°C'
      device_class: temperature

@fft2411 Thank you very much for the details!

This is indeed more complex than I anticipated. I noticed the LPS8v2 supports two gateways and one can be a local The Things Stack gateway / ChirpStack MQTT bridge. It seems like the LPS8v2 would make the setup to support local MQTT much simpler.

Hi @dv8 ,
I might have done something wrong or too complicated.
I got some of the infos here:

but apparently the templates seem not to work any more after the last update.

thanks
Juergen

Hi @francisp ,
I read here Templating - Home Assistant, that one should add the states to it, to be more secure. So I changed it to

mqtt:
  sensor:
    # Dragino Gateway dragino-275a08 sensors
    # Sensor-1
    - state_topic: "homeassistant/dragino-275a08/018905E4/data"
      name: "dragino-275a08-018905E4-temp"
      value_template: "{{ states('value_json.TempC_SHT') }}"
      unit_of_measurement: "°C"
      unique_id: dragino-275a08-018905E4-temp
      device_class: temperature
    - state_topic: "homeassistant/dragino-275a08/018905E4/data"
      name: "dragino-275a08-018905E4-hum"
      value_template: "{{ states('value_json.Hum_SHT') }}"
      unit_of_measurement: "%"
      unique_id: dragino-275a08-018905E4-hum
      device_class: humidity
    - name: "dragino-275a08-018905E4-bat-stat"
      state_topic: "homeassistant/dragino-275a08/018905E4/data"
      value_template: "{{ states('value_json.Bat_status') }}"
      unit_of_measurement: "%"
      unique_id: dragino-275a08-018905E4-bat-stat
      device_class: battery

But now if I check in developer tools template with

{% for state in states.sensor %}
  {{ state.entity_id }}={{ state.state }},
{% endfor %}

those are “unknown”.

any hint?
thank you
Juergen

The states() function accepts an entity_id as its argument. You can’t use that function here and you don’t need it either.

To your original question, you can tie sensors into a device by using the device: configuration variable with either identifiers or connections as mentioned in the mqtt sensor docs.