Automation + script

Hi.I’m new to scripts.
could I ask what’s wrong with this automation and this script?
@ pnbruckner

automation:

- id: 'Co2 Cucina light'
  alias: Co2 Cucina light
  trigger:
    platform: numeric_state
    entity_id: sensor.netatmo_cucina_co2
    above: 1500
  condition:
    condition: time
    after: '08:00'
    before: '21:00'
  action:
  - service: script.turn_on
    entity_id: script.flash_room
    data:
      variables:
        entity_id: 'light.gateway_light_f0b4299a4613'
        brightness: '255'
        rgb_color: '[255, 0, 0]'
  - delay: 0:03
  - service: script.turn_off
    entity_id: script.flash_room
  - service: script.turn_on
    entity_id: script.flash_cancel

script

  flash_room:
    alias: Flash Room
    sequence:
      - alias: Gateway flash on
        service: light.turn_on
        data_template:
          entity_id: "{{ entity_id }}"
          brightness: "{{ brightness }}"
          rgb_color: "{{ rgb_color }}"
      - delay:
          # time for flash light on
          seconds: 1
      - alias: Gateway flash Off
        service: light.turn_off
        data_template:
          entity_id: "{{ entity_id }}"
      - alias: loop room
        service: script.turn_on
        data:
          entity_id: script.flash_loop

# LOOP BACK TO START FLASH AGAIN   
  flash_loop:
    alias: Flash loop
    sequence:
      - delay:
          # time for flash light off
          seconds: 1
      - alias: loop_room
        service: script.turn_on
        data:
          entity_id: script.flash_room

# CANCEL FLASHING 
  flash_cancel:
    alias: Cancel Light Flash
    sequence:
      - service: script.turn_off
        data:
          entity_id: script.flash_room
      - service: script.turn_off
        data:
          entity_id: script.flash_loop
      - service: light.turn_off
        data_template:
          entity_id: "{{ entity_id }}"

At what point does it fail?

the light does not flash

Are there any error messages in the system log?

Possibly an error message reporting the inability to run a script that’s already running?

There are no message of error the configuration is ok. But don’t flash

I’m not looking for configuration errors, I’m looking for errors reported to the system log when you run the automation and scripts.

I tested your automation and scripts and they produced errors in the system log.

The errors indicate a problem with the way you call script.flash_room. This script uses variables. Whenever you call the script without supplying variables, it fails (and reports an error in the system log).

In the automation, you call the script with variables:

  - service: script.turn_on
    entity_id: script.flash_room
    data:
      variables:
        entity_id: 'light.gateway_light_f0b4299a4613'
        brightness: '255'
        rgb_color: '[255, 0, 0]'

However, at the end of the automation you call it without supplying variables.

  - service: script.turn_off
    entity_id: script.flash_room

The same thing is done in script.flash_loop. It calls script.flash_room without supplying variables:

      - alias: loop_room
        service: script.turn_on
        data:
          entity_id: script.flash_room

here I can easily put the variables

  - service: script.turn_off
    entity_id: script.flash_room

but here I don’t know how to do this because this script must be valid for more automations. if I insert the variables inside the script, the script can be valid only for an authorization.

with this script I should check three automations and then three gateways

  - alias: loop_room
    service: script.turn_on
    data:
      entity_id: script.flash_room
- id: 'Co2 Cucina light'
  alias: Co2 Cucina light
  trigger:
    platform: numeric_state
# sensor da cambiare
    entity_id: sensor.netatmo_cucina_co2
    above: 500 #1500
  condition:
    condition: time
    after: '08:00'
    before: '21:00'
  action:
  - service: script.flash_room
    data_template:
 # id gateway da cambiare
      entity_id: 'light.gateway_light_f0b4299a4613'
  - delay: 0:03
  - service: script.turn_off
    entity_id: script.flash_room
    data_template:
 # id gateway da cambiare
      entity_id: 'light.gateway_light_f0b4299a4613'
  - service: script.turn_on
    entity_id: script.flash_cancel
  flash_room:
    alias: Flash Room
    sequence:
      - alias: Gateway flash on
        service: light.turn_on
        data_template:
          entity_id: "{{ entity_id }}"
          brightness: 255
          rgb_color: [255, 0, 0]
      - delay:
          # time for flash light on
          seconds: 1
      - alias: Gateway flash Off
        service: light.turn_off
        data_template:
          entity_id: "{{ entity_id }}"
      - alias: loop room
        service: script.turn_on
        data:
          entity_id: script.flash_loop

# LOOP BACK TO START FLASH AGAIN   
  flash_loop:
    alias: Flash loop
    sequence:
      - delay:
          # time for flash light off
          seconds: 1
      - alias: loop_room
        service: script.turn_on
        data:
          entity_id: script.flash_room

# CANCEL FLASHING 
  flash_cancel:
    alias: Cancel Light Flash
    sequence:
      - service: script.turn_off
        data:
          entity_id: script.flash_room
      - service: script.turn_off
        data:
          entity_id: script.flash_loop
      - service: light.turn_off
        data_template:
          entity_id: "{{ entity_id }}"

I modified your code to work in my environment and succeeded in making it flash a light.

Try this:

Automation

- alias: 'Co2 Cucina light'
  trigger:
    platform: numeric_state
    entity_id: sensor.netatmo_cucina_co2
    above: 1500
  condition:
    condition: time
    after: '08:00'
    before: '21:00'
  action:
  - service: script.flash_room
    data:
      entity_id: 'light.gateway_light_f0b4299a4613'
  - delay: '00:00:15'
  - service: script.flash_cancel
    data:
      entity_id: 'light.gateway_light_f0b4299a4613'

Scripts

  flash_room:
    alias: Flash Room
    sequence:
      - service: light.turn_on
        data_template:
          entity_id: "{{ entity_id }}"
      - delay:
          seconds: 2
      - service: light.turn_off
        data_template:
          entity_id: "{{ entity_id }}"
      - service: script.flash_loop
        data_template:
          entity_id: "{{ entity_id }}"

  flash_loop:
    alias: Flash loop
    sequence:
      - delay:
          seconds: 2
      - service: script.flash_room
        data_template:
          entity_id: "{{ entity_id }}"

  flash_cancel:
    alias: Cancel Light Flash
    sequence:
      - service: script.turn_off
        entity_id: script.flash_room
      - service: script.turn_off
        entity_id: script.flash_loop
      - service: light.turn_off
        data_template:
          entity_id: "{{ entity_id }}"

If that works then you can adjust the delay time and put back brightness and rgb_color (I removed them for testing purposes).

2 Likes

@123

script

  flash_room:
    alias: Flash Room
    sequence:
      - service: light.turn_on
        data_template:
          entity_id: "{{ entity_id }}"
          brightness: "{{ brightness }}"
          rgb_color: ["{{ rgb_color }}"]
      - delay:
          seconds: 2
      - service: light.turn_off
        data_template:
          entity_id: "{{ entity_id }}"
      - service: script.flash_loop
        data_template:
          entity_id: "{{ entity_id }}"
          brightness: "{{ brightness }}"
          rgb_color: ["{{ rgb_color }}"]


  flash_loop:
    alias: Flash loop
    sequence:
      - delay:
          seconds: 2
      - service: script.flash_room
        data_template:
          entity_id: "{{ entity_id }}"
          brightness: "{{ brightness }}"
          rgb_color: ["{{ rgb_color }}"]


  flash_cancel:
    alias: Cancel Light Flash
    sequence:
      - service: script.turn_off
        entity_id: script.flash_room
      - service: script.turn_off
        entity_id: script.flash_loop
      - service: light.turn_off
        data_template:
          entity_id: "{{ entity_id }}"

automation

- alias: 'Co2 Cucina light'
  trigger:
    platform: numeric_state
    entity_id: sensor.netatmo_cucina_co2
    above: 1500
  condition:
    condition: time
    after: '08:00'
    before: '21:00'
  action:
  - service: script.flash_room
    data:
      entity_id: 'light.gateway_light_f0b4299a4613'
      brightness: '255'
      rgb_color: '255, 108, 145'
  - delay: '00:01:00'
  - service: script.flash_cancel
    data:
      entity_id: 'light.gateway_light_f0b4299a4613'

error

Error executing script script.flash_room. Invalid data for call_service at pos 1: None for dictionary value @ data[‘rgb_color’]

Error while executing automation automation.co2_cucina_light. Invalid data for call_service at pos 1: None for dictionary value @ data[‘rgb_color’]

it appears that it does not pass the RGB value and creates error in the script.
I made several attempts but my own RGB which is wrong

RGB colour has to be supplied as a list, like this:

  action:
  - service: script.flash_room
    data:
      entity_id: 'light.gateway_light_f0b4299a4613'
      brightness: 255
      rgb_color: [255,108,145] ### <-------format it like this.

Also your brightness should be a number not a string (so no quotes), though I think it will still work.

yes I know but I can’t pass the value to the script. I have already tried to pass the value with the syntax
rgb_color: ‘[255,108,145]’

but the script returns that type of error

I also tried to put this kind of syntax believing that the script formatted the value as an array but it doesn’t work

I seem to remember having a similar problem. My solution was to use color_name: instead. List of names: https://www.w3.org/TR/css-color-3/#svg-color

Correct because values are passed as strings. The rgb_color value you want to pass is a list but it will get converted into a string when passed to the script. The easiest workaround is the one tom_I offered: use color_name.

I think the gateway doesn’t take color_name but now I try.

now it work
thank you

automation

- alias: 'Co2 Cucina light'
  trigger:
    platform: numeric_state
    entity_id: sensor.netatmo_cucina_co2
    above: 1500
  condition:
    condition: time
    after: '08:00'
    before: '21:00'
  action:
  - service: script.flash_room
    data:
      entity_id: 'light.gateway_light_f0b4299a4613'
      brightness: '255'
      color_name: 'red'
  - delay: '00:01:00'
  - service: script.flash_cancel
    data:
      entity_id: 'light.gateway_light_f0b4299a4613'

script:

  flash_room:
    alias: Flash Room
    sequence:
      - service: light.turn_on
        data_template:
          entity_id: "{{ entity_id }}"
          brightness: "{{ brightness }}"
          color_name: "{{ color_name }}"
      - delay:
          seconds: 1
      - service: light.turn_off
        data_template:
          entity_id: "{{ entity_id }}"
      - service: script.flash_loop
        data_template:
          entity_id: "{{ entity_id }}"
          brightness: "{{ brightness }}"
          color_name: "{{ color_name }}"


  flash_loop:
    alias: Flash loop
    sequence:
      - delay:
          seconds: 1
      - service: script.flash_room
        data_template:
          entity_id: "{{ entity_id }}"
          brightness: "{{ brightness }}"
          color_name: "{{ color_name }}"


  flash_cancel:
    alias: Cancel Light Flash
    sequence:
      - service: script.turn_off
        entity_id: script.flash_room
      - service: script.turn_off
        entity_id: script.flash_loop
      - service: light.turn_off
        data_template:
          entity_id: "{{ entity_id }}"

Do you know if light.gateway_light_f0b4299a4613 supports the light component’s flash option?

You can test it using the Services page.

  • For ‘Service’, select light.turn_on
  • For ‘Entity’ enter light.gateway_light_f0b4299a4613
  • For ‘Service Data’ use {"entity_id": "light.gateway_light_f0b4299a4613", "brightness": "255", "color_name": "red", "flash": "short"}
  • Click the CALL SERVICE button

light.gateway_light_xxxxxx not support the light component’s flash option. :frowning:

Oh well, it was worth trying.

Glad to hear the revised scripts are now working properly.