|
| 1 | +<script type="text/javascript"> |
| 2 | + RED.nodes.registerType("teslemetry-energy-history", { |
| 3 | + category: "Teslemetry Energy", |
| 4 | + paletteLabel: "History", |
| 5 | + color: "#e6e600", |
| 6 | + defaults: { |
| 7 | + name: { value: "" }, |
| 8 | + teslemetryConfig: { |
| 9 | + value: "", |
| 10 | + type: "teslemetry-config", |
| 11 | + required: true, |
| 12 | + }, |
| 13 | + siteId: { value: "" }, |
| 14 | + historyType: { value: "energy" }, |
| 15 | + period: { value: "day" }, |
| 16 | + startDate: { value: "" }, |
| 17 | + endDate: { value: "" }, |
| 18 | + timeZone: { value: "" }, |
| 19 | + }, |
| 20 | + inputs: 1, |
| 21 | + outputs: 1, |
| 22 | + icon: "font-awesome/fa-line-chart", |
| 23 | + label: function () { |
| 24 | + return this.name || "Energy: " + (this.historyType || "History"); |
| 25 | + }, |
| 26 | + oneditprepare: function () { |
| 27 | + const node = this; |
| 28 | + const siteSelect = $("#node-input-siteId"); |
| 29 | + const configInput = $("#node-input-teslemetryConfig"); |
| 30 | + const historyTypeSelect = $("#node-input-historyType"); |
| 31 | + const periodRow = $("#period-row"); |
| 32 | + |
| 33 | + function updateSites() { |
| 34 | + const configId = configInput.val(); |
| 35 | + if (configId && configId !== "_ADD_") { |
| 36 | + $.getJSON("teslemetry/energy_sites", { config: configId }) |
| 37 | + .done(function (data) { |
| 38 | + const current = siteSelect.val() || node.siteId; |
| 39 | + siteSelect.empty(); |
| 40 | + siteSelect.append( |
| 41 | + '<option value="">From msg.siteId</option>', |
| 42 | + ); |
| 43 | + if (data && data.length > 0) { |
| 44 | + data.forEach(function ([id, name]) { |
| 45 | + siteSelect.append( |
| 46 | + '<option value="' + |
| 47 | + id + |
| 48 | + '">' + |
| 49 | + name + |
| 50 | + " (" + |
| 51 | + id + |
| 52 | + ")</option>", |
| 53 | + ); |
| 54 | + }); |
| 55 | + } |
| 56 | + if (current) { |
| 57 | + siteSelect.val(current); |
| 58 | + } |
| 59 | + }) |
| 60 | + .fail(function (jqxhr, textStatus, error) { |
| 61 | + console.error( |
| 62 | + "Failed to fetch energy sites", |
| 63 | + error, |
| 64 | + ); |
| 65 | + }); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + function updatePeriodVisibility() { |
| 70 | + const historyType = historyTypeSelect.val(); |
| 71 | + if (historyType === "telemetry") { |
| 72 | + periodRow.hide(); |
| 73 | + } else { |
| 74 | + periodRow.show(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + configInput.change(function () { |
| 79 | + updateSites(); |
| 80 | + }); |
| 81 | + |
| 82 | + historyTypeSelect.change(function () { |
| 83 | + updatePeriodVisibility(); |
| 84 | + }); |
| 85 | + |
| 86 | + if (configInput.val()) { |
| 87 | + updateSites(); |
| 88 | + } |
| 89 | + |
| 90 | + updatePeriodVisibility(); |
| 91 | + }, |
| 92 | + }); |
| 93 | +</script> |
| 94 | + |
| 95 | +<script type="text/html" data-template-name="teslemetry-energy-history"> |
| 96 | + <div class="form-row"> |
| 97 | + <label for="node-input-name"><i class="fa fa-tag"></i> Name</label> |
| 98 | + <input type="text" id="node-input-name" placeholder="Name" /> |
| 99 | + </div> |
| 100 | + <div class="form-row"> |
| 101 | + <label for="node-input-teslemetryConfig"> |
| 102 | + <i class="fa fa-globe"></i> Config |
| 103 | + </label> |
| 104 | + <input type="text" id="node-input-teslemetryConfig" /> |
| 105 | + </div> |
| 106 | + <div class="form-row"> |
| 107 | + <label for="node-input-siteId"> |
| 108 | + <i class="fa fa-home"></i> Site ID |
| 109 | + </label> |
| 110 | + <select id="node-input-siteId" style="width: 70%;"> |
| 111 | + <option value="">From msg.siteId</option> |
| 112 | + </select> |
| 113 | + </div> |
| 114 | + <div class="form-row"> |
| 115 | + <label for="node-input-historyType"> |
| 116 | + <i class="fa fa-database"></i> History Type |
| 117 | + </label> |
| 118 | + <select id="node-input-historyType"> |
| 119 | + <option value="energy">Energy History</option> |
| 120 | + <option value="backup">Backup History</option> |
| 121 | + <option value="telemetry">Telemetry (Charging)</option> |
| 122 | + </select> |
| 123 | + </div> |
| 124 | + <div class="form-row" id="period-row"> |
| 125 | + <label for="node-input-period"> |
| 126 | + <i class="fa fa-calendar"></i> Period |
| 127 | + </label> |
| 128 | + <select id="node-input-period"> |
| 129 | + <option value="day">Day</option> |
| 130 | + <option value="week">Week</option> |
| 131 | + <option value="month">Month</option> |
| 132 | + <option value="year">Year</option> |
| 133 | + </select> |
| 134 | + </div> |
| 135 | + <div class="form-row"> |
| 136 | + <label for="node-input-startDate"> |
| 137 | + <i class="fa fa-calendar-o"></i> Start Date |
| 138 | + </label> |
| 139 | + <input |
| 140 | + type="text" |
| 141 | + id="node-input-startDate" |
| 142 | + placeholder="From msg.startDate (ISO 8601)" |
| 143 | + /> |
| 144 | + </div> |
| 145 | + <div class="form-row"> |
| 146 | + <label for="node-input-endDate"> |
| 147 | + <i class="fa fa-calendar-check-o"></i> End Date |
| 148 | + </label> |
| 149 | + <input |
| 150 | + type="text" |
| 151 | + id="node-input-endDate" |
| 152 | + placeholder="From msg.endDate (ISO 8601)" |
| 153 | + /> |
| 154 | + </div> |
| 155 | + <div class="form-row"> |
| 156 | + <label for="node-input-timeZone"> |
| 157 | + <i class="fa fa-clock-o"></i> Time Zone |
| 158 | + </label> |
| 159 | + <input |
| 160 | + type="text" |
| 161 | + id="node-input-timeZone" |
| 162 | + placeholder="From msg.timeZone (e.g., America/Los_Angeles)" |
| 163 | + /> |
| 164 | + </div> |
| 165 | +</script> |
| 166 | + |
| 167 | +<script type="text/html" data-help-name="teslemetry-energy-history"> |
| 168 | + <p>Retrieves historical data from an Energy Site via Teslemetry API.</p> |
| 169 | + <p>Requires a valid Teslemetry configuration and a Site ID.</p> |
| 170 | + |
| 171 | + <h3>History Types</h3> |
| 172 | + <dl> |
| 173 | + <dt>Energy History</dt> |
| 174 | + <dd> |
| 175 | + Returns energy measurements (solar production, battery usage, grid |
| 176 | + imports/exports) aggregated by the selected period. |
| 177 | + </dd> |
| 178 | + <dt>Backup History</dt> |
| 179 | + <dd> |
| 180 | + Returns backup (off-grid) event history aggregated by the selected |
| 181 | + period. |
| 182 | + </dd> |
| 183 | + <dt>Telemetry (Charging)</dt> |
| 184 | + <dd> |
| 185 | + Returns wall connector charging history. Period is not used for this |
| 186 | + type. |
| 187 | + </dd> |
| 188 | + </dl> |
| 189 | + |
| 190 | + <h3>Inputs</h3> |
| 191 | + <dl class="message-properties"> |
| 192 | + <dt class="optional"> |
| 193 | + siteId <span class="property-type">number</span> |
| 194 | + </dt> |
| 195 | + <dd>Energy Site ID (if not set in config)</dd> |
| 196 | + <dt class="optional"> |
| 197 | + historyType <span class="property-type">string</span> |
| 198 | + </dt> |
| 199 | + <dd>Type of history: "energy", "backup", or "telemetry"</dd> |
| 200 | + <dt class="optional"> |
| 201 | + period <span class="property-type">string</span> |
| 202 | + </dt> |
| 203 | + <dd>Aggregation period: "day", "week", "month", or "year"</dd> |
| 204 | + <dt class="optional"> |
| 205 | + startDate <span class="property-type">string</span> |
| 206 | + </dt> |
| 207 | + <dd>Start date in ISO 8601 format (defaults to start of today)</dd> |
| 208 | + <dt class="optional"> |
| 209 | + endDate <span class="property-type">string</span> |
| 210 | + </dt> |
| 211 | + <dd>End date in ISO 8601 format (defaults to end of today)</dd> |
| 212 | + <dt class="optional"> |
| 213 | + timeZone <span class="property-type">string</span> |
| 214 | + </dt> |
| 215 | + <dd>IANA time zone identifier (e.g., "America/Los_Angeles")</dd> |
| 216 | + </dl> |
| 217 | + |
| 218 | + <h3>Outputs</h3> |
| 219 | + <dl class="message-properties"> |
| 220 | + <dt>payload <span class="property-type">object</span></dt> |
| 221 | + <dd>The historical data from the API.</dd> |
| 222 | + </dl> |
| 223 | + |
| 224 | + <h3>References</h3> |
| 225 | + <ul> |
| 226 | + <li> |
| 227 | + <a href="https://teslemetry.com/docs">Teslemetry API Documentation</a> |
| 228 | + </li> |
| 229 | + </ul> |
| 230 | +</script> |
0 commit comments