#Script gets stuck(?) when trigger by a button

1 messages · Page 1 of 1 (latest)

quick hound
#

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.

worthy bridge
#

What do you see in the automation trace for the run that wasn't stopped?

robust needle
#

Hello @quick hound,
You are switching terms here automation ans script but sharing only a tiny fragment. Please show us your code so we can help.

autumn beaconBOT
#

To format your text as code, enter three backticks on the first line, press Enter for a new line, paste your code, press Enter again for another new line, and lastly three more backticks.
```yaml
example: here
```
Don't forget you can edit your post rather than repeatedly posting the same thing.

quick hound
robust needle
#

Sorry, didn't see the link... 🫢

#

So the automation is working with run, it means your code is working, but your trigger is not.
Make sure that is the right event.

#

Run basically runs the action, not the trigger or conditions.

quick hound
quick hound
robust needle
#

Code Brown, I'm cracking up over here....

quick hound
#

It's my first automation/script, I'm having fun with it 😆

#

Well, I was, until it started acting up...

robust needle
#

I see the trace is saying trigger, condition 1 pass, condition 2 fail.

worthy bridge
#

Yes, no issue there as condition 2 is disabled.

#

Please show the trace of the script run corresponding to this automation run.

robust needle
#

It can't get to the script BC it drops out at condition 2

quick hound
robust needle
quick hound
#

Here's the automation again:

worthy bridge
#

Ok. In the second part where you have the 2 scene.turn_on actions, click on each of them. Anything in the trace showing errors?

robust needle
#

What didn't work?

worthy bridge
#

Another place to check is in your Scenes page. Do you see the scene temp_led_state? What's in there?

robust needle
#

What was supposed to happen, that looked to me like sucessful execution of your code.

worthy bridge
#

Yes. So I don't think it is a logic error, but just checking to be sure. What's in scene.temp_led_state?

quick hound
worthy bridge
#

Ok, so the snapshot was successful.

#

Are your bulb and your button both ZigBee?

#

You said the second scene.turn_on never works (not even once) when triggering with the button?

quick hound
#

Well, let me explain it step-by-step:

#

When triggered using the HA UI, the expected happens:

  • Light turns on (if necessary)
  • light turns brown
  • light turns back to previous state (white)
  • done

When triggered with button:

  • Light turns on (if necessary)
  • light turns brown
  • done
#

So the part that doesn't work is, it doesn't revert to temp scene, specifically when triggered with button...

worthy bridge
#

Yup. HA seems to think that it did do the second scene.turn_on. so I'm thinking it is ZigBee interference, whereby HA did send the signal to change the light back but the light didn't receive it or didn't respond.

#

Are the bulb and button both ZigBee?

quick hound
#

Both button and LED are Ikea and connected through Dirigera to HA. The button doesn't trigger anything in Ikea Home itself, only paired.

#

I'm not sure if they are ZigBee. They are supposed to be Matter?

worthy bridge
#

Ok. How far are both devices from the dirigera?

#

Yeah they're ZigBee. Only the dirigera is matter I believe, but from the dirigera to the button and bulb, Comms is by ZigBee.

quick hound
#

About 1-2 meters away in different directions.

#

How can they interfere though? The automation is triggered by state change on the button, and only then HA sends the request to LED.

worthy bridge
#

Try this: change the delays in your script to 5 seconds. Maybe the dirigera can't deconflict the signals if the delay is just 400ms.

#

ZigBee is a whole other set of considerations.

#

So within HA, there are no errors as HA can handle the quick succession of events. But the IKEA hardware...not so sure. Especially the Dirigera.

#

I have many dozens of IKEA buttons and bulbs in my setup. I started with the old Tradfri hub but quickly changed to a dedicated ZigBee coordinator cos the IKEA hub was not responsive enough.

quick hound
#

So um, you might be right. Inside the automation I added a delay of 1 second before executing the script and it works with the button...

#

The lowest I can go is 600ms delay. At 500 it gets stuck.

#

To be more specific, I didn't change the delay between color changes. Just wait before triggering the script after button is pressed.

worthy bridge
#

Yup. That's your problem then.

quick hound
#

That's so weird though. It can handle color changes in quick succession, but somehow these two events conflict?

#

Thanks a lot for helping me get here though!

worthy bridge
#

Because it's two different devices. Also depends on your specific environment...whether the hub is near WiFi transmitters, congestion on 2.4GHz channels...

quick hound
#

Idk, the reason must be something else. Because it's way to consistent with that failure.

#

Btw, what do you recommend?

#

I'm running HAOS in a VM. Do I get some USB stick to replace Dirigera?

worthy bridge
#

If you intend to seriously expand your ZigBee network, I'd recommend to first read up on ZHA or Zigbee2MQTT and decide how you want to integrate ZigBee with HA. I use ZHA so I can avoid involving another component (MQTT) in my ZigBee workflows.

#

And yes, a dedicated universal ZigBee coordinator will be best. The Dirigera will only work with IKEA ZigBee devices. The Z2M docs have a nice list of coordinators to choose from.

quick hound
#

Is ZigBee on the way out? Will it eventually be replaced with Matter/Thread?

#

Or are they not mutually exclusive?

worthy bridge
#

I highly doubt it. Been using ZigBee for years. It's an established and proven protocol with a big ecosystem of devices.

#

Matter is new, inconsistent and has few devices.

#

I have 132 ZigBee devices (and counting) and no matter.

quick hound
#

Fair enough.