TTN Node data visible & usable in Domoticz

Hi,
I am looking for a solution that makes it possible to use the data from my nodes in Domoticz, a home automation system. This solution should run on a Raspberry Pi (on which Domoticz is also running).

That was the easy part…

I have a TTN gateway up and running (location De Wijk in The Netherlands) and my first node is a Kiss-Lora, which runs fine. I see all the data coming in on my console page. But it is of no use as I can not display this data in a user friendly way nor use the data to trigger an alarm, a light or whatsoever.
The next TTN-node will be a simple detect-alarm for my postbox: if a letter is thrown in this box an alarm will be generated by Domoticz.

My knowledge in how to retrieve data from TTN and make it available for Domoticz is close to zero, so any guidance what to do is highly appreciated. Maybe there are already good solutions which I can’t find as I don’t know where to look for.

Please help me with my first steps in this fascinating world.
Thanks a lot

Bernd

A late response, but perhaps it may still be of use to you or someone else.

MQTT is the way to go. Using the mosquitto MQTT client you can subscribe to your TTN application topic as follows:

mosquitto_sub -v -h eu.thethings.network -t ‘+/devices/+/up’ -u ‘’ -P ‘<access key of your TTN application, ttn-account-v2.etc>’

If you want to use TLS, copy https://console.thethingsnetwork.org/mqtt-ca.pem to local dir and append:

–cafile mqtt-ca.pem -p 8883

This will output the uplink data of your application in json as soon as it is received by TTN. If you want a specific payload field, you can make topic more specific, e.g. +/devices/+/up/temperature_5, and you get only the value for that parameter.

@ abaretta

Interesting, but i do mot completely understand the sequence of it.
Do I need to add a special integration to my application ? (MQTT disappeared from integration)
Which part needs to be installed on the TTN side and where?
Which part needs to be installed on the Domoticz side, most cases a Raspberry Pi.

1 Like

@ Ooievaar

I am getting TTN data into my Domoticz in the following way:

First I added the “data storage” integration to my TTN application.
In the integration overview the link: "go to platform " leads you to a customized Swagger page. There I choose “query/{device-ID}” filled in device ID and a time (5 to 15 minutes)
Click “try it out!”.
That gives you a CURL string. This string is the key that you enter in a LUA script running in Domoticz.

In Domoticz I use a LUA script like this:

local idxVolt = 643 -- virtual sensor for monitoring battary of node 
local idxT_H  = 642 -- virtual temp/humidity sensor

commandArray = {} 

time = os.date("*t")
if  ((time.min % 5)==0)  then         -- Run every 5 minutes.

    json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
    local config =     assert(io.popen("curl -X GET etc-etc... between brackets the exact curl string from swagger page..."))
    local devices = config:read('*all') 
    config:close()

    local dataLoRa = json:decode(devices) 
    if (dataLoRa == nil) then                      -- if no data available then
        print ("dataLoRa-1 string is empty  ")     -- prints this text in the logfile when no data
    else                                           -- if data available extract json results from it 
        local hum  = dataLoRa[1].hum               -- the [1] selects the first dataset from data storage, this does not need to be the
        local temp = dataLoRa[1].temp              -- last dataset received in TTN, if ?last=5m selects the time period for the TTN data
        local volt = dataLoRa[1].volt / 1000       -- converts millivolt to volt
        commandArray[1] = {['UpdateDevice'] = idxVolt .. '|0|' .. volt}
        commandArray[2] = {['UpdateDevice'] = idxT_H .. "|0|" .. temp .. ";" .. hum .. ";0"}
        print ("data from TTN imported")           -- prints this line in log if succesfull
   end
end
return commandArray
1 Like

MQTT is still available. You do not need to add anything in the integrations tab to use MQTT. Just use the manual https://www.thethingsnetwork.org/docs/applications/mqtt/ to check how to use it.

In addition to the link posted by @kersing (there’s a quickstart guide as well), all you need to do is run an MQTT client and subscribe to the topics of your TTN application. The mqtt client you can install on the raspberry pi/Domoticz device.

As mentioned the output is json, so you can easily adapt your existing script to use the MQTT data instead. With MQTT updates are pushed by TTN as opposed to pulling them from the TTN storage.

@ kersing:
Thanks for the clarification, I have some more puzzling to do.

@ abaretta:
You are right, pushing data from TTN is a much more elegant solution than pulling data every 5-15 minutes. Althoug for simple no_real_time sensors my solution is adequate.
If I want to add some sort of doorsensor or proximity sensor I will definitly need to do MQTT for a real time response in Domoticz. So I also have to install mosquitto_sub , next to mosquitto, on the server. As soon as I have the time I will try everything out.

I combined a few ideas in this script, hope it helps others.
Creating a Dummy hardware device with virtual sensors and figuring out the msg format for the %hum took the most time.

#!/bin/sh

mosquitto_sub  -h eu.thethings.network -t '+/devices/+/up' -u 'XXXXX' -P 'ttn-account-v2.XXXXXXXX' | 
while read RAW_DATA
do
 lux=`echo $RAW_DATA | jq -r '.payload_fields.lux'`
 tmp=`echo $RAW_DATA | jq -r '.payload_fields.temperature'`
 hum=`echo $RAW_DATA | jq -r '.payload_fields.humidity'`
 mosquitto_pub -h localhost -m '{ "idx" : 12, "nvalue" : 0, "svalue" : "'$lux'" }' -t 'domoticz/in'
 mosquitto_pub -h localhost -m '{ "idx" : 13, "nvalue" : 0, "svalue" : "'$tmp'" }' -t 'domoticz/in'
 mosquitto_pub -h localhost -m '{ "idx" : 14, "nvalue" : '$hum', "svalue" : "'$hum'" }' -t 'domoticz/in'
done