Automation not triggering script. Help Please

I’m after some advice on what may be wrong with the below script and/or automation, stopping it from triggering. I can’t see what it could be but if anyone could please advise where it is going wrong.

The script works fine when triggering with a toggle in lovelace but the automation doesn’t seem to do anything at all. In the automation I’ve tried both script.turn_on and homeassistant.turn_on:

script:

rainbow_light:
  alias: Rainbow Light
  mode: single
  fields:
    entity:
      description: light.guest_room_lamp
      example: light.guest_room_lamp
  variables:
    colors: "{{ [[0.217,0.077], [0.157,0.05], [0.136,0.04], [0.137,0.065],\n    [0.141,0.137],\
      \ [0.146,0.238], [0.151,0.343], [0.157,0.457],\n    [0.164,0.591], [0.17,0.703],\
      \ [0.172,0.747], [0.199,0.724],\n    [0.269,0.665], [0.36,0.588], [0.444,0.517],\
      \ [0.527,0.447],\n    [0.612,0.374], [0.677,0.319], [0.701,0.299], [0.667,0.284],\n\
      \    [0.581,0.245], [0.477,0.196], [0.385,0.155], [0.301,0.116], \n    [0.217,0.077]]\
      \ }}"
  sequence:
  - service: light.turn_on
    data_template:
      xy_color: '{{  colors[(now().second/2.5)|round(0)] }}'
      transition: 4
      brightness: 250
      entity_id: '{{ entity }}'
  icon: mdi:looks

automation:

- alias: Sunset Guest Bedroom
  trigger:
    - platform: sun
      event: sunset
      offset: '+00:40:00'
  action:
    - service: script.rainbow_light
    - delay: '05:00:00'
    - service: script.turn_off
      entity_id: script.rainbow_light

The action section of the automation would be;

  action:
    service: script.turn_on
    target:
      entity_id: script.rainbow_light

You may need ’ around the first script ID - e.g. ‘rainbow_light’:

1 Like

Calling the script directly is also valid. Scripts are services (look in the developer tools services section). So this is valid:

It has an effect on the program flow though. See https://www.home-assistant.io/integrations/script/#waiting-for-script-to-complete

The caveat with the script.turn_off service is that it only works for scripts with a delay or a wait.

1 Like

There are a few things I see wrong. Some might be assumptions on your part.

  1. Scripts don’t continuously run. They are 1 and done. I think that’s the gap in your thought process maybe?

  2. You aren’t providing an entity to run for the script and you aren’t providing a default. So nothing will happen.

  3. Your colors section should error every time, you’re mixing strings and list syntax. Just use multiline formatting. It’s easier to read and you don’t need the \ or the \n’s.

  4. Use variables as much as you can so you can view the trace and see what variables failed.

  5. I’d try to set the transition to match the color changing time. If transition is in seconds, make that 2.5 as well with a 2.5 second delay after (or 5 second if you want the full color for some time). Otherwise you’ll skip colors.

rainbow_light:
  alias: Rainbow Light
  mode: single
  icon: mdi:looks
  fields:
    entity:
      description: The light you want to rainbow
      example: light.guest_room_lamp
    duration:
      description: Time in seconds that the light will repeat the rainbow colors
      example: 300
  variables:
    end: >
      {{ now() + timedelta(seconds=300) }}
    delta: 2.5
    colors: >
        {{ [
             [0.217, 0.077], [0.157, 0.050], [0.136, 0.040], [0.137, 0.065], [0.141, 0.137],
             [0.146, 0.238], [0.151, 0.343], [0.157, 0.457], [0.164, 0.591], [0.170, 0.703],
             [0.172, 0.747], [0.199, 0.724], [0.269, 0.665], [0.360, 0.588], [0.444, 0.517],
             [0.527, 0.447], [0.612, 0.374], [0.677, 0.319], [0.701, 0.299], [0.667, 0.284],
             [0.581, 0.245], [0.477, 0.196], [0.385, 0.155], [0.301, 0.116], [0.217, 0.077]
           ] }}
  sequence:
  - repeat:
      while:
      - condition: template
        value_template: "{{ now() < end }}"
      sequence:
        - variables:
            index: >
              {{ (now().second/delta)|round(0) }}
            color: >
              {{ colors[index] }}
        - service: light.turn_on
          target:
            entity_id: "{{ entity }}"
          data:
            xy_color: "{{ color }}"
            transition: "{{ delta }}"
            brightness: 250
        - delay:
            seconds: "{{ delta }}"

Then in your automation…

- alias: Sunset Guest Bedroom
  trigger:
    - platform: sun
      event: sunset
      offset: '+00:40:00'
  action:
    - service: script.rainbow_light
      data:
        entity: light.guest_room_lamp
        duration: 300
1 Like

I made a bit of an error in my original post as I’ve just realised, instead of calling a script, I need to call another automation (which calls the script), so apologies all for that, but all seems to work fine now.

Petro, thanks for your points. Point 1 and 2 are really because I incorrectly referred to calling a script instead of another automation. Point 3 Colors section definitely works perfectly.

Points 4 and 5, thanks I will look into these and see how I can implement into my script.

What I am having a problem with now, is turning the damned thing off. My updated automation is below but it is just continuing to run:

- alias: Sunset Guest Bedroom
  trigger:
    - platform: sun
      event: sunset
      offset: '-03:00:00'
  action:
    - service: automation.turn_on
      data:
        entity_id: automation.rainbow_light
    - delay: '00:45:00'
    - service: automation.turn_off
      data:
        entity_id: automation.rainbow_light
    - service: light.turn_off
      data:
        entity_id: light.guest_room_lamp