Skip to content

Instantly share code, notes, and snippets.

@Engineer-of-Stuff
Created September 5, 2019 02:13
Show Gist options
  • Save Engineer-of-Stuff/129badef3e8de319975d474dedbb9d62 to your computer and use it in GitHub Desktop.
Save Engineer-of-Stuff/129badef3e8de319975d474dedbb9d62 to your computer and use it in GitHub Desktop.

How to Export Data from ThingsBoard

  1. Get your auth token like this:

    curl -X POST \
    --header 'Content-Type: application/json' \
    --header 'Accept: application/json' \
    -d '{"username":"[your username]","password":"[your password]"}' \
    'https://[thingsboard url]/api/auth/login'

    You will receive something like this:

    {"token":"[really long string]","refreshToken":"[really long string]"}

    Your auth token is the token value.

  2. Edit export-tb-data.sh with your settings:

  • THINGSBOARD_URL - URL to your TB install

  • DEVICEID - find the device ID by going to Devices, click on the device, then "Copy Device ID"

  • KEYS - the names of the data values you send to TB. key1,key2

  • ENDTS - the current unix time in milliseconds, see www.epochconverter.com. startTs is set to January 1st 2001 at 12:00 AM. TB will give you all values between Jan. 1st 2001 and what you put for ENDTS.

  • OUTFILE - the name of the file to put the exported data in

  1. Then run: bash export-tb-data.sh > tb-data.json

    Depending on the size of your data it could take over 2 minutes and the output file can be over 50 mb.

    If there is an error just retry.

  2. In the output file, data will be unformatted one line. It will have this structure:

    {
      "gas": [
        {
          "ts": 1479735870786,
          "value": "1"
        },
        {
          "ts": 1479735871857,
          "value": "2"
        }
      ],
      "temperature": [
        {
          "ts": 1479735870786,
          "value": "3"
        },
        {
          "ts": 1479735871857,
          "value": "4"
        }
      ]
    }

    To view your data just do cat tb-data.json. Don't try to open it in any program or editor if the file is over 1mb.

#!/bin/bash
JWT_TOKEN=""
THINGSBOARD_URL=""
DEVICEID=""
KEYS=""
ENDTS=""
OUTFILE="tb-data.json"
curl -X GET \
--header 'Accept: */*' \
--header "Content-Type:application/json" \
--header "X-Authorization: Bearer ${JWT_TOKEN}" \
"${THINGSBOARD_URL}/api/plugins/telemetry/DEVICE/${DEVICEID}/values/timeseries?keys=${KEYS}&startTs=978307200000&endTs=${ENDTS}&agg=NONE&limit=100000000" \
-o $OUTFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment