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
if (card && card.type === 'automation') {
if (simState[id].on && card.rules) {
// Collect all targets this automation activates
const myTargets = new Set();
card.rules.forEach(r => { if (r.target) myTargets.add(r.target); });
// 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
// Automation logic:
// - Multiple automations can be ON at the same time
// - Turning ON applies this automation's rules
// - Turning OFF only removes outputs that no OTHER active automation also needs
if (card && card.type === 'automation' && card.rules) {
if (simState[id].on) {
// Activating: just apply this automation's rules on top of whatever is already on
card.rules.forEach(r => {
if (!r.target) return;
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 === 'set') { simState[r.target].on = true; simState[r.target].value = r.value || 0; }
});
} else if (!simState[id].on && card.rules) {
// Deactivating: turn off outputs that no other active automation needs
const otherActiveTargets = new Set();
} else {
// Deactivating: collect all targets that OTHER active automations still need
const keepOn = new Set();
layout.pages.forEach(p => p.cards.forEach(c => {
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 => {
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;
}
});