Doing different things in script, depending on who called it

Hey hey
Ive had an automation that i run when my door is locking and when its locked.
It turns on a light (outside), That flashes red for example if any window or door is opened, or bleu if my robot vacuum didnt get fresh water this day.
This works perfectly fine so far, but the problem is that light is outside.
If the light doesnt flash green (what signalisizes everything is ok) I have to unlock again and go inside again.
So I got a signal light for inside and want to add the same thing as outside inside my house.
If the door is unlocking (at a specified time) I want to have the same thing as outside.

I dont want to use this inside an automation, too. Because this would mean doubling the code for unlocking and locked.
So I think have a script is best way here.
I did create the script so far, but always turning on both lamps, which isnt exactly what I want.

If the door in UNLOCKING, I want to use the inside lamp.
If the door is LOCKING, I want to use the outside lamp (or lamps in nearer future).

So I suppose I need a field and call the script giving it the field value I can react to.
So I can keep my tests (light on test, robot vaccum test, and so on) and depending on the field value I turn the specific light on.

Am I correct with this way? Or is there any better solution?
And can someone give me a hint how to use the fields?

We cannot guess what you code id, so please share what you have done.

How to help us help you - or How to ask a good question.

Since you are calling the scripts yourself, you can pass information to the script and use that in the script to choose what to do. See the section about passing variables to scripts here:

You could for instance pass the color to the script that flashes the light.

Uhm SORRY!
I forgot to post the code Ive already done.
So here it is:

alias: Alles aus Test
sequence:
  - if:
      - condition: state
        entity_id: schedule.turbeleuchtungszeiten
        state: "off"
    then:
      - stop: ""
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: state
                entity_id: binary_sensor.alle_fenster
                state: "on"
              - condition: state
                entity_id: binary_sensor.alle_turen
                state: "on"
            alias: Fenster und Türen offen
        sequence:
          - service: light.turn_on
            metadata: {}
            data:
              rgb_color:
                - 255
                - 0
                - 0
              brightness_pct: 100
            target:
              entity_id:
                - light.nachtlicht_eingang_licht
          - service: light.turn_on
            metadata: {}
            data:
              rgb_color:
                - 255
                - 0
                - 0
              brightness_pct: 100
              effect: Flash
            target:
              entity_id:
                - light.shelly_bulb_carport
          - delay:
              hours: 0
              minutes: 0
              seconds: 30
              milliseconds: 0
          - service: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id:
                - light.nachtlicht_eingang_licht
                - light.shelly_bulb_carport
      - conditions:
          - alias: Licht an
            condition: or
            conditions:
              - condition: state
                entity_id: switch.alle_ladenlichter
                state: "on"
              - condition: state
                entity_id: switch.alle_kuchengerate
                state: "on"
        sequence:
          - service: light.turn_on
            metadata: {}
            data:
              rgb_color:
                - 255
                - 136
                - 0
              brightness_pct: 100
            target:
              entity_id:
                - light.nachtlicht_eingang_licht
          - service: light.turn_on
            metadata: {}
            data:
              rgb_color:
                - 255
                - 136
                - 0
              brightness_pct: 100
              effect: Flash
            target:
              entity_id:
                - light.shelly_bulb_carport
          - delay:
              hours: 0
              minutes: 0
              seconds: 30
              milliseconds: 0
          - service: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id:
                - light.nachtlicht_eingang_licht
                - light.shelly_bulb_carport
      - conditions:
          - condition: state
            entity_id: input_boolean.roborock_tiere_wasser_beigefullt
            state: "off"
          - condition: state
            entity_id: input_boolean.roborock_tiere_out_of_order
            state: "off"
        sequence:
          - service: light.turn_on
            metadata: {}
            data:
              rgb_color:
                - 0
                - 110
                - 255
              brightness_pct: 100
            target:
              entity_id:
                - light.nachtlicht_eingang_licht
          - service: light.turn_on
            metadata: {}
            data:
              rgb_color:
                - 0
                - 110
                - 255
              brightness_pct: 100
              effect: Flash
            target:
              entity_id:
                - light.shelly_bulb_carport
          - delay:
              hours: 0
              minutes: 0
              seconds: 30
              milliseconds: 0
          - service: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id:
                - light.nachtlicht_eingang_licht
                - light.shelly_bulb_carport
        alias: Roborock kein Wasser
    default:
      - service: light.turn_on
        metadata: {}
        data:
          rgb_color:
            - 55
            - 255
            - 0
          brightness_pct: 100
        target:
          entity_id:
            - light.nachtlicht_eingang_licht
      - service: light.turn_on
        metadata: {}
        data:
          rgb_color:
            - 55
            - 255
            - 0
          brightness_pct: 100
        target:
          entity_id:
            - light.shelly_bulb_carport
      - alias: Carport Licht an, wenn dunkel
        if:
          - condition: numeric_state
            entity_id: sensor.bewegungsmelder_draussen_beleuchtungsstarke
            below: 25
        then:
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.schalter_carport_licht
      - delay:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0
      - service: light.turn_off
        metadata: {}
        data: {}
        target:
          entity_id:
            - light.shelly_bulb_carport
            - light.nachtlicht_eingang_licht
      - delay:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0
      - service: switch.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: switch.schalter_carport_licht
description: ""
icon: mdi:alert-circle
fields:
  ausloser:
    selector:
      text: null
    name: Auslöser
    required: true

perhaps something like this.

i’ve switched your field to be an entity type. with a default set to light.nachtlicht_eingang_licht. so you can call this script with any light entity, and it will use that light to do your signaling.

have your automation pass in whichever light is appropriate.

holler if you need help on the automation side…

alias: Alles aus Test
sequence:
  - if:
      - condition: state
        entity_id: schedule.turbeleuchtungszeiten
        state: "off"
    then:
      - stop: ""
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: state
                entity_id: binary_sensor.alle_fenster
                state: "on"
              - condition: state
                entity_id: binary_sensor.alle_turen
                state: "on"
            alias: Fenster und Türen offen
        sequence:
          - service: light.turn_on
            metadata: {}
            data:
              rgb_color:
                - 255
                - 0
                - 0
              brightness_pct: 100
            target:
              entity_id: "{{ light_entity }}"
          - service: light.turn_on
            metadata: {}
            data:
              rgb_color:
                - 255
                - 0
                - 0
              brightness_pct: 100
              effect: Flash
            target:
              entity_id: "{{ light_entity }}"
          - delay:
              hours: 0
              minutes: 0
              seconds: 30
              milliseconds: 0
          - service: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: "{{ light_entity }}"
      - conditions:
          - alias: Licht an
            condition: or
            conditions:
              - condition: state
                entity_id: switch.alle_ladenlichter
                state: "on"
              - condition: state
                entity_id: switch.alle_kuchengerate
                state: "on"
        sequence:
          - service: light.turn_on
            metadata: {}
            data:
              rgb_color:
                - 255
                - 136
                - 0
              brightness_pct: 100
            target:
              entity_id: "{{ light_entity }}"
          - service: light.turn_on
            metadata: {}
            data:
              rgb_color:
                - 255
                - 136
                - 0
              brightness_pct: 100
              effect: Flash
            target:
              entity_id: "{{ light_entity }}"
          - delay:
              hours: 0
              minutes: 0
              seconds: 30
              milliseconds: 0
          - service: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: "{{ light_entity }}"
      - conditions:
          - condition: state
            entity_id: input_boolean.roborock_tiere_wasser_beigefullt
            state: "off"
          - condition: state
            entity_id: input_boolean.roborock_tiere_out_of_order
            state: "off"
        sequence:
          - service: light.turn_on
            metadata: {}
            data:
              rgb_color:
                - 0
                - 110
                - 255
              brightness_pct: 100
            target:
              entity_id: "{{ light_entity }}"
          - service: light.turn_on
            metadata: {}
            data:
              rgb_color:
                - 0
                - 110
                - 255
              brightness_pct: 100
              effect: Flash
            target:
              entity_id: "{{ light_entity }}"
          - delay:
              hours: 0
              minutes: 0
              seconds: 30
              milliseconds: 0
          - service: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: "{{ light_entity }}"
        alias: Roborock kein Wasser
    default:
      - service: light.turn_on
        metadata: {}
        data:
          rgb_color:
            - 55
            - 255
            - 0
          brightness_pct: 100
        target:
          entity_id: "{{ light_entity }}"
      - service: light.turn_on
        metadata: {}
        data:
          rgb_color:
            - 55
            - 255
            - 0
          brightness_pct: 100
        target:
          entity_id: "{{ light_entity }}"
      - alias: Carport Licht an, wenn dunkel
        if:
          - condition: numeric_state
            entity_id: sensor.bewegungsmelder_draussen_beleuchtungsstarke
            below: 25
        then:
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.schalter_carport_licht
      - delay:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0
      - service: light.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: "{{ light_entity }}"
      - delay:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0
      - service: switch.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: switch.schalter_carport_licht
description: ""
icon: mdi:alert-circle
fields:
  light_entity:
    selector:
      entity: {}
    name: light entity
    required: true
    default: light.nachtlicht_eingang_licht

hey @armedad thanks for this help :o)

Just for my understanding.
When calling the script by an automation I hand over the entity I want to shine.
This seems to be perfectly what I wanted. (will test your code this afternoon).

There is a little but:
One of the two entitties Im using in this script should flash, the other shouldnt.
Is there a way to hand this setting, too?

If this is a big deal, or would make the script unneccessary difficult, ofcourse I can live without the flashing on all lights.

the way I have done it, the entity that you pass in turn on and flash. The other light won’t get touched. is that what you wanted? apologies if I misunderstood

or are you saying that one of the lights when it is the one chosen, should turn color and flash, and the other, when chosen, should only turn color? If this is what you mean, I would add a new field that says flash, and invoke flash if requested by the caller

I see that you call turn_on. Then turn_on with flash. Do you always have to do that as two separate calls? Your light won’t turn on and flash in one call?

I want one light to turn on and flash and the other light to turn on without flashing.
So it would be awsome if you give me an example how to hand over the device and the flashing status to my script

I do two light.turn_on calls, because I dont think its possible to to turn on more than one light with different settings (flash and not flash) in this one call.

just noticed you had replied… when you reply, please hit the reply next to my post, not the blue button at the bottom of the thread. if you reply to my post instead of to the thread, i’ll get notified and be able to respond to you faster…

this automation is getting very long and unwieldy. it would be much better if you collapse a bunch of it… for example, at the end of each conditoin, you do a delay and then turn off the light. you could pull those out of each individual condition and just do that after the whole condition block. you could also just determine what color you want at the beginning then call to turn the light on and maybe flash using the color.

but without doing all that re-designing of your code, here’s how you can add a field that tells it whether to flash or not. there’s a new field required called should_flash

alias: Alles aus Test
sequence:
  - if:
      - condition: state
        entity_id: schedule.turbeleuchtungszeiten
        state: "off"
    then:
      - stop: ""
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: state
                entity_id: binary_sensor.alle_fenster
                state: "on"
              - condition: state
                entity_id: binary_sensor.alle_turen
                state: "on"
            alias: Fenster und Türen offen
        sequence:
          - service: light.turn_on
            metadata: {}
            data:
              rgb_color:
                - 255
                - 0
                - 0
              brightness_pct: 100
            target:
              entity_id: "{{ light_entity }}"
          - if:
              - condition: template
                value_template: "{{ should_flash == true }}"
            then:
              - service: light.turn_on
                metadata: {}
                data:
                  rgb_color:
                    - 255
                    - 0
                    - 0
                  brightness_pct: 100
                  effect: Flash
                target:
                  entity_id: "{{ light_entity }}"
          - delay:
              hours: 0
              minutes: 0
              seconds: 30
              milliseconds: 0
          - service: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: "{{ light_entity }}"
      - conditions:
          - alias: Licht an
            condition: or
            conditions:
              - condition: state
                entity_id: switch.alle_ladenlichter
                state: "on"
              - condition: state
                entity_id: switch.alle_kuchengerate
                state: "on"
        sequence:
          - service: light.turn_on
            metadata: {}
            data:
              rgb_color:
                - 255
                - 136
                - 0
              brightness_pct: 100
            target:
              entity_id: "{{ light_entity }}"
          - if:
              - condition: template
                value_template: "{{ should_flash == true }}"
            then:
              - service: light.turn_on
                metadata: {}
                data:
                  rgb_color:
                    - 255
                    - 136
                    - 0
                  brightness_pct: 100
                  effect: Flash
                target:
                  entity_id: "{{ light_entity }}"
          - delay:
              hours: 0
              minutes: 0
              seconds: 30
              milliseconds: 0
          - service: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: "{{ light_entity }}"
      - conditions:
          - condition: state
            entity_id: input_boolean.roborock_tiere_wasser_beigefullt
            state: "off"
          - condition: state
            entity_id: input_boolean.roborock_tiere_out_of_order
            state: "off"
        sequence:
          - service: light.turn_on
            metadata: {}
            data:
              rgb_color:
                - 0
                - 110
                - 255
              brightness_pct: 100
            target:
              entity_id: "{{ light_entity }}"
          - if:
              - condition: template
                value_template: "{{ should_flash == true }}"
            then:
              - service: light.turn_on
                metadata: {}
                data:
                  rgb_color:
                    - 0
                    - 110
                    - 255
                  brightness_pct: 100
                  effect: Flash
                target:
                  entity_id: "{{ light_entity }}"
          - delay:
              hours: 0
              minutes: 0
              seconds: 30
              milliseconds: 0
          - service: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: "{{ light_entity }}"
        alias: Roborock kein Wasser
    default:
      - service: light.turn_on
        metadata: {}
        data:
          rgb_color:
            - 55
            - 255
            - 0
          brightness_pct: 100
        target:
          entity_id: "{{ light_entity }}"
      - alias: Carport Licht an, wenn dunkel
        if:
          - condition: numeric_state
            entity_id: sensor.bewegungsmelder_draussen_beleuchtungsstarke
            below: 25
        then:
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.schalter_carport_licht
      - delay:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0
      - service: light.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: "{{ light_entity }}"
      - delay:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0
      - service: switch.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: switch.schalter_carport_licht
description: ""
icon: mdi:alert-circle
fields:
  light_entity:
    selector:
      entity: {}
    name: light entity
    required: true
    default: light.nachtlicht_eingang_licht
  should_flash:
    selector:
      boolean: {}
    name: should flash
    required: true
    default: false

Man! Thanks for your awsome help.
I just copied your code in my HomeAssistant and its looking exactly what I wanted.
Really really thank you!
Ofcourse I’ll redesign my code with your “make it easier” suggestions and tell my result here.

But before I do there is another question to your code.
If we look at turning on the light in condition 1 for example.
You turn on the light, check if it should flash and call turn on again.
Im not 100% sure, but Im quit sure that using the light.turn on on a light that is turned on already is the way to change its lightning behavior (making it flash in this case).
Wouldnt it be better to use sth like this (I can code this my own - just a question of understanding):
If “should_flash” == true
then light.turn_on - with flashing
else light.turn_on - without flashing

And if you allow I got one more request.
Im really not familiar with fields and variables in home assistant.
After your code example here and reading a bit about fields I guess I did get the basics and should be able to try and error (and google) to achieve what I want).

But Ive never used variables - what I suppose need to to get the color saved.
Can you give me an example with just a blanc script creating an “rgb_color” variable (or field if its more usefull) and assigning it a random rgb color, so that it can be used within the light.turn_on call?

ha! this is the question i was asking YOU when i asked this:

i don’t know what lights you are using, so i figured maybe your specific lights had to be turned on first before you can turn it on to flash. i think this came about because of how i edited your code initially (i did a global replace for your light entities to the new variable)… i didn’t realize that i had introduced this… i thought it was your original code.

so take a look at this… this does the “if/then/else” to remove to duplicate calls to light.turn_on

it also combines all the delay/turn_off to the end (this could be optimized further)

one note… of course i’m not able to test all of it, so i’m writing code blind… i may have a typo or such, but i hope this helps you get the right idea. i have a couple other questions/optmizations for you, but i’ll do that in another post

alias: Alles aus Test
sequence:
  - if:
      - condition: state
        entity_id: schedule.turbeleuchtungszeiten
        state: "off"
    then:
      - stop: ""
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: state
                entity_id: binary_sensor.alle_fenster
                state: "on"
              - condition: state
                entity_id: binary_sensor.alle_turen
                state: "on"
            alias: Fenster und Türen offen
        sequence:
          - if:
              - condition: template
                value_template: "{{ should_flash == true }}"
            then:
              - service: light.turn_on
                metadata: {}
                data:
                  rgb_color:
                    - 255
                    - 0
                    - 0
                  brightness_pct: 100
                  effect: Flash
                target:
                  entity_id: "{{ light_entity }}"
            else:
              - service: light.turn_on
                metadata: {}
                data:
                  rgb_color:
                    - 255
                    - 0
                    - 0
                  brightness_pct: 100
                target:
                  entity_id: "{{ light_entity }}"
      - conditions:
          - alias: Licht an
            condition: or
            conditions:
              - condition: state
                entity_id: switch.alle_ladenlichter
                state: "on"
              - condition: state
                entity_id: switch.alle_kuchengerate
                state: "on"
        sequence:
          - if:
              - condition: template
                value_template: "{{ should_flash == true }}"
            then:
              - service: light.turn_on
                metadata: {}
                data:
                  rgb_color:
                    - 255
                    - 136
                    - 0
                  brightness_pct: 100
                  effect: Flash
                target:
                  entity_id: "{{ light_entity }}"
            else:
              - service: light.turn_on
                metadata: {}
                data:
                  rgb_color:
                    - 255
                    - 0
                    - 0
                  brightness_pct: 100
                target:
                  entity_id: "{{ light_entity }}"            
      - conditions:
          - condition: state
            entity_id: input_boolean.roborock_tiere_wasser_beigefullt
            state: "off"
          - condition: state
            entity_id: input_boolean.roborock_tiere_out_of_order
            state: "off"
        sequence:
          - if:
              - condition: template
                value_template: "{{ should_flash == true }}"
            then:
              - service: light.turn_on
                metadata: {}
                data:
                  rgb_color:
                    - 0
                    - 110
                    - 255
                  brightness_pct: 100
                  effect: Flash
                target:
                  entity_id: "{{ light_entity }}"
            else:
              - service: light.turn_on
                metadata: {}
                data:
                  rgb_color:
                    - 0
                    - 110
                    - 255
                  brightness_pct: 100
                target:
                  entity_id: "{{ light_entity }}"
        alias: Roborock kein Wasser
    default:
      - service: light.turn_on
        metadata: {}
        data:
          rgb_color:
            - 55
            - 255
            - 0
          brightness_pct: 100
        target:
          entity_id: "{{ light_entity }}"
      - alias: Carport Licht an, wenn dunkel
        if:
          - condition: numeric_state
            entity_id: sensor.bewegungsmelder_draussen_beleuchtungsstarke
            below: 25
        then:
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.schalter_carport_licht
      - delay:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0
      - service: light.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: "{{ light_entity }}"
      - delay:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0
      - service: switch.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: switch.schalter_carport_licht
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - service: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: "{{ light_entity }}"
description: ""
icon: mdi:alert-circle
fields:
  light_entity:
    selector:
      entity: {}
    name: light entity
    required: true
    default: light.nachtlicht_eingang_licht
  should_flash:
    selector:
      boolean: {}
    name: should flash
    required: true
    default: false
1 Like

some addtional items for you.

  1. you have the lights flashing by having effect: flash, according to the documentation: Light - Home Assistant, flash is turned on by flash: slow or flash: fast but i suppose this might vary depending on what light you have. i don’t know (i don’t have any lights that do this).

  2. you could potentially simplify the code further by replacing:

          - if:
              - condition: template
                value_template: "{{ should_flash == true }}"
            then:
              - service: light.turn_on
                metadata: {}
                data:
                  rgb_color:
                    - 255
                    - 136
                    - 0
                  brightness_pct: 100
                  effect: Flash
                target:
                  entity_id: "{{ light_entity }}"
            else:
              - service: light.turn_on
                metadata: {}
                data:
                  rgb_color:
                    - 255
                    - 0
                    - 0
                  brightness_pct: 100
                target:
                  entity_id: "{{ light_entity }}"            

with this:

              - service: light.turn_on
                metadata: {}
                data:
                  rgb_color:
                    - 255
                    - 136
                    - 0
                  brightness_pct: 100
                  effect: >
                     {{ 'Flash' if should_flash == true else 'none' }}
                target:
                  entity_id: "{{ light_entity }}"

note that i’m not positive if passing ‘none’ in for effect will get rejected but i thought this format using template would simplify a lot if it were allowed.

  1. warning again, this is not tested because i don’t have light entities that can change colors or flash, but here’s the general idea for you for variables:
sequence:
  - variables:
      light_color: >
        {% if states('switch.alle_ladenlichter') == 'on' and
        states('switch.alle_kuchegerate') == 'on') %}
          [255, 136, 0]
        {% else %}
          [255, 0, 0]
        {% endif %}
  - service: light.turn_on
    data:
      rgb_color: |
        {{ light_color }}
      brightness_pct: 100
      effect: |
        {{ 'Flash' if should_flash == true else 'none' }}
    target:
      entity_id: "{{ light_entity }}"
fields:
  light_entity:
    selector:
      entity: {}
    name: light entity
    required: true
    default: light.nachtlicht_eingang_licht
  should_flash:
    selector:
      boolean: {}
    name: should flash
    required: true
    default: false

see how this can make things much simpler for you?

important notes.

  • variables are defined at the top of it’s scope block. no matter where you write them, they are defined at the top of their block (you can read the indent level as the block)
  • variables cannot be changed. so you can’t do an “if” later on and change the variable.

Really Thanks for your great explanation and tipps!

This is far too complicated (atleast for me as a beginner for now), to do it while im working.
Ill take a deep look into this at the weekend, where I can try and google without having people interrupting me.

To the two light.turn_ons.
In my original script, I always turn on BOTH lights.
The INSIDE lamp should NOT FLASH.
The OUTSIDE lamp should FLASH.
This is the reason for the two calls. There is no need to turn my light on and change settings then.

cool… holler if u have questions…there’s a lot to be said for being satisfied with just having something that works… but also some best learning in trying to optimize. if u are interested in the fun and learning, don’t be shy about experimenting and asking questions here. lots of us are happy to help… especially helping people who are curious and want to learn. cheers!