AvrelDocs

Collector data sources

The collector reads your equipment through a data source. What it reads, and how, is written in a small JSON file on the gateway called the source config (source.json). You write the file once per gateway; there is nothing to program and nothing in Avrel to change when your hardware is different from anyone else's.

The collector only ever reads. None of the source types below has a way to write a register, publish a message, call a method or switch an output, and the service that runs it cannot open hardware devices at all (see Security and data).

Source type (adapter)Use it forHow it reads
http-jsonControllers, PLC gateways, battery systems, robots and loggers with a JSON APIHTTP GET only
modbus-tcpPLCs, inverters, battery systems, meters, drivesModbus function codes 1 to 4 only (read coils, discrete inputs, holding and input registers)
mqttDevices and robots that publish to an MQTT brokerSubscribes; never publishes
opcuaIndustrial controllers and SCADA serversOPC UA Read service only
fileA data logger that appends to a CSV or JSON-lines file on the gatewayOpens the file for reading

Making a source config#

The quickest way is in the app. Open Edge > Collectors and choose Data source (or add a collector, which shows the same step after the key). Pick the kind of equipment, edit the example, and Avrel checks it with the same rules the collector uses on the gateway. Download source.json saves the checked file, and the install command under it is ready to paste. Avrel does not keep the file; it stays on your gateway.

You can also write the file by hand from the examples below, and check it on the gateway before installing:

python3 avrel_agent.py --check-source --source-config source.json

It reads the source once and prints what came back, for example {"ok": true, "readings": {"battery_v": "51.2 V"}}. The installer runs the same check.

Site impact#

When Avrel checks a config, it also lists every request the collector will make at the site: each HTTP GET, each Modbus read (registers next to each other are read in one request), the MQTT subscriptions, the OPC UA Read call or the file it reads, how often, and about how many requests an hour that comes to. It names calls you can drop, such as a status check the readings already make clear. Nothing is sent to the site to work this out. Assistants get the same as site_impact from prepare_collector_source.

Show the list to whoever looks after the equipment and ask them one question for each address: does reading it do anything? Some controllers take a fresh measurement on a shared bus, clean up a queue or change state when an address is read. When they confirm an address is safe to read, set "passive": true on it (on an HTTP endpoint, or at the top of the config for the whole source), so the next person can see it was checked. Until then, Avrel lists the question.

If an address is costly to read but changes slowly, give its HTTP endpoint min_interval_s. The collector then reads it at most that often, and in between sends the readings it got last time with the time they were measured.

passive and min_interval_s need collector 0.7 or later. An older collector refuses a config that has them, so update the collector first.

Readings#

Every source type has a list of readings. Each reading has:

FieldMeaningDefault
nameWhat Avrel calls the reading, for example battery_v. Letters, digits, ., _, : and -; up to 64 characters. This is the name you see in charts, safety rules and the 3D twin.Required
unitThe unit to show, for example V, A, °C or %.None
scaleMultiply the raw value by this, for example 0.1 when a register holds tenths of a volt.1
offsetThen add this.0

Plus the fields that say where the value is, which depend on the source type. true and false are sent as 1 and 0. A value that is missing, not a number, or older than max_age_s is left out, never guessed.

To show a reading on the right component in the 3D twin, add its name to that component's Readings in Edit hardware (or to readings on the node in topology.json).

Secrets#

Never put a password or token in source.json: the collector refuses a config that has one. Save the secret in a file only root can read and pass it to the installer:

sudo install -m 600 /dev/stdin /root/avrel-source-secret <<< 'the-password'
sudo bash install.sh ... --source-config source.json --source-secret-file /root/avrel-source-secret

The installer puts it in the collector's settings file (/etc/avrel-collector/collector.env, root only), and the collector reads it from the AVREL_SOURCE_SECRET variable. A config can name a different variable with secret_env, but only one starting with AVREL_SOURCE_, so a config can never send the collector's own Avrel key to the equipment.

HTTP and JSON APIs#

The collector GETs one or more URLs and picks values out of the JSON by path. A path is the keys from the top of the document, separated by dots, with list positions as numbers: drives.0.temperature is the temperature of the first item in drives.

{
  "adapter": "http-json",
  "url": "http://127.0.0.1:8080/api/status",
  "readings": [
    {"name": "battery_v", "path": "battery.voltage", "unit": "V"},
    {"name": "battery_soc", "path": "battery.soc", "unit": "%"},
    {"name": "motor_temp_c", "path": "drives.0.temperature", "unit": "°C", "time_path": "drives.0.updated_at"}
  ]
}

When the API returns its readings as a list of items, each with a name and a value, use list instead of naming every reading. With a status_url, the equipment counts as answering only when that URL answers (and, with status_require, has that field):

{
  "adapter": "http-json",
  "base_url": "http://127.0.0.1:8888",
  "status_url": "/api/system/status",
  "status_require": "timestamp",
  "endpoints": [
    {"url": "/api/sensors/latest",
     "list": {"path": "readings", "name": "measure", "value": "value", "unit": "unit", "time": "timestamp"}}
  ],
  "max_age_s": 900
}
SettingMeaningDefault
url and readings, or endpointsOne URL, or up to 20 {url, readings, list} entries. A URL starting with / is added to base_url.Required
min_interval_s (per endpoint)Read this endpoint at most this often (10 to 86400 seconds), repeating its last readings in between.Every poll
passive (per endpoint, or for the whole config)true once the equipment's owner confirms reading it changes nothing. A note for people; it does not change what is read.Not set
base_urlThe start of every URL that begins with /.None
status_url, status_requireA URL that must answer (and a field it must have) for the equipment to count as answering.None
listpath to the list, then the field names in each item for name, value, and optionally unit and time. only limits it to the names you list.None
time_path (per reading)Where the measurement time is, so stale values are left out. Times can be ISO 8601 or seconds or milliseconds since 1970.The time of the poll
naive_timeHow to read a time with no time zone: utc or local.utc
max_age_sLeave out readings measured longer ago than this.900
auth{"type": "bearer"} or {"type": "basic", "user": "avrel"}. The token or password comes from the secret file.None
timeout_sHow long to wait for an answer.10

Redirects are not followed, so credentials can never be sent somewhere else.

Modbus TCP#

The collector reads with Modbus function codes 1 to 4 only. It groups neighbouring addresses so a few requests read everything, and if one address is refused it reads the others one at a time.

{
  "adapter": "modbus-tcp",
  "host": "192.168.1.50",
  "port": 502,
  "unit_id": 1,
  "readings": [
    {"name": "dc_bus_v", "table": "holding", "address": 0, "type": "uint16", "scale": 0.1, "unit": "V"},
    {"name": "pack_current_a", "table": "input", "address": 10, "type": "int32", "scale": 0.01, "unit": "A"},
    {"name": "inverter_temp_c", "table": "holding", "address": 20, "type": "float32", "word_order": "little", "unit": "°C"},
    {"name": "contactor_closed", "table": "discrete", "address": 3}
  ]
}
SettingMeaningDefault
host, portThe device or Modbus gateway.Port 502
unit_idThe Modbus unit (slave) ID, 0 to 255.1
table (per reading)holding, input, coil or discrete.holding
address (per reading)The 0-based register or bit number, as sent on the wire. A register documented as 40001 is 0, 40011 is 10, and input register 30001 is 0 in the input table.Required
type (per reading)uint16, int16, uint32, int32, float32, uint64, int64 or float64. Coils and discrete inputs are always on/off.uint16
word_order (per reading)For values over two or more registers: big (first register is the most significant, the Modbus convention) or little.big
timeout_sHow long to wait for an answer.5

MQTT#

The collector subscribes to the topics you list (MQTT 3.1.1, at QoS 0) and keeps the newest value of each. It has no code to publish. A payload can be a plain number, or JSON with the value at path.

{
  "adapter": "mqtt",
  "host": "127.0.0.1",
  "port": 1883,
  "username": "avrel-reader",
  "readings": [
    {"name": "pack_v", "topic": "robot/bms/state", "path": "voltage", "time_path": "ts", "unit": "V"},
    {"name": "cpu_temp_c", "topic": "robot/compute/temp", "unit": "°C"}
  ],
  "max_age_s": 300
}
SettingMeaningDefault
host, portThe broker.Port 1883, or 8883 with tls
tls, ca_fileConnect with TLS, checking the broker's certificate (against ca_file if given).Off
usernameSign in with this user; the password comes from the secret file. Give the collector a broker account that can only subscribe.None
client_idThe MQTT client ID.avrel- and a random suffix
topic (per reading)The full topic. Wildcards (+, #) are not supported, so each reading is one topic.Required
path, time_path (per reading)Where the value and its time are in a JSON payload.The whole payload
max_age_sLeave out values received longer ago than this.900
keepalive_s, timeout_sKeep-alive interval and connection timeout.60, 10

If the broker cannot be reached, Avrel shows CONTROLLER OFFLINE with the reason, such as bad username or password. The collector keeps trying, waiting longer each time up to two minutes.

OPC UA#

The collector connects, reads the nodes you list with the OPC UA Read service, and disconnects, once per poll. It never writes and never calls methods.

OPC UA needs the asyncua Python package on the gateway: sudo apt install python3-pip && sudo pip3 install asyncua. The installer checks for it.

{
  "adapter": "opcua",
  "endpoint": "opc.tcp://192.168.1.60:4840",
  "username": "avrel-reader",
  "readings": [
    {"name": "line_speed", "node": "ns=2;s=Line1.Speed", "unit": "m/s"},
    {"name": "spindle_temp_c", "node": "ns=2;s=Line1.Spindle.Temperature", "unit": "°C"},
    {"name": "door_closed", "node": "ns=3;i=1001"}
  ]
}
SettingMeaningDefault
endpointThe server address, starting opc.tcp://.Required
usernameSign in with this user; the password comes from the secret file. Without it, the collector connects anonymously.Anonymous
securityA security policy, mode and the client certificate and key files, for example Basic256Sha256,SignAndEncrypt,/etc/avrel-collector-opcua/cert.der,/etc/avrel-collector-opcua/key.pem. The files must be readable by the collector service.None
node (per reading)The node ID, such as ns=2;s=Battery.Voltage or ns=3;i=1001.Required
max_age_sLeave out values whose source timestamp is older than this.900
timeout_sHow long to wait for the server.10

A node the server refuses, or whose status is not Good, is left out.

CSV and JSON-lines files#

When a data logger already writes to a file on the gateway, the collector can read it. Each poll sends the newest complete row; a row the logger is still writing is skipped. To load the rest of the file into Avrel, use history import.

{
  "adapter": "file",
  "path": "/var/log/logger/latest.csv",
  "time_column": "timestamp",
  "readings": [
    {"name": "battery_v", "column": "vbat", "unit": "V"},
    {"name": "ambient_c", "column": "t_amb", "unit": "°C"}
  ]
}

For a JSON-lines file (one JSON object per line), give a path for each reading instead of a column:

{
  "adapter": "file",
  "path": "/var/log/bms/state.jsonl",
  "time_path": "ts",
  "readings": [
    {"name": "soc", "path": "bms.soc", "unit": "%"},
    {"name": "cell_max_v", "path": "bms.cells.max", "unit": "V"}
  ]
}
SettingMeaningDefault
pathThe file, as an absolute path. Not under /tmp, which the collector cannot see.Required
formatcsv or jsonl.From the file name
delimiterFor CSV: ,, ;, a vertical bar or a tab. The first line must be the column names.,
time_column or time_pathWhere each row's time is. Without it, the time the file last changed is used.File change time
naive_timeHow to read a time with no time zone: utc or local.utc
max_age_sSend nothing when the newest row is older than this.900

The collector runs as its own user, so the file must be readable by other users (sudo chmod o+r <file>); the installer warns you if it is not. A file under a home folder works: the installer then lets the service read home folders, but never write them.

When the source does not answer#

If the equipment cannot be read, the collector still reports, with the reason (for example connection refused, timeout, HTTP 401 or file not found) and no readings. Avrel shows the system as CONTROLLER OFFLINE and keeps showing when the last good reading arrived. Addresses are never included in the reason. See Running a collector for what to check.