Skip to content

Commit 86fa74f

Browse files
bharvey88CloudCannon
authored andcommitted
Updated 1 file via CloudCannon.
1 parent b5f2af8 commit 86fa74f

File tree

1 file changed

+35
-34
lines changed

1 file changed

+35
-34
lines changed

docs/snippets/matrix-yaml-generator.htm

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
```html
12
<h2 style="color:#1e88e5; display: flex; align-items: center; gap: 10px;">
23
<img src="/assets/yaml-generator-apollo-logo-small.png" alt="Apollo Automation Logo" style="height: 32px;">
34
Apollo Automation M-1 LED Matrix YAML Generator
@@ -194,19 +195,19 @@ <h3 style="color:#1e88e5;">📝 Generated YAML:</h3>
194195
console.log("Initializing Apollo Matrix YAML Generator...");
195196

196197
const defaultFX = 122;
197-
const output = document.getElementById("output");
198-
const entries = document.getElementById("entity-entries");
199-
const urlInput = document.getElementById("matrix-url");
200-
if (!output || !entries || !urlInput) return;
201-
if (entries.hasChildNodes()) return;
202-
203198
const defaultConfigs = [
204199
{ primary: "temperature", primaryEntity: "weather.nowcast", secondary: "feels_like", secondaryEntity: "sensor.feels_like", tempUnit: "F" },
205200
{ primary: "lock", primaryEntity: "lock.front_door_lock", secondary: "none", secondaryEntity: "", tempUnit: "F" },
206201
{ primary: "co2", primaryEntity: "sensor.brandon_air_1_co2", secondary: "voc", secondaryEntity: "sensor.brandon_air_1_sen55_voc", tempUnit: "F" },
207202
{ primary: "ping", primaryEntity: "sensor.1_1_1_1_round_trip_time_average", secondary: "alarm", secondaryEntity: "alarm_control_panel.alarmo", tempUnit: "F" }
208203
];
209204

205+
const output = document.getElementById("output");
206+
const entries = document.getElementById("entity-entries");
207+
const urlInput = document.getElementById("matrix-url");
208+
if (!output || !entries || !urlInput) return;
209+
if (entries.hasChildNodes()) return;
210+
210211
// Build fields
211212
for (let i = 0; i < 4; i++) {
212213
const def = defaultConfigs[i];
@@ -257,65 +258,64 @@ <h3 style="color:#1e88e5;">📝 Generated YAML:</h3>
257258
urlInput.addEventListener("input", updateYAML);
258259
urlInput.addEventListener("change", updateYAML);
259260

260-
// Generate YAML with special-case logic
261+
// Generate YAML with fallback for segment 1
261262
function updateYAML() {
262263
console.log("Updating YAML...");
263264
const segments = Array.from(entries.querySelectorAll("fieldset")).map((fs, i) => {
264-
const pType = fs.querySelector('select[name="primary"]').value;
265-
const pEnt = fs.querySelector('input[name="primaryEntity"]').value.trim();
266-
const sType = fs.querySelector('select[name="secondary"]').value;
267-
const sEnt = fs.querySelector('input[name="secondaryEntity"]').value.trim();
268-
const unit = fs.querySelector('select[name="tempunit"]')?.value || "F";
269-
const custom = fs.querySelector('textarea[name="custom"]')?.value.trim() || "";
265+
const pTypeRaw = fs.querySelector('select[name="primary"]').value;
266+
const pEntRaw = fs.querySelector('input[name="primaryEntity"]').value.trim();
267+
const sType = fs.querySelector('select[name="secondary"]').value;
268+
const sEntRaw = fs.querySelector('input[name="secondaryEntity"]').value.trim();
269+
const unit = fs.querySelector('select[name="tempunit"]')?.value || "F";
270+
const custom = fs.querySelector('textarea[name="custom"]')?.value.trim() || "";
270271

271-
if (!pEnt && pType !== 'custom') return null;
272+
// Fallback to defaultConfigs for missing primary/secondary
273+
const pEnt = pEntRaw || defaultConfigs[i].primaryEntity;
274+
const sEnt = (sType !== "none") ? (sEntRaw || defaultConfigs[i].secondaryEntity) : "";
272275

273276
let tpl = "";
274277

275-
// Special cases
276-
if (pType === "lock") {
278+
if (pTypeRaw === "lock") {
277279
const name = pEnt.split('.').pop()
278-
.replace(/_lock$/, "")
279-
.replace(/_/g, " ")
280-
.replace(/\b\w/g, m => m.toUpperCase());
280+
.replace(/_lock$/, "")
281+
.replace(/_/g, " ")
282+
.replace(/\b\w/g, m => m.toUpperCase());
281283
tpl = `{{ states('${pEnt}') == 'locked' and '${name} Locked' or '${name} Unlocked' }}`;
282284
}
283-
else if (pType === "door") {
285+
else if (pTypeRaw === "door") {
284286
const name = pEnt.split('.').pop()
285-
.replace(/_sensor$/, "")
286-
.replace(/_/g, " ")
287-
.replace(/\b\w/g, m => m.toUpperCase());
287+
.replace(/_sensor$/, "")
288+
.replace(/_/g, " ")
289+
.replace(/\b\w/g, m => m.toUpperCase());
288290
tpl = `{{ states('${pEnt}') == 'on' and '${name} Sensor Open' or '${name} Sensor Closed' }}`;
289291
}
290-
else if (pType === "co2" && sType === "voc") {
292+
else if (pTypeRaw === "co2" && sType === "voc") {
291293
tpl = `{{ states('${pEnt}') | int }}ppm {{ states('${sEnt}') | int }}ppm`;
292294
}
293-
// Fallback
294295
else {
295-
let pVal = (pType === "custom" && custom)
296+
let pVal = (pTypeRaw === "custom" && custom)
296297
? custom
297-
: formatDataType(pEnt, pType, unit);
298-
let sVal = (sType === "custom" && custom && pType !== "custom")
298+
: formatDataType(pEnt, pTypeRaw, unit);
299+
let sVal = (sType === "custom" && custom && pTypeRaw !== "custom")
299300
? custom
300301
: (sType !== "none" ? formatDataType(sEnt, sType, unit) : "");
301302

302-
// Add labels for non-custom
303-
if (pType !== "custom" && pVal) {
304-
const lbl = dataTypeLabel(pType);
303+
if (pTypeRaw !== "custom" && pVal) {
304+
const lbl = dataTypeLabel(pTypeRaw);
305305
if (lbl) pVal = `${lbl}: ${pVal}`;
306306
}
307307
if (sType !== "custom" && sType !== "none" && sVal) {
308308
const lbl = dataTypeLabel(sType);
309309
if (lbl) sVal = `${lbl}: ${sVal}`;
310310
}
311311

312-
if (pType === "custom") {
312+
if (pTypeRaw === "custom") {
313313
tpl = custom;
314314
} else if (sType === "custom" && custom) {
315315
tpl = `${pVal} ${custom}`;
316316
} else {
317317
tpl = sVal
318-
? `${pVal}${getSeparator(pType, sType)}${sVal}`
318+
? `${pVal}${getSeparator(pTypeRaw, sType)}${sVal}`
319319
: pVal;
320320
}
321321
}
@@ -326,7 +326,7 @@ <h3 style="color:#1e88e5;">📝 Generated YAML:</h3>
326326
"fx": ${defaultFX},
327327
"n": "${tpl}"
328328
}`;
329-
}).filter(Boolean);
329+
});
330330

331331
output.textContent = `rest_command:
332332
matrix_all_segments:
@@ -374,3 +374,4 @@ <h3 style="color:#1e88e5;">📝 Generated YAML:</h3>
374374
}
375375
})();
376376
</script>
377+
```

0 commit comments

Comments
 (0)