First things first, here are the relevant YAMLs.
I've been banging my head with this "very important" automation for several hours and I'm pooped.
It works as expected when I use the Run actions or Run (script) button in HA, it saves the current state of the LED, changes the color to one specified in the predefined scene, then reverts it back, in effect flashing a color.
But the problem comes from using a button (specifically, Ikea Tradfri On/Off) to trigger the same automation. The automation starts, it transitions the color of the LED to specified scene color (brown) and stops there, never reverting to the original state. I can't understand why.
At first I though it's because this button always sends 2 change events on 1 click, so I assumed the script ran twice, because of that, the second time the event of "saving the current LED state" coincides with when it has changed color to brown so it just sets itself back to brown thinking it was the initial state.
I added this to debounce the automation using:
- condition: template
value_template: >-
{{ (now().timestamp() - (state_attr('script.engage_code_brown',
'last_triggered') | default(0, true) | as_timestamp)) > 1 }}
And it seems that when triggered twice, the automation terminates on the second execution, because Traces ends with Stopped because a condition failed at February 15, 2025 at 1:06:22 AM (runtime: 0.00 seconds), but the behavior didn't change. So maybe that's not the issue.
How can I debug this? I would really appreciate any directions.