Repository navigation
feat(node-red): Local Read node for the Powerwall gateway on the LAN #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
packages/node-red-contrib-teslemetry/src/nodes/teslemetry-local-powerwall-read.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| <script type="text/javascript"> | ||
| (function () { | ||
| const READS = { | ||
| live_status: "Live status", | ||
| meters_aggregates: "Meters (aggregates)", | ||
| status: "System status", | ||
| device_controller: "Device controller", | ||
| grid_status: "Grid status", | ||
| battery_soe: "State of energy", | ||
| battery_soe_raw: "State of energy (raw)", | ||
| config: "Gateway config", | ||
| components: "Components (Powerwall 3)", | ||
| firmware: "Firmware", | ||
| backup_events: "Backup events", | ||
| }; | ||
|
|
||
| RED.nodes.registerType("teslemetry-local-powerwall-read", { | ||
| category: "Teslemetry Energy", | ||
| paletteLabel: "Local Read", | ||
| color: "#e6e600", | ||
| defaults: { | ||
| name: { value: "" }, | ||
| localGateway: { | ||
| value: "", | ||
| type: "teslemetry-local-powerwall", | ||
| required: true, | ||
| }, | ||
| read: { value: "live_status", required: true }, | ||
| interval: { | ||
| value: 10, | ||
| validate: function (v) { | ||
| const n = Number(v); | ||
| return v === "" || n === 0 || (n >= 2 && n <= 86400); | ||
| }, | ||
| }, | ||
| }, | ||
| inputs: 1, | ||
| outputs: 1, | ||
| icon: "font-awesome/fa-server", | ||
| label: function () { | ||
| return this.name || "Local: " + (READS[this.read] || this.read); | ||
| }, | ||
| oneditprepare: function () { | ||
| const select = $("#node-input-read"); | ||
| Object.keys(READS).forEach(function (key) { | ||
| select.append($("<option>").val(key).text(READS[key])); | ||
| }); | ||
| select.val(this.read || "live_status"); | ||
| $("#node-input-interval").spinner({ min: 0, max: 86400 }); | ||
| }, | ||
| }); | ||
| })(); | ||
| </script> | ||
|
|
||
| <script type="text/html" data-template-name="teslemetry-local-powerwall-read"> | ||
| <div class="form-row"> | ||
| <label for="node-input-name"><i class="fa fa-tag"></i> Name</label> | ||
| <input type="text" id="node-input-name" placeholder="Name" /> | ||
| </div> | ||
| <div class="form-row"> | ||
| <label for="node-input-localGateway"> | ||
| <i class="fa fa-server"></i> Local gateway | ||
| </label> | ||
| <input type="text" id="node-input-localGateway" /> | ||
| </div> | ||
| <div class="form-row"> | ||
| <label for="node-input-read"><i class="fa fa-book"></i> Read</label> | ||
| <select id="node-input-read"></select> | ||
| </div> | ||
| <div class="form-row"> | ||
| <label for="node-input-interval"> | ||
| <i class="fa fa-clock-o"></i> Poll every | ||
| </label> | ||
| <input type="text" id="node-input-interval" style="width: 80px" /> | ||
| seconds (0 = only on input) | ||
| </div> | ||
| </script> | ||
|
|
||
| <script type="text/html" data-help-name="teslemetry-local-powerwall-read"> | ||
| <p> | ||
| 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. | ||
| </p> | ||
| <p> | ||
| It needs a <b>Local gateway</b>: a Teslemetry Local Powerwall config | ||
| node that is paired and verified. Node-RED must be on the same network | ||
| as the gateway. | ||
| </p> | ||
|
|
||
| <h3>Reads</h3> | ||
| <dl class="message-properties"> | ||
| <dt>Live status</dt> | ||
| <dd> | ||
| Solar, battery, home and grid power, the battery level and the | ||
| grid status, in the shape of the cloud's <code>live_status</code>. | ||
| Values the gateway has no source for are <code>null</code>. | ||
| Built from three gateway reads. | ||
| </dd> | ||
| <dt>Meters (aggregates)</dt> | ||
| <dd> | ||
| The gateway's <code>/api/meters/aggregates</code>: power, energy | ||
| and voltage per meter (<code>site</code>, <code>battery</code>, | ||
| <code>load</code>, <code>solar</code>...). | ||
| </dd> | ||
| <dt>System status</dt> | ||
| <dd> | ||
| The gateway's status query: battery energy, grid connection | ||
| (islanding), meter power, alerts and more. | ||
| </dd> | ||
| <dt>Device controller</dt> | ||
| <dd> | ||
| The extended status query, with more device detail such as | ||
| solar inverters and remote meters. | ||
| </dd> | ||
| <dt>Grid status</dt> | ||
| <dd>A string, such as <code>SystemGridConnected</code>.</dd> | ||
| <dt>State of energy</dt> | ||
| <dd>The battery level as the Tesla app shows it, in percent.</dd> | ||
| <dt>State of energy (raw)</dt> | ||
| <dd> | ||
| The gateway's own battery level, which counts a 5% reserve buffer | ||
| and reads higher than the app. | ||
| </dd> | ||
| <dt>Gateway config</dt> | ||
| <dd>The gateway's <code>config.json</code>.</dd> | ||
| <dt>Components (Powerwall 3)</dt> | ||
| <dd>Powerwall 3 component data.</dd> | ||
| <dt>Firmware</dt> | ||
| <dd>Gateway firmware details.</dd> | ||
| <dt>Backup events</dt> | ||
| <dd>The active manual backup and any scheduled backup events.</dd> | ||
| </dl> | ||
|
|
||
| <h3>Inputs</h3> | ||
| <p>Any message reads the gateway once, whatever its content.</p> | ||
|
|
||
| <h3>Outputs</h3> | ||
| <dl class="message-properties"> | ||
| <dt>payload <span class="property-type">object | string | number</span></dt> | ||
| <dd>What the gateway returned for the selected read.</dd> | ||
| <dt>topic <span class="property-type">string</span></dt> | ||
| <dd>The read's name, such as <code>meters_aggregates</code>.</dd> | ||
| </dl> | ||
|
|
||
| <h3>Details</h3> | ||
| <p> | ||
| <b>Poll every</b> sets how often the node reads the gateway on its | ||
| own: 10 seconds by default, from 2 to 86400 (24 hours). Set it to 0 to read only when | ||
| a message arrives. The node status shows the time of the last read, | ||
| or the error. A gateway that stays unreachable is logged once, not on | ||
| every poll, until a read succeeds again. | ||
| </p> | ||
| <p> | ||
| A message that fails to read goes to a <b>catch</b> node. Live status | ||
| and other cloud data still come from the Energy Event node; this node | ||
| does not merge the two. | ||
| </p> | ||
| </script> |
92 changes: 92 additions & 0 deletions
92
packages/node-red-contrib-teslemetry/src/nodes/teslemetry-local-powerwall-read.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<unknown> { | ||
| 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<typeof setTimeout> | 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, | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.