Is it possible to define an extra payload fields directly from TTN application different per device like an API Key

Hi

For each of my objects, I need to define a custom field eg: API key of approximately 24 characters in order to save the data in a custom application that uses a REST API. Between TTN and my custom application, I use Node-Red ( This contrib : https://www.npmjs.com/package/node-red-contrib-ttn ) formatting my payload as I want. For some devices it’s easy to do a check according to the msg.dev_id, I assign the correct API key but I would like to skip this step and define the right API key directly from TTN. All of my devices use the same TTN application.

Ex:

APIKEY “1234567890ABCDEFGHIJLKMNO”

  • Device 1
  • Device 3
  • Device 5

APIKEY “QWERTYUIOPASDFG124565457”

  • Device 4
  • Device 2

Do you think is possible ?

I’ll try to explain correctly what I need, if you need more explanation, let me know.

In July 2017, @htdvisser wrote:

For TTN Console, maybe it’s on the roadmap for version 3? Also, I don’t know if you get these attributes in the MQTT message. You surely won’t get them in the Payload Format functions.

See also the other replies in that topic. And maybe related: How to best write an application that contains many nodes with different measure data types.

@arjanvanb “attributes” look like what I wanted. If the attributes (key:value) can be in the MQTT message, I’ll be ok from my side to handle it.

I looked the API reference and I’ll to know how I can make the call API for set the key “mycustomkey” and value “mycustomvalue” for a specific device ?

Ex: ttnctl devices set mydeviceid --attr-set {“mycustomkey”:“mycustomvalue”}

Thanks for you help

Are you asking how to use the command? The documentation says:

--attr-set stringSlice      Add a device attribute (key:value)

So, I doubt it accepts a JSON value like {"mycustomkey":"mycustomvalue"}. Also, the code suggests it just splits the parameter on the first colon between the key and its value:

But then, the above also shows that ttnctl will log an error telling you what it expects.

If your own command does not work, then I’d try:

ttnctl devices set mydeviceid --attr-set mycustomkey:mycustomvalue

If you know how to use spaces in the value, let us know? Maybe simply --attr-set mycustomkey:"my custom value", but I’ve not tried.

A post was split to a new topic: “ttnctl devices info” throws “Application not registered to this Handler”

As soon I can find my problem “ttnctl devices info” throws “Application not registered to this Handler” I’ll confirm if it’s work @arjanvanb

After testing, is not possible to get attribute in MQTT message. I need to find a way to include APIKEY in my MQTT Message per device

It works for me:

  1. Downloaded the latest version for the Mac and renamed the unzipped executable to ttnctl.
  2. Followed the instructions to log in.
  3. Listed the applications: ttnctl applications list
  4. Followed the instructions to select the active application for the subsequent commands: ttnctl applications select
  5. Listed the active application’s devices: ttnctl devices list
  6. Executed:
    • ttnctl devices set my-device-id --attr-set my1stkey:my1stvalue
    • ttnctl devices set my-device-id --attr-set my2ndkey:"my 2nd value"
    • ttnctl devices set my-device-id --attr-set "my 3rd key":"my 3rd value"
    • ttnctl devices set my-device-id --attr-set "my 4th key:my 4th value"
    • ttnctl devices set my-device-id --attr-set my5thkey:123

…and am nicely getting the additional attributes in each uplink I see using mosquitto_sub -h eu.thethings.network -u my-app-id -P ttn-account-v2.w1...Gk -t my-app-id/devices/+/+

{
   "app_id": "my-app-id",
   "dev_id": "my-device-id",
   ...
   "payload_raw": "By4HLgkSKXUpmSnKACEAIQApYQ==",
   "metadata": {
      "time": "2018-10-27T11:35:41.447910401Z",
      ...
   },
   "attributes": {
      "my 3rd key": "my 3rd value",
      "my 4th key": "my 4th value",
      "my1stkey": "my1stvalue",
      "my2ndkey": "my 2nd value",
      "my5thkey": "123"
   }
}

Also, running ttnctl devices info my-device-id nicely shows:

 Attributes:
          my 3rd key: my 3rd value
          my 4th key: my 4th value
            my1stkey: my1stvalue
            my2ndkey: my 2nd value
            my5thkey: 123

So:

  • ttnctl is quite lenient when it comes to quoting whitespace.
  • All values are string values, even when they could be numeric.
  • Beware that at most 5 attributes are supported: Could not set device on Handler: Maximum number of custom attributes (5) exceeded.
4 Likes

Really ?? Ok ok let me try it and let you know.

BTW thank you for your help,

Hi @arjanvanb following your instructions, yes I am using ttnctl command to create my application, create a device and set attributes on the device and fetch it. It’s work if my handler = ttn-handler-eu. If I set my application to “ttn-handler-us-west” like I want, I am not longer able to register a new device

iMac:~ admin$ ttnctl devices register my-device-bob-1
INFO Using Application                        AppEUI=1234567890 AppID=hello-word-bob
INFO Generating random DevEUI...             
INFO Generating random AppKey...             
INFO Discovering Handler...                   Handler=ttn-handler-eu
INFO Connecting with Handler...               Handler=eu.thethings.network:1904
FATAL Could not register Device                error=Could not set device on Handler: Application not registered to this Handler: handler:application:hello-word-bobnot found source=Wrap: /go/src/github.com/TheThingsNetwork/ttn/utils/errors/errors.go:222

Why Discovering Handler… is ttn-handler-eu while my application is set to ttn-handler-us-west ?

I think my problem is about the wrong handler

ok @arjanvanb I found on to specify my handler-id using the .ttnctl.yml saved in my home directory. Now, I am able to set a custom attributes and get it in my message object.

Thank you for your help