Fix automation: multiple can be ON, turning off only removes unique outputs

This commit is contained in:
Richard Sauer 2026-04-10 06:46:00 +10:00
parent ba697f410f
commit 05facc24f1

View File

@ -915,33 +915,13 @@ function toggleSim(id) {
} }
} }
// Automation logic: activating one deactivates others, but shared outputs stay on // Automation logic:
if (card && card.type === 'automation') { // - Multiple automations can be ON at the same time
if (simState[id].on && card.rules) { // - Turning ON applies this automation's rules
// Collect all targets this automation activates // - Turning OFF only removes outputs that no OTHER active automation also needs
const myTargets = new Set(); if (card && card.type === 'automation' && card.rules) {
card.rules.forEach(r => { if (r.target) myTargets.add(r.target); }); if (simState[id].on) {
// Activating: just apply this automation's rules on top of whatever is already on
// Deactivate other automation cards, but track what they had on
const allAutoCards = [];
layout.pages.forEach(p => p.cards.forEach(c => {
if (c.type === 'automation' && c.id !== id) allAutoCards.push(c);
}));
allAutoCards.forEach(ac => {
if (simState[ac.id] && simState[ac.id].on) {
// Turn off this automation
simState[ac.id].on = false;
// Turn off its outputs ONLY if not shared with the new automation
(ac.rules || []).forEach(r => {
if (r.target && !myTargets.has(r.target)) {
if (simState[r.target] && r.action !== 'off') simState[r.target].on = false;
}
});
}
});
// Now apply this automation's rules
card.rules.forEach(r => { card.rules.forEach(r => {
if (!r.target) return; if (!r.target) return;
if (!simState[r.target]) simState[r.target] = {on: false, value: 0}; if (!simState[r.target]) simState[r.target] = {on: false, value: 0};
@ -949,16 +929,19 @@ function toggleSim(id) {
else if (r.action === 'off') simState[r.target].on = false; else if (r.action === 'off') simState[r.target].on = false;
else if (r.action === 'set') { simState[r.target].on = true; simState[r.target].value = r.value || 0; } else if (r.action === 'set') { simState[r.target].on = true; simState[r.target].value = r.value || 0; }
}); });
} else if (!simState[id].on && card.rules) { } else {
// Deactivating: turn off outputs that no other active automation needs // Deactivating: collect all targets that OTHER active automations still need
const otherActiveTargets = new Set(); const keepOn = new Set();
layout.pages.forEach(p => p.cards.forEach(c => { layout.pages.forEach(p => p.cards.forEach(c => {
if (c.type === 'automation' && c.id !== id && simState[c.id] && simState[c.id].on) { if (c.type === 'automation' && c.id !== id && simState[c.id] && simState[c.id].on) {
(c.rules || []).forEach(r => { if (r.target) otherActiveTargets.add(r.target); }); (c.rules || []).forEach(r => {
if (r.target && (r.action === 'on' || r.action === 'set')) keepOn.add(r.target);
});
} }
})); }));
// Only turn off outputs unique to this automation
card.rules.forEach(r => { card.rules.forEach(r => {
if (r.target && !otherActiveTargets.has(r.target) && r.action !== 'off') { if (r.target && !keepOn.has(r.target) && r.action !== 'off') {
if (simState[r.target]) simState[r.target].on = false; if (simState[r.target]) simState[r.target].on = false;
} }
}); });