Skip to content

Data explorer ​

This page shows you how to query what Sensora stores, with SQL or with a question in plain words, and how to get the result out as a chart, a CSV file or a direct database connection.

The Data explorer is under Developer › Data in the sidebar (/data). Everything on it is read-only. A query cannot change or delete data.

The Data page with the table list on the left, a SQL query in the editor and its result as a table with inline bars

Run a query ​

  1. Open Developer › Data.
  2. In the Tables list, select a table name to see its columns. Hover over the table and select Query to load SELECT * FROM <table> LIMIT 100 into the editor.
  3. Change the SQL in the editor.
  4. Select Run, or press Ctrl+Enter (Cmd+Enter on a Mac).

A first query to try:

sql
SELECT device_uid, avg(value) AS avg_temperature
FROM metrics
WHERE metric = 'temperature'
  AND recorded_at > now() - interval '1 hour'
GROUP BY 1
ORDER BY 2 DESC

You can run a query over the API too:

bash
curl -X POST http://localhost:8090/api/data/query \
  -H "Content-Type: application/json" \
  -d '{"sql": "SELECT status, count(*) AS devices FROM devices GROUP BY 1"}'

The answer looks like this:

json
{
  "cols": [["status", "text"], ["devices", "int8"]],
  "rows": [["online", 148], ["provisioned", 13], ["offline", 115]],
  "ms": "1"
}

If your instance has API keys, add the header Authorization: Bearer <key>. See API reference.

Tables ​

The Tables list shows nine tables with their columns and row counts.

TableOne row is
metricsOne stored reading of one metric: device_uid, metric, value, unit, recorded_at, sim
metrics_5mA 5-minute average of one metric of one device: bucket, avg_value, min_value, max_value, n
eventsOne log line: device_uid, type, detail, at, rule_id. See Logs
devicesOne device with its name, template, status (online, offline or provisioned) and last_seen
templatesOne device template
integrationsOne webhook or MQTT integration
inbox_itemsOne payload an integration received, with its status and raw_payload
rule_executionsOne run of a rule, with condition_met, action_result and error_message
rulesOne rule with its graph

The database is PostgreSQL 16, so PostgreSQL functions work. Sensora adds time_bucket('1 hour', recorded_at) to group timestamps into periods.

The read-only role sees more than nine tables

A query can read almost every table in the database, not only the nine in the list. That includes settings, api_keys (a hash and a masked form of each key, never the key itself), claim codes in plain text, and the URL tokens of webhook integrations. Treat access to the Data explorer, and to the direct connection below, as access to the whole instance.

Limits ​

LimitValue
Statements per queryOne. SELECT, WITH ... SELECT, VALUES or TABLE only
Rows returned1,000 at most
Run time5 seconds. A longer query is stopped
Length of the SQL20,000 characters

A result is cut at 1,000 rows. The page then shows "Showing the first 1,000", but the API answer has no such mark. When the API returns exactly 1,000 rows, assume the result was cut. Add a WHERE on time or aggregate with GROUP BY.

Ask in plain words ​

Type a question in Ask a question about your data, for example average temperature per device, last hour, and select Ask. Ask Sensora writes the SQL, puts it in the editor and runs it. The SQL stays on screen, so you can read it, change it and run it again.

This needs an AI model. Without one, the page shows Connect a model, as in the picture above. See Ask Sensora.

Save a query ​

  1. Select Save query above the editor.
  2. Type a name and select Save.

Saved queries show in the left rail. There are no user accounts, so everyone who uses the instance sees the same list. Select a saved query to load it. Its menu has Open, Rename… and Delete…. Names must be unique, ignoring upper and lower case.

Chart and download ​

  • Chart draws the first number column of the result, over time when the result has a timestamp column, otherwise as bars per category.
  • Download CSV saves the rows on screen as sensora-query.csv. This is at most 1,000 rows.

To export more than 1,000 rows, use a direct connection.

Connect Grafana or a notebook directly ​

Tools such as Grafana, Jupyter and psql can read the PostgreSQL database directly. Sensora creates a database role named readonly for this. The bundled Docker Compose stack does not create a login for it and does not open the database port. You do both yourself. Run the commands from the repository folder.

  1. Create a login user that is a member of the readonly role. The role itself cannot log in.

    bash
    docker compose -f deploy/docker-compose.yml exec db \
      psql -U postgres -d sensora \
      -c "CREATE ROLE grafana LOGIN PASSWORD 'choose-a-password' IN ROLE readonly;"

    Replace postgres and sensora if you changed DB_USER or DB_NAME.

  2. Publish the database port. Save this as deploy/db-port.yml:

    yaml
    services:
      db:
        ports:
          - "127.0.0.1:5432:5432"

    Then start the stack with both files. Use both -f options every time you start the stack, or the port closes again.

    bash
    docker compose -f deploy/docker-compose.yml -f deploy/db-port.yml up -d

    127.0.0.1 lets only programs on the server itself connect. To reach the database from another machine, replace it with the server's address on your network.

  3. Connect your tool with the user from step 1:

    text
    postgresql://grafana:[email protected]:5432/sensora

DANGER

The published port also accepts the database owner, whose password is password unless you set DB_PASSWORD before the first start. Never publish the port on an address that the internet can reach.

A direct connection has no row cap and no time limit. It is still read-only: the readonly role cannot write.

Over a direct connection, write explorer.devices to get the devices table of the explorer with its status column. The plain name devices is the underlying table, which has no status column.

Show the connection string on the Data page ​

Optional: set READONLY_DSN in deploy/.env and start the stack again. The Direct connection card on the Data page then shows the string with a copy button.

ini
READONLY_DSN=postgresql://grafana:[email protected]:5432/sensora

READONLY_DSN only puts text on the screen. It does not create the user or open the port. Everyone who can open the Data page can read the password in it.

When READONLY_DSN is not set, the Read-only database row in Settings › Server & storage shows the placeholder postgresql://readonly@localhost:5432/sensora. It does not work as it is.

Sensora is open source under the Apache 2.0 license.