{"id":258,"date":"2020-08-14T16:29:39","date_gmt":"2020-08-14T16:29:39","guid":{"rendered":"https:\/\/samndave.org.uk\/ourdoc\/?p=258"},"modified":"2025-07-10T16:11:34","modified_gmt":"2025-07-10T16:11:34","slug":"thingsboard-data-hacking","status":"publish","type":"post","link":"https:\/\/samndave.org.uk\/index.php\/2020\/08\/14\/thingsboard-data-hacking\/","title":{"rendered":"ThingsBoard Data Hacking"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Thingsboard is a great IoT logging platform, however some management of data can be impossible to do from the GUI and can get annoying. The following database hacks provide a workaround to some of the common issues.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Connecting To The Database<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>SSH to your ThingsBoard server<\/li><li>If you don&#8217;t remember the password:<ul><li><code>cd \/etc\/thingsboard\/conf<\/code><\/li><li><code>grep SPRING_DATASOURCE thingsboard.yml<\/code><\/li><\/ul><\/li><li>Connect with <code>psql -U &lt;username&gt; -d &lt;database&gt; -h 127.0.0.1 -W<\/code><ul><li>The default database name is &#8216;thingsboard&#8217;<\/li><\/ul><\/li><li>Leave the <code>postgres=#<\/code> prompt with <code>\\q<\/code><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Database Tables<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Postgres does not support &#8216;show tables&#8217;. Use &#8216;<code>\\dt<\/code>&#8216; to view tables:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thingsboard=# \\dt\n                List of relations\n Schema |         Name         | Type  |  Owner\n--------+----------------------+-------+----------\n public | admin_settings       | table | postgres\n public | alarm                | table | postgres\n public | asset                | table | postgres\n public | attribute_kv         | table | postgres\n public | audit_log            | table | postgres\n public | component_descriptor | table | postgres\n public | customer             | table | postgres\n public | dashboard            | table | postgres\n public | device               | table | postgres\n public | device_credentials   | table | postgres\n public | entity_view          | table | postgres\n public | event                | table | postgres\n public | relation             | table | postgres\n public | rule_chain           | table | postgres\n public | rule_node            | table | postgres\n public | tb_user              | table | postgres\n public | tenant               | table | postgres\n public | ts_kv                | table | postgres\n public | ts_kv_latest         | table | postgres\n public | user_credentials     | table | postgres\n public | widget_type          | table | postgres\n public | widgets_bundle       | table | postgres\n(22 rows)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Describe a table with &#8216;<code>\\d<\/code>&#8216;, e.g.:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thingsboard=# \\d device\n                           Table \"public.device\"\n     Column      |          Type          | Collation | Nullable | Default\n-----------------+------------------------+-----------+----------+---------\n id              | character varying(31)  |           | not null |\n additional_info | character varying      |           |          |\n customer_id     | character varying(31)  |           |          |\n type            | character varying(255) |           |          |\n name            | character varying(255) |           |          |\n search_text     | character varying(255) |           |          |\n tenant_id       | character varying(31)  |           |          |\nIndexes:\n    \"device_pkey\" PRIMARY KEY, btree (id)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>device<\/strong> &#8211; Contains the device to ID mappings. This is a different ID than what can be found from the ThingsBoard device control panel in the GUI<\/li><li><strong>ts_kv<\/strong> &#8211; Telemetry data. entity_id can be the device ID, &#8216;key&#8217; is the field name.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Removing unwanted telemetry fields<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">ThingsBoard never forgets. If you have sent data to a device ID, it will remember this field for ever, and always show these fields as being available for graphing &amp; reporting on the dashboards. Latest Telemetry from the GUI will show these fields have not been received for a long time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following session determines a device ID for a known device (SoilSensor1), then deletes the test fields key1 to key4, along with &#8216;values&#8217; which was created in error during an integration configuration. For the GUI, the ts_kv_latest table also needs cleaning.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thingsboard=# select id,name from device where name='SoilSensor1';\n               id                |    name\n---------------------------------+-------------\n 1ea5d8e1f450e60959cbf5261aa9fac | SoilSensor1\n(1 row)\n\nthingsboard=# select distinct key from ts_kv where entity_id='1ea5d8e1f450e60959cbf5261aa9fac';\n      key\n----------------\n soilPC\n soilMoisture\n devCharge\n chargeStatus\n power\n devPower\n key1\n key4\n key2\n battery\n busVoltage\n current\n values\n key3\n pressure\n temperature\n humidity\n onBatteryPower\n(18 rows)\n\nthingsboard=# delete from ts_kv where entity_id='1ea5d8e1f450e60959cbf5261aa9fac' and key like 'key%';\nDELETE 4\nthingsboard=# delete from ts_kv where entity_id='1ea5d8e1f450e60959cbf5261aa9fac' and key='values';\nDELETE 20\nthingsboard=# select distinct key from ts_kv where entity_id='1ea5d8e1f450e60959cbf5261aa9fac';\n      key\n----------------\n power\n devPower\n battery\n busVoltage\n current\n pressure\n temperature\n humidity\n onBatteryPower\n soilPC\n soilMoisture\n devCharge\n chargeStatus\n(13 rows)\n\nthingsboard=# delete from ts_kv_latest  where entity_id='1ea5d8e1f450e60959cbf5261aa9fac' and key like 'key%';\nDELETE 4\nthingsboard=# delete from ts_kv_latest  where entity_id='1ea5d8e1f450e60959cbf5261aa9fac' and key='values';\nDELETE 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> Note, if you are viewing the Devices tab in ThingsBoard, then some details cache. Navigate to another section then return to verify the unwanted data has gone.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Renaming a field<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you send data to ThingsBoard with a mistake in the field name, when you correct it, ThingsBoard can not tie the two sets of data together. While this is perfectly reasonable, it can be quite annoying. The following example had a rogue apostrophe when sending humidity data. As a result we ended up with two datasets, humidity and humidity&#8217;. The data was joined together and verified via a graph on the dashboard:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thingsboard=# select id,name from device where name='SoilSensor2';\n               id                |    name\n---------------------------------+-------------\n 1eab940ff2287c0a4259322b72a0dfe | SoilSensor2\n(1 row)\n\nthingsboard=# select distinct key from ts_kv where entity_id='1eab940ff2287c0a4259322b72a0dfe';\n      key\n----------------\n power\n devPower\n battery\n current\n busVoltage\n pressure\n temperature\n humidity\n onBatteryPower\n humidity'\n soilMoisture\n soilPC\n devCharge\n chargeStatus\n(14 rows)\n\nthingsboard=# update ts_kv set key='humidity' where key='humidity''' and entity_id='1eab940ff2287c0a4259322b72a0dfe';\nUPDATE 54827\n\nthingsboard=# delete from ts_kv_latest where key='humidity''' and entity_id='1eab940ff2287c0a4259322b72a0dfe';\nDELETE 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note the single quote needs to be doubled to escape it, and because we already have a field called humidity in the latest telemetry, we just delete rather than rename.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When changing a graph, if you edit it, click on the field, you can just rename in plain text rather than delete the old and set up the new. This is useful if you have added attributes such as colour or a custom label, e.g. &#8216;Humidity (%)&#8217;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Thingsboard is a great IoT logging platform, however some management of data can be impossible [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,32],"tags":[33],"class_list":["post-258","post","type-post","status-publish","format-standard","hentry","category-iot","category-thingsboard","tag-thingsboard"],"_links":{"self":[{"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/posts\/258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/comments?post=258"}],"version-history":[{"count":1,"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/posts\/258\/revisions"}],"predecessor-version":[{"id":1170,"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/posts\/258\/revisions\/1170"}],"wp:attachment":[{"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/media?parent=258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/categories?post=258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/tags?post=258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}