The Things Stack integration ? (on TTN V3)?

Hi

Following the obsolescence of TTN V2 (The Things Network( being replaced by The Things Stack V3 that has none integratiom so for in HA, I’m trying to find an alternate way to use my Lora sensors in HA. I was thinking to use the MQTT gateway of TTS but it doesn’t use the standard login/pass but an API key which is not supported by MQTT integration in HA.
Someone found out a way to integrate Lora sensors on TTS (TTN V3) in HA or is there no workaround till someone develops an integration for TTS ?

Thanks

Vincèn

Mosquitto has the concept of a mqtt-bridge, e.g. one broker acting as a client to another mqtt broker.
So
HA → Mqtt using login and password to mosquitto → mosquitto using api-key to TTS mqtt server

yep right, forgot about these but in fact found out that API key is basically the MQTT password at TTS :wink: So I have setup an MQTT client in HA to connect at TTS one. Now I’m investigating how I can access these datas in HA and if HA connects well at TTS MQTT server :wink:

@vincen Could you show me your configuration? I have upgraded from V2 to V3, but I am not able to get a valid connection to the new mqtt service at TTS.

Edit: I see now that you probably are not using mosquitto in bridge mode as I do.

@stigvi I have managed to get it working (I’m preparing a blog post to explain whole process and will share link here) but basically I have added a bridge configuration at Mosquitto addon in hassio like that (to duplicate for each device on Lora that you want to get data from):

connection nameitasyouwant
address eu1.cloud.thethings.network
bridge_protocol_version mqttv311
remote_username xxxxx@ttn (to get on device profile page in TTS console)
start_type automatic
notifications false
try_private false
remote_password NNSXS.XXXXXXX (named API keys in TTS console)
bridge_insecure true
topic # in 0
cleansession true

I have then configured a JSON filter on uplink in TTS console to decode payload and in HA configuration here is sample code from one of my sensor (Dragino LHT65 in that case):
Side note: I have used a mqtt client on my computer connected at TTS MQTT server to get proper state topic to use easily :wink:

sensor:
  - platform: mqtt
    state_topic: "v3/mqtt usernamefromttsconsole@ttn/devices/keyofdevice/up"
    name: “Ext. Temp DS”
    value_template: '{{ value_json.uplink_message.decoded_payload.TempC_DS }}'
    unit_of_measurement: "°C"

or for a tracker sensor:

  - platform: mqtt
    state_topic: "v3/mqtt usernamefromttsconsole@ttn/devices/xxxxxxx/location/solved"
    name: “Tracker Latitude”
    value_template: '{{ value_json.location_solved.location.latitude }}'
  - platform: mqtt
    state_topic: "v3/mqtt usernamefromttsconsole@ttn/devices/xxxx/location/solved"
    name: “Tracker Longitude”
    value_template: '{{ value_json.location_solved.location.longitude }}'

Hope it helps and let me know if you have questions :wink:

Vincèn

3 Likes

Thanks for your reply. I tried it, but still no go here

1636729722: Connecting bridge ttn (eu1.cloud.thethings.network:1883)
1636729722: mosquitto version 1.6.12 running
1636729722: Socket error on client <unknown>, disconnecting.
connection ttn
    address eu1.cloud.thethings.network
    topic # in 0
    remote_username xxxxxxx@ttn
    remote_password NNSXS.XXXXXXXXXXXXX
    keepalive_interval 300
    bridge_protocol_version mqttv311
    start_type automatic 
    try_private false
    bridge_insecure true
    notifications false
    cleansession true

This is my mosquitto configuration in the HA addon part

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

Please post complete logs of mqtt once restarted the MQTT server and check in same time if you see MQTT connection showing up in console in TTN/TTS :wink: You should see An Application subscribed or something similar in TTN console of the device (in verbose mode) when your MQTT server reconnects at TTN/TTS !

I’m struggling a bit, my config is very similar to yours and I have clearly mqtt working as mqtt-explorer with the same API key/username works just fine (i can see it registering in the TTNv3 console etc) - so I think it must be my mosquitto config but I’m not sure what since it seems seems happy:

Dec 11 13:02:07 box1 mosquitto[9831]: 1639224127: mosquitto version 2.0.13 starting
Dec 11 13:02:07 box1 mosquitto[9831]: 1639224127: Config loaded from /etc/mosquitto/mosquitto.conf.
Dec 11 13:02:07 box1 mosquitto[9831]: 1639224127: Opening ipv4 listen socket on port 1883.
Dec 11 13:02:07 box1 mosquitto[9831]: 1639224127: Opening ipv6 listen socket on port 1883.
Dec 11 13:02:07 box1 mosquitto[9831]: 1639224127: Connecting bridge TTN3 (eu1.cloud.thethings.network:8883)
Dec 11 13:02:07 box1 mosquitto[9831]: 1639224127: mosquitto version 2.0.13 running

Here’s the config:

connection TTN3
address eu1.cloud.thethings.network:8883
topic # in 0
remote_username username@tts
remote_password NNSXS.XXXXXX
start_type automatic
try_private false
bridge_insecure true
notifications false
cleansession false

I also note that in the mosquitto broker connection state, I always get ‘0’ which i guess means no good…

My problem turns out to be indeed the CA files not being loaded, it seems in some distros mosquitto will not go look in /etc/ssl and fails silently.
Adding this worked a treat
bridge_capath /etc/ssl/certs

If you use config I posted above (The Things Stack integration ? (on TTN V3)? - #5 by vincen) it’ll work for sure as I use it since few months without any problems with multiple sensors !

where do you put the config? If i put it here /hassio/addon/core_mosquitto/config it says invalid yaml.

@rancho check that your mosquitto config file contains these directives:

customize:
  active: true
  folder: mosquitto

and put the extra config indicated previously in /mosquitto/bridge.conf file (I access it through samba share (hassio add-on) on my hassio machine and then restart mosquitto and you should be good (open the console of the device on TTS website before restart of mosquitto and you should see the mosquitto connection when you restart it and if everything is good :wink:

how to visualise the decoded payload in home assistant? i see this with mosquitto “decoded_payload”:{“airHum”:54.08,“airPres”:1023.35,“airTemp”:8.39,“bat”:91,“e25”:21.4,“ec”:0,“leak”:0,“lux”:0.91,“temp”:5.99,“valv”:0,“vwc”:36} somehow with template i guess

Please check previous posts (extract below) in thread where I explain how I did to check messages received through MQTT and be able to decode it properly in HA :wink:

thanks now it works, i just over-complicated it first :slight_smile:

1 Like

@vincen next issue: how can i add the second application definition in the configuration.yaml?

thethingsnetwork:
app_id: app1
access_key key1

but how to add more, app2 and key2?
UPDATE: looks like this only needed for the old V2 version of TTN, now all traffic comes via the MQTT bridge, so the TTN integration is basically outdated and useless. Correct me if i am wrong

What ? you find it too easy :rofl:

You mean for an other device you have in TTN ? You create as many mqtt bridges as devices :wink: with a different identifier each and then rest similar :wink:

Yep as we are orphan of an integration for TTS we have to use for now the mqtt bridge but it’s robust, I have it setup here for something like six months and never had any issue with it like typical lora stuffs, you setup them and they run alone for years :sunglasses:

ok mate, all clear now, i removed the old TTNv2 configs so i used only which works, and thats your bridge solution. Thanks so much. TTN keeps making videos for IoT in angriculture but they dont even support the small farmers and home assistant users… Strange but thats how it is.

Good to hear it works all good :wink: Well they can’t support all platforms and as far as I know TTN integration was not done by them ! They are pushing too much to use cloud stuffs while we can do everything locally and independant of any external things !