diff --git a/.changeset/node-red-local-powerwall-read.md b/.changeset/node-red-local-powerwall-read.md new file mode 100644 index 0000000..bae8913 --- /dev/null +++ b/.changeset/node-red-local-powerwall-read.md @@ -0,0 +1,5 @@ +--- +"@teslemetry/node-red-contrib-teslemetry": minor +--- + +Add a Local Powerwall Read node that polls a Powerwall gateway on your local network and sends what it returns: live status, meters aggregates, system status, grid status, state of energy, gateway config, components, firmware or backup events. diff --git a/packages/node-red-contrib-teslemetry/README.md b/packages/node-red-contrib-teslemetry/README.md index 6a5875b..bc01be6 100644 --- a/packages/node-red-contrib-teslemetry/README.md +++ b/packages/node-red-contrib-teslemetry/README.md @@ -64,7 +64,7 @@ Configuration node to store your Teslemetry Access Token. - **Replay last known values on connect**: Off by default, so nodes stay silent until a value next changes. When ticked, Teslemetry sends its last known state each time the stream connects or reconnects. Replayed events carry `isCache: true` in the payload of the event nodes; a Signal node outputs only the value, so a replayed value there has no marker. ### teslemetry-local-powerwall -Configuration node that connects Node-RED to a Powerwall gateway on your local network. Node-RED must be on the same network as the gateway. Pick it in the **Local gateway** field of an Energy Command node to send commands to the Powerwall without the internet. +Configuration node that connects Node-RED to a Powerwall gateway on your local network. Node-RED must be on the same network as the gateway. Pick it in the **Local gateway** field of an Energy Command node to send commands to the Powerwall without the internet, or of a Local Powerwall Read node to read the gateway. **Configuration:** - **Config**: Select your Teslemetry Config node @@ -77,6 +77,28 @@ Configuration node that connects Node-RED to a Powerwall gateway on your local n The key and password are stored as Node-RED credentials: outside your flows, never in an exported flow, and not shown again in the editor. Set `credentialSecret` in `settings.js` to encrypt them with a secret of your own. +### teslemetry-local-powerwall-read +Reads a Powerwall gateway over your local network, with no internet or Teslemetry in between. It polls the gateway itself and sends what the gateway returns as it is. The cloud nodes (Energy Event, Energy Command) are unchanged; this node does not merge local data into them. + +**Configuration:** +- **Local gateway**: A Teslemetry Local Powerwall config node, paired and verified. +- **Read**: What to read: + - **Live status**: solar, battery, home and grid power, the battery level and the grid status, in the shape of the cloud's `live_status`. Values the gateway has no source for are `null`. + - **Meters (aggregates)**: the gateway's `/api/meters/aggregates`, power, energy and voltage per meter (`site`, `battery`, `load`, `solar`...). + - **System status** and **Device controller**: the gateway's status queries (battery energy, grid connection, meter power, alerts; the second adds more device detail). + - **Grid status**: a string such as `SystemGridConnected`. + - **State of energy**: the battery level as the Tesla app shows it, and **State of energy (raw)** as the gateway counts it, which includes a 5% buffer. + - **Gateway config** (`config.json`), **Components (Powerwall 3)**, **Firmware** and **Backup events**. +- **Poll every**: Seconds between reads, 10 by default, from 2 to 86400 (24 hours). Set 0 to read only when a message arrives. + +Any input message also reads the gateway once. Polling stops when the flow is redeployed or stopped. + +**Outputs:** +- `msg.payload`: What the gateway returned for the read (an object, or a string or number for grid status and state of energy). +- `msg.topic`: The read's name, such as `meters_aggregates`. + +The node status shows the time of the last read, or the error. A read triggered by a message that fails goes to a **catch** node; a gateway that stays unreachable while polling is logged once, not on every poll. + ### teslemetry-vehicle-command Send commands to a specific vehicle or retrieve vehicle data. diff --git a/packages/node-red-contrib-teslemetry/package.json b/packages/node-red-contrib-teslemetry/package.json index 8468691..d475719 100644 --- a/packages/node-red-contrib-teslemetry/package.json +++ b/packages/node-red-contrib-teslemetry/package.json @@ -56,6 +56,7 @@ "teslemetry-energy-command": "dist/nodes/teslemetry-energy-command.js", "teslemetry-energy-history": "dist/nodes/teslemetry-energy-history.js", "teslemetry-energy-event": "dist/nodes/teslemetry-energy-event.js", + "teslemetry-local-powerwall-read": "dist/nodes/teslemetry-local-powerwall-read.js", "teslemetry-wall-connector": "dist/nodes/teslemetry-wall-connector.js" } }, diff --git a/packages/node-red-contrib-teslemetry/src/localPowerwall.ts b/packages/node-red-contrib-teslemetry/src/localPowerwall.ts index 4dba285..251bbe6 100644 --- a/packages/node-red-contrib-teslemetry/src/localPowerwall.ts +++ b/packages/node-red-contrib-teslemetry/src/localPowerwall.ts @@ -224,3 +224,24 @@ export async function sendLocalFirst( }); return { result: await call(router), path, failover }; } + +/** + * The reads a gateway answers on the LAN, by the name a Local Powerwall Read + * node selects. Each resolves to what the gateway returned, unwrapped from the + * cloud-style `response` envelope where the library adds one. + */ +export const LOCAL_READS = { + live_status: async (site: LocalSite) => (await site.live_status()).response, + meters_aggregates: (site: LocalSite) => site.powerwall.get_meters_aggregates(), + status: (site: LocalSite) => site.powerwall.get_status(), + device_controller: (site: LocalSite) => site.powerwall.get_device_controller(), + grid_status: (site: LocalSite) => site.powerwall.get_grid_status(), + battery_soe: (site: LocalSite) => site.powerwall.get_battery_soe(), + battery_soe_raw: (site: LocalSite) => site.powerwall.get_battery_soe_raw(), + config: (site: LocalSite) => site.powerwall.get_config(), + components: (site: LocalSite) => site.powerwall.get_components(), + firmware: (site: LocalSite) => site.powerwall.get_firmware_details(), + backup_events: (site: LocalSite) => site.powerwall.get_backup_events(), +} satisfies Record Promise>; + +export type LocalRead = keyof typeof LOCAL_READS; diff --git a/packages/node-red-contrib-teslemetry/src/nodes/teslemetry-local-powerwall-read.html b/packages/node-red-contrib-teslemetry/src/nodes/teslemetry-local-powerwall-read.html new file mode 100644 index 0000000..9e99073 --- /dev/null +++ b/packages/node-red-contrib-teslemetry/src/nodes/teslemetry-local-powerwall-read.html @@ -0,0 +1,159 @@ + + + + + diff --git a/packages/node-red-contrib-teslemetry/src/nodes/teslemetry-local-powerwall-read.ts b/packages/node-red-contrib-teslemetry/src/nodes/teslemetry-local-powerwall-read.ts new file mode 100644 index 0000000..aadccf5 --- /dev/null +++ b/packages/node-red-contrib-teslemetry/src/nodes/teslemetry-local-powerwall-read.ts @@ -0,0 +1,92 @@ +import { Node, NodeAPI, NodeDef } from "node-red"; +import { failInput, getErrorMessage } from "../shared"; +import { pollInterval } from "../validation"; +import type { LocalRead } from "../localPowerwall"; +import { Msg } from "../types"; +import type { TeslemetryLocalPowerwallNode } from "./teslemetry-local-powerwall"; + +export interface TeslemetryLocalPowerwallReadNodeDef extends NodeDef { + localGateway: string; + read: LocalRead; + /** Seconds between polls; 0 reads only on an input message. */ + interval: string | number; +} + +export default function (RED: NodeAPI) { + function TeslemetryLocalPowerwallReadNode( + this: Node, + config: TeslemetryLocalPowerwallReadNodeDef, + ) { + RED.nodes.createNode(this, config); + const node = this; + const read = config.read || "live_status"; + const gateway = RED.nodes.getNode(config.localGateway) as + | TeslemetryLocalPowerwallNode + | undefined; + + async function readGateway(): Promise { + if (!gateway?.local) { + throw new Error("Set up the Local gateway first"); + } + const { LOCAL_READS } = await import("../localPowerwall"); + if (!Object.prototype.hasOwnProperty.call(LOCAL_READS, read)) { + throw new Error(`Unknown read: ${read}`); + } + return LOCAL_READS[read](await gateway.local()); + } + + function showRead() { + node.status({ + fill: "green", + shape: "dot", + text: new Date().toLocaleTimeString(), + }); + } + + let closed = false; + let timer: ReturnType | undefined; + /** Set while polls fail, so a gateway that stays down is logged once, not every poll. */ + let failing = false; + const interval = pollInterval(config.interval); + + async function poll() { + try { + const payload = await readGateway(); + if (closed) return; + failing = false; + showRead(); + node.send({ payload, topic: read }); + } catch (error) { + if (closed) return; + const text = getErrorMessage(error); + node.status({ fill: "red", shape: "ring", text }); + if (!failing) node.error(`Local Powerwall read failed: ${text}`); + failing = true; + } + // The next poll waits for this one, so slow replies never pile up. + if (!closed) timer = setTimeout(poll, interval); + } + if (interval) timer = setTimeout(poll, 0); + + node.on("input", async (msg: Msg, send, done) => { + try { + const payload = await readGateway(); + showRead(); + send({ ...msg, payload, topic: read }); + done(); + } catch (error) { + failInput(node, done, error); + } + }); + + node.on("close", (done: () => void) => { + closed = true; + clearTimeout(timer); + done(); + }); + } + RED.nodes.registerType( + "teslemetry-local-powerwall-read", + TeslemetryLocalPowerwallReadNode, + ); +} diff --git a/packages/node-red-contrib-teslemetry/src/nodes/teslemetry-local-powerwall.html b/packages/node-red-contrib-teslemetry/src/nodes/teslemetry-local-powerwall.html index 0895e6f..399fbf0 100644 --- a/packages/node-red-contrib-teslemetry/src/nodes/teslemetry-local-powerwall.html +++ b/packages/node-red-contrib-teslemetry/src/nodes/teslemetry-local-powerwall.html @@ -255,11 +255,12 @@