Skip to main content

Device batteries

A battery is always on a device (an inverter). A device can have no battery, one, or as many as you need: there is no limit.

The order is fixed:

  1. Create the plant (HTTP server POST /v1/plants). It carries no inverter or battery details.
  2. Register the inverter into the plant: on mobile with register, on the web with register_device_web.
  3. Add its batteries with add_device_battery (below), one request per battery.

A battery cannot be added before its device exists, and never to a plant directly. Since 2026-11-20 (migration 0143) the batteries table has no plant column at all: a battery's plant is its device's plant.

  • It follows the device. When the device moves to another plant or changes owner, its batteries move with it.
  • It has its device's owner. That is the plant's creator until the plant is shared with an end user, then the end user, and after an unshare, whoever unshared it (see the HTTP server docs for sharing).
  • It goes when its device goes. Deleting a device (delete_device / confirm_delete_device, or the web console's device deletion) deletes its batteries. Removing a single battery is remove_battery.

The battery object​

Every detail is optional. A battery with no details means "there is a battery, details not entered yet".

FieldTypeMeaning
batteryIdstring (UUID)Names the battery in edit and remove
deviceKeystring (UUID)The device it is on. Always set
plantIdstring | nullIts device's plant. null while the device is in stock (no plant)
brandstring (1–64) | null"ELEKTRA_BOOST" for our own brand, any other brand as free text
namestring (1–100) | nullA label, e.g. "Rack 2"
capacityKwhnumber (> 0, ≤ 100000) | nullUsable capacity
serialNumberstring (1–64) | nullEntered when the battery is added, and never changed after that. Unique across the whole fleet (case is ignored)
versionnumberChanges on every edit. Send it back when you edit or remove
createdAt, updatedAtRFC 3339

Text is trimmed, and an empty string counts as null. Our own brand is recognised however it is spelled (ELEKTRA_BOOST, Elektra Boost, elektra-boost) and always sent back as "ELEKTRA_BOOST".

A request's battery object takes only brand, name, capacityKwh and serialNumber. Any other field (including the old brandOther and powerKw) is refused with INVALID_REQUEST, never ignored.

The serial number​

  • Optional. A battery can be saved without one.
  • Set once, when the battery is added (add_device_battery, or the battery object on register).
  • Never edited. update_battery cannot add a serial to a battery saved without one, change it, or clear it. Any of those is refused with BATTERY_SERIAL_IMMUTABLE. Sending the battery's current serial back unchanged is fine, so a form can send the whole object. The database enforces the same rule.
  • Unique. Two batteries can never have the same serial, compared ignoring case. Adding a duplicate is refused with BATTERY_SERIAL_IN_USE. When a battery is deleted, its serial is free again.

In an edit form, show the serial read-only. If the battery has none, show "Not entered" and no way to add one.

Listing a device's batteries​

// → client
{"type": "list_device_batteries",
"deviceKey": "3f2504e0-4f89-41d3-9a0c-0305e82c3301",
"userId": "17833b2e-a851-4a17-a764-62524d667959",
"limit": 50}
// ← server
{"type": "device_batteries", "ok": true,
"deviceKey": "3f2504e0-4f89-41d3-9a0c-0305e82c3301",
"total": 63,
"nextCursor": "1795166400000000000_6c1f2a1e-…",
"batteries": [
{"batteryId": "6c1f…", "deviceKey": "3f2504e0-4f89-41d3-9a0c-0305e82c3301",
"plantId": "01JABCDEFGHJKMNPQRSTVWXYZ0", "brand": "ELEKTRA_BOOST", "name": null,
"capacityKwh": 5.12, "serialNumber": "EB-24-000913", "version": 1,
"createdAt": "2026-11-20T09:00:00Z", "updatedAt": "2026-11-20T09:00:00Z"}
]}
  • Oldest first, paged:
    • limit defaults to 50, and the most is 200.
    • total is how many batteries the device has.
    • nextCursor is absent on the last page. To get the next page, send it back as cursor.
    • A cursor this server did not issue is refused with INVALID_REQUEST: start again without one.
  • A device without a battery answers "batteries": [] and "total": 0.
  • get_devices already carries hasBattery and batteryCount on every device, plus each device's first 50 batteries as itemType: "battery" items. You need this message only to refresh one device, or to page past the first 50.

Adding a battery​

// → client
{"type": "add_device_battery",
"deviceKey": "3f2504e0-4f89-41d3-9a0c-0305e82c3301",
"userId": "17833b2e-a851-4a17-a764-62524d667959",
"battery": {"brand": "Pylontech", "capacityKwh": 10, "serialNumber": "PY-0042"}}
// ← server
{"type": "add_device_battery_result", "ok": true,
"deviceKey": "3f2504e0-4f89-41d3-9a0c-0305e82c3301",
"batteryId": "9a7e…",
"battery": {"batteryId": "9a7e…", "deviceKey": "3f2504e0-4f89-41d3-9a0c-0305e82c3301",
"plantId": "01JABCDEFGHJKMNPQRSTVWXYZ0",
"brand": "Pylontech", "capacityKwh": 10, "name": null, "serialNumber": "PY-0042",
"version": 1, "…": "…"}}
  • One battery per request. To add several, send several requests, one after another. Battery messages share one in-flight slot per connection (see Errors).
  • The device must be in a plant. A device in stock is refused with DEVICE_NOT_IN_PLANT.
  • battery may be left out, or sent as {}. Fields you leave out are stored as null.
  • Adding is not idempotent. Retrying a lost reply adds a second battery, so refresh the list before retrying. A retry that carries a serial number is safe: the duplicate is refused with BATTERY_SERIAL_IN_USE.

Older apps can still put one battery object on register. New apps register first and then use add_device_battery.

Editing a battery​

// → client
{"type": "update_battery",
"userId": "17833b2e-a851-4a17-a764-62524d667959",
"batteryId": "9a7e…",
"version": 1,
"battery": {"name": "Rack 2", "capacityKwh": null}}
// ← server
{"type": "update_battery_result", "ok": true, "status": "applied",
"deviceKey": "3f2504e0-4f89-41d3-9a0c-0305e82c3301",
"batteryId": "9a7e…",
"battery": {"…": "…", "name": "Rack 2", "capacityKwh": null, "version": 2}}
  • The battery id alone names it. No deviceKey is needed.
  • The edit is a patch. A field you leave out keeps its value, and null clears it. The exception is serialNumber, which cannot be changed (see above).
  • version is required. It is the version you last read. If the battery changed since then, the reply is BATTERY_STALE: refresh and retry.
  • Retries are safe. An edit that would change nothing answers "status": "unchanged" whatever version it names, so retrying after a lost reply is harmless.

Removing a battery​

// → client
{"type": "remove_battery",
"userId": "17833b2e-a851-4a17-a764-62524d667959",
"batteryId": "9a7e…",
"version": 2}
// ← server
{"type": "remove_battery_result", "ok": true,
"deviceKey": "3f2504e0-4f89-41d3-9a0c-0305e82c3301", "batteryId": "9a7e…"}

version is optional here. Left out, the battery is removed whatever it holds. Removing is permanent. Removing a battery that is already gone answers BATTERY_NOT_FOUND: after a retried request, treat that as done.

The 2026-11-18 names​

update_device_battery and remove_device_battery are still accepted and behave exactly like update_battery and remove_battery. Their replies are update_device_battery_result and remove_device_battery_result. Their deviceKey is optional. When you send it, it must be the battery's device, or the reply is BATTERY_NOT_FOUND. New code should use the new names.

Who may do what​

ReadAdd, edit or remove
A device's batteriesdevices VIEW on the devicedevices EDIT on the device

An owner always has both on their own device. A revoked or deactivated device's batteries can be listed but not changed.

Other connections​

After any add, edit or remove, the user's other connections are told. As with device_updated, only connections that have sent subscribe_alerts receive it. Any other screen should refresh with list_device_batteries or get_devices.

{"type": "device_batteries_changed",
"deviceKey": "3f2504e0-4f89-41d3-9a0c-0305e82c3301",
"hasBattery": true,
"batteryCount": 3,
"batteries": [ … ]}

batteries is the device's first 50. batteryCount is the total.

Errors​

Every reply carries ok. On failure it also carries code and error.

codeWhen
INVALID_REQUESTAny of:
  • a missing userId, deviceKey (list, add), batteryId (edit, remove) or version (edit)
  • an unknown field in battery, a wrong type, or a value outside the ranges above
  • a negative limit, or a cursor this server did not issue
  • another device change still in flight on this connection
NOT_AUTHORIZEDThe device does not exist, or the caller lacks the access in the table above
DEVICE_REVOKEDThe device is revoked or deactivated. Its batteries can still be listed but not changed
DEVICE_NOT_IN_PLANTadd_device_battery on a device that is in no plant (stock)
BATTERY_NOT_FOUNDNo battery with that batteryId, or it is not on the deviceKey an old-name message sent
BATTERY_STALEversion no longer matches. Refresh and retry
BATTERY_SERIAL_IN_USEAnother battery already has this serial number
BATTERY_SERIAL_IMMUTABLEAn edit tried to add, change or clear the serial number
BATTERY_FAILEDA server-side failure. Try again

BATTERY_LIMIT no longer exists: a device has no battery limit.

Battery messages share update_device's slot: one device change in flight per connection.

A request may carry a requestId, and every reply echoes it. The web console uses these same messages (see Web console devices). Its get_device_details also returns hasBattery, batteryCount and the first 50 batteries.

What else changes​

  • Activity Trail. A battery change records a device_update entry on its device, with details.action set to battery_added, battery_updated or battery_removed, and details.batteryId.
  • Plant "has a battery". The HTTP server works this out from the plant's devices: a plant has a battery when any of its devices has one. TCP tells it through the hasBattery field of device.binding.changed.v1, which is sent when a device gains its first battery or loses its last.
  • Web console fleet overview. battery.capacityKwh is the sum of capacityKwh over the batteries on the in-scope plants' in-service devices. battery.plantsWithCapacity counts the plants with any capacity entered.
  • Device deletion. Deleting a device deletes its batteries.
    • The logger moves elsewhere. A device that never connected (a "waiting for device" registration) is normally removed when its logger is registered on another inverter. If batteries were added to it, it is deactivated instead, and keeps them.
    • Claim merged into an existing device. When a new logger's first connection shows it is on an inverter the owner already has in the same plant, the batteries added to the new registration move to that inverter's device.
  • Plant deletion is unchanged: the plant's devices are released to unbound stock and keep their batteries. The batteries' owner follows the device's new owner.
  • Migration 0143 (2026-11-20). It converts the data in place:
    • Backup: every battery row is first copied to batteries_backup_0143.
    • Batteries waiting at a plant: each was put on that plant's oldest in-service device, else its oldest device of any status. One whose plant had no device at all was deleted, and is kept in the backup.
    • Duplicate serials: where two batteries had the same serial (ignoring case), the oldest kept it and the others' serials were cleared. The original values are in the backup.
  • Earlier migrations.
    • 0138: copied a plant's old battery capacity to a battery on its device.
    • 0139 and 0141: carried the remaining plant-level batteries over.
    • Brand backfill: only the capacity existed at plant level, so these batteries have no brand. The one-off brand import fills it in from the HTTP server's export: npm run export:plant-battery -- --stage dev in cse-http-be, then go run ./cmd/battery-brand-import -in <export> -out <prefix> in cse_tcp_be/server. Its SQL finds a plant's batteries through the plant's devices.