Can I trigger an automation by scene being activated?

Here’s what I am trying to do and how I’m trying to do it.
Alternate suggestions about how to achieve this same result are totally welcome.

I have an RGB strip next to my bed. (WS2812B controlled by WLED)
I have used this python code to create the ability to fade it up or down: Light Fader by Transition Time
I have set up an automation at 6:30am on weekdays to turn it on, blue, 1% and then fade up to 60% over 30 minutes. This works beautifully.
The other side is I want to make it fade out over 30 minutes at night when I go to bed which is a variable time so I can’t schedule it.
I use both homekit and alexa integrations so I can use voice commands with either siri or alexa.
I have written a script that sets the lights red, 45% brightness, and then fades the lights down to 0 (turns off) over 30 minutes. This works.
I would like to be able to trigger this script by Alexa, but Alexa can not have scripts or automations as entities, only scenes.

So what I need is a way to be able to activate a scene that then triggers my script and this is what I am stuck on.

How could I make this work?

1 Like

I can confirm that you cannot use a scene to turn on a script.

However, turning on a scene produces an event. We can use that event to trigger an automation to call your script.

- alias: 'On My_Scene Activated'
  trigger:
    platform: event
    event_type: 'call_service'
    event_data:
      domain: scene
      service: turn_on
      service_data:
        entity_id: 'scene.my_scene'
  action:
    service: script.turn_on
    entity_id: script.my_script

So when my_scene is turned on, the associated event is detected by the automation and turns on my_script.

13 Likes

Another approach might be a template switch. I don’t use either Homekit or Alexa integrations, but it looks like the Alexa integration allows you to work with a switch entity, so you could probably use the turn_on: series of actions in the template switch to run your script. So the voice command would be “turn on your_switch_name”.

1 Like

I tried using an automation triggered by the scene being activated like this:

- id: '1598543301938'
  alias: Sleepy Light Automation
  description: ''
  trigger:
  - entity_id: scene.bed_light_on_red_45
    platform: state
    to: 'on'
  condition: []
  action:
  - data: {}
    entity_id: script.sleepy_light
    service: script.sleepy_light
  mode: single

But activating the scene does not trigger the automation.
Have I missed something here?

Yes. A scene’s state never changes to on.

If you look at it’s state in Developer Tools > States you’ll see it is scening (whatever that means). It doesn’t change when the scene is activated (i.e. “turned on”).

That’s why I suggested you use an Event Trigger (not a State Trigger).

Huh??? All my scenes turn on scripts…

EDIT… and none of them work lol!!! Never noticed because I never call them anyway (Google Assistant uses the script direct)

1 Like

I got it working like this:

If I activate the scene the “event” of that scene activating triggers the automation which calls the script.
It’s a few steps but it works nicely and I am very happy with the result.
Thank you everyone for the assist, seriously, thank you!

Here is the code for the automation that worked for me:

- id: '1598543301938'
  alias: Sleepy Light Automation
  trigger:
  - event_data:
      domain: scene
      service: turn_on
      service_data:
        entity_id: scene.bed_light_on_red_45
    event_type: call_service
    platform: event
  action:
  - entity_id: script.sleepy_light
    service: script.turn_on
  mode: single

@mrsnyds - I didn’t go down the path of a template switch to solve this one but I can see how it would work. You have introduced me to another great feature of this platform I wasn’t aware of, thank you for that. I will be using these in the future.

2 Likes

The example I posted works (and is the one you ultimately used) yet you marked the post with the example that doesn’t work with the Solution tag. Any particular reason for that?

An error on my part.
I have changed it to mark your post as the solution.
Thank you for your vigilance.

1 Like

I tried the suggested solution and it did work but for all kind of events e.g. on home assistant restart

Hi, i tried the code that u wrote. If im pressing the aktivate button on a scene in the scenes tab, my automation gets triggered. If i aktivate the scene with another automation for example than nothing happens.
Does someone has an idea?

1 Like