Simple ... flashing light's via on off and delay. Restore previous light states

This is a simple blueprint for flashing light’s via on/off and delay. Activated and deactived via input_boolean and restore previous light state.

Link to Blueprint: https://github.com/myGithub-Markus/home-assistant_blueprints/blob/main/flashing_lights.yaml

New features added:

  • optional light scene
  • individual activated sensor and sensor states
  • activated timeout
blueprint:
  name: flashing lights
  description: >
    Flashing lights via on off and delay and finally restore the previous light states.
  domain: automation
  input:
    activated:
      name: Sensor
      description: "Sensor to activate or deactivate flashing lights."
      selector:
        entity:
    activated_states:
      name: States to match with activated sensor (optional)
      description: "A comma separated list of activated states."
      default: "on"
    activated_timeout:
      name: (OPTIONAL) activated timeout
      description: "Max time to leave flashing light during activated sensor state is true. '0' means no timeout is defined."
      default: 0
      selector:
        number:
          min: 1
          max: 3600
          unit_of_measurement: seconds
    light_target:
      name: Light
      description: "To using lights"
      selector:
        target:
          entity:
            domain: light
    scene_target:
      name: Scene
      default: scene.none
      description: "To using scene"
      selector:
        entity:
          domain: scene
    delay:
      name: (OPTIONAL) Delay
      description: "Delay flashing lights"
      default: 1000
      selector:
        number:
          min: 0
          max: 20000
          unit_of_measurement: milliseconds

mode: single
max_exceeded: silent

trigger:
  - platform: state
    entity_id: !input activated

variables:
  activated: !input activated
  activated_states_str: !input activated_states
  activated_states: "{{ activated_states_str.split(',') | map('trim') | list }}"
  activated_timeout: !input activated_timeout
  delay: !input delay
  scene_target: !input scene_target

condition: []

action:
  - service: scene.create
    data:
      scene_id: all_lights_snapshot
      snapshot_entities: >
        {{ states.light | map(attribute='entity_id') | join(',') }}
  - choose:
      - conditions:
          - "{{ scene_target == 'scene.none' }}"
        sequence:
          - service: light.turn_off
            target: !input light_target
          - repeat:
              while:
                - condition: template
                  value_template: >
                    {% set time_now = as_timestamp(now()) %}
                    {% set activated_last_changed = float(states[activated].last_changed.timestamp(),0) %}
                    {% set is_timed_out = false %}
                    {% if activated_timeout > 0 %}
                    {% set is_timed_out = (time_now - activated_last_changed) > activated_timeout %}
                    {% endif %} 
                    {% set activated_state = 'unknown' %}
                    {% if activated is defined %}
                    {% set activated_state = states(activated) %}
                    {% endif %} 
                    {{ activated_state in activated_states and not is_timed_out }}
              sequence:
                - delay:
                    milliseconds: >
                      {{ delay }}
                - service: light.toggle
                  target: !input light_target
          - service: scene.turn_on
            data:
              entity_id: scene.all_lights_snapshot
      - conditions:
          - "{{ scene_target != 'scene.none' }}"
        sequence:
          - repeat:
              while:
                - condition: template
                  value_template: >
                    {% set time_now = as_timestamp(now()) %}
                    {% set activated_last_changed = float(states[activated].last_changed.timestamp(),0) %}
                    {% set is_timed_out = false %}
                    {% if activated_timeout > 0 %}
                    {% set is_timed_out = (time_now - activated_last_changed) > activated_timeout %}
                    {% endif %} 
                    {% set activated_state = 'unknown' %}
                    {% if activated is defined %}
                    {% set activated_state = states(activated) %}
                    {% endif %} 
                    {{ activated_state in activated_states and not is_timed_out }}
              sequence:
                - service: scene.turn_on
                  entity_id: !input scene_target
                - delay:
                    milliseconds: >
                      {{ delay }}
                - service: light.turn_off
                  target: !input light_target
                - delay:
                    milliseconds: >
                      {{ delay }}
          - service: scene.turn_on
            data:
              entity_id: scene.all_lights_snapshot

Preview Blueprint Automation GUI

8 Likes

This is very useful, thank you!

I installed it and noticed that it only works with entity types : light. I adapted this to show also type: switch but it did not work (although the entities -> switch can be selected). Any clue what more we need to adapt to the script? Thanks in advance!

1 Like

Could this be set to select a red colour and flash it?

1 Like

Could. When it wouldn’t be buggy.

I’m still pretty new but under “Activated” I cant choose anything to trigger the flashing. I want to choose my doorbell here.

same. nothing is showing up on mine either.

the current blueprint only supports input_boolean under “Activated”. So if you configured any input_boolean it should be available under Activated.

input_boolean:
  flashing_lights:
    name: Activated and deactivated flashing lights
- id: '4711'
  alias: flashing lights
  description: ''
  use_blueprint:
    path: .../flashing_lights.yaml
    input:
      delay: '1000'
      activated: input_boolean.flashing_lights
      target_lights:
        entity_id: light.all_indoor

Brilliant - thank you!

Many thanks for it! it’s very useful for me since I’m deaf and I use this to trigger light when the doorbell is pressed.

If I understand the flashing count is based on how long the status is “on” + delay?

However is there a possibility to configure counts of flashing because I would love to determine source/trigger based on count of flashing.
For example:
Doorbell = 3 times
Fire alarm = 6 times
burglar alarm = 10 times

7 Likes

Markus78 I use your blueprint choosing a device to activate the flashing light. It worked but the only way to stop it was stopping the automation. Then I create an input_boolean and modified the automation including also the delay. No results. Can you please help me to understand how it works and how to fix it in order to stop after a certain time or event? Thanks

@markus78, I have one idea to work around the issue of restoring light states when some lights are off:

  1. Save the state as scene_old.
  2. Turn on all the lights configured in the automation.
  3. Wait a short delay to make sure they are turned on.
  4. Save the state as scene_on.
  5. Continue the blinking light automation/blueprint as normal. Go nuts!
  6. Wait a short delay before activating any scene.
  7. Activate scene_on.
  8. Wait another short delay before activating another scene.
  9. Activate scene_old.
  10. END

This should make sure the “off” state is correctly saved and restored, because the on/off state is saved first, then the light properties are saved next; and they are restored in reverse order. In theory, because I haven’t tested it myself (and I likely won’t test any time soon). So, if this solution sounds plausible, please test if it works.

My only suggestion is the “short delay” should be an input variable with a default value. The default delay should work for most people, but being a parameter lets each person to fine-tune it to their setup.

I added a RGB name and brigthness selector to the blueprint for those who would like to use it on LED RGB lights. The RGB works fine, not sure about the brightness, still have to do some tests.

blueprint:
  name: flashing lights
  description: 'Flashing lights via on off and delay and finally restore the previous
    light states.

    '
  domain: automation
  source_url: https://community.home-assistant.io/t/simple-flashing-lights-via-on-off-and-delay-restore-previous-light-states/258099
  input:
    activated:
      name: Activated
      description: Enter the Entity Name for Trigger (On-Off)
      selector:
        entity:
          domain: input_boolean
    target_lights:
      name: Lights
      description: To flashing lights
      selector:
        target:
          entity:
            domain: light
    color_rgb:
      name: RGBColor
      description: Choose the color for blinking lights
      selector:
        select:
          options:
            - white   
            - red
            - green
            - lime
            - blue
            - navy
            - yellow
            - orange
            - turquoise
            - violet
            - magenta
            - aliceblue
            - antiquewhite
            - aquamarine
            - aqua
            - cadetblue
            - coral
            - cornflowerblue
            - crimson
            - darkblue
            - darkorange
            - darkorchid
            - darksalmon
            - darkslategray
            - deeppink
            - darkviolet
            - deepskyblue
            - ghostwhite
            - gold
            - lawngreen
            - lightcoral
            - lightgreen
            - lightseagreen
    brightness_rgb:
      name: (OPTIONAL) Light Brightness
      description: Color Brigthness between 0 to 255
      selector:
        number:
          min: 0
          max: 255
          unit_of_measurement: RGB
          step: 1.0
          mode: slider
    delay:
      name: (OPTIONAL) Delay
      description: Delay flashing lights
      default: 1000
      selector:
        number:
          min: 0.0
          max: 20000.0
          unit_of_measurement: milliseconds
          step: 1.0
          mode: slider
mode: restart
max_exceeded: silent
trigger:
- platform: state
  entity_id: !input 'activated'
  to: 'on'
variables:
  activated: !input 'activated'
  delay: !input 'delay'
  target_lights: !input 'target_lights'
condition: []
action:
- service: scene.create
  data:
    scene_id: all_lights_snapshot
    snapshot_entities: "{% set lights = states.light\n  | map(attribute='entity_id')\
      \ | join(',') %}\n  {{lights}}\n"
- service: light.turn_on
  target: !input 'target_lights'
  data:
    color_name: !input 'color_rgb'
    brightness: !input 'brightness_rgb'
- repeat:
    while:
    - condition: template
      value_template: '{{ activated == none or is_state(activated, ''on'') }}'
    sequence:
    - delay:
        milliseconds: '{{ delay }}'
    - service: light.toggle
      target: !input 'target_lights'
- service: scene.turn_on
  data:
    entity_id: scene.all_lights_snapshot

You can add whatever color names you like (as long as they are CSS color names). A full list of color names can be found here (make sure they are written small caps).

CSS Color Names

Cheers!

4 Likes

Tried this, but it doesn’t display and trigger entity’s for me. The dropdown list is empty. Any suggestions?

You’re probably trying to call “switches” not “lights”. For switches to show up in the selector, you need to define the switches as lights first. You can do so in the configuration.yaml in a very simple manner. See code below. Just add the entity name of your switches and under name you can add whatever you want. It’s probably a good idea to add all your switches here which are in fact switches that turn on lights. Test your config and then restart your server and the entities should show up in scripts or automations aso that need lights as entity or device inputs. Hope that helps!

# Tell Home Assistant that the following switches are lights, so they appear in script and other GUIs
light:
  - platform: switch
    name: Enter Name Here01
    entity_id: switch.sonoff_yourswitchentity01
  - platform: switch
    name: Enter Name Here02
    entity_id: switch.sonoff_yourswitchentity02
  - platform: switch
    name: Enter Name Here03
    entity_id: switch.sonoff_yourswitchentity03

That should do the trick.

Hope you have a great day!
Cheers!

3 Likes

Hi, great suggestion! Having the same issue, could you please provide more instructions on how i can do this? (fairly new to it all)

I think you can start with the official documentation: Automating Home Assistant - Home Assistant

Great Blueprint @markus78 - I can see this being used in many of my automations.

With the idea of flashing my LED strip when the doorbell rings, am I correct in my thinking such that I need an automation to set my input boolean to “on” when the doorbell is pressed, eg:

alias: Set Doorbell_Pressed = ON when the doorbell is pressed
description: ''
trigger:
  - platform: device
    device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    domain: nest
    type: doorbell_chime
condition: []
action:
  - service: input_boolean.turn_on
    target:
      entity_id: input_boolean.doorbell_pressed
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - service: input_boolean.turn_off
    target:
      entity_id: input_boolean.doorbell_pressed
mode: single

This seems to successfully change the boolean value to ON, and keeps it on for 15 seconds before changing it back to OFF.

Then, I have another automation which is your blueprint that is triggered when my input_boolean.doorbell_pressed has changed to ON.

This seems to work well - when the doorbell is pressed, the lights flash on and off for 15 seconds, and the initial on/off state of the light is preserved, great! Can you confirm that this is the correct usage? Or did you design it in a way that could simplify my implementation?

Nice addition @avviano - I’m now able to specify that I would like my LED strip to flash specifically in RED.

Is it possible to use alternating colour into this? So for example to accommodate choosing 2 RGB colours, and respective Brightness. Then when the condition is triggered, it alternates (e.g. RED then BLUE and then continue the repeat / while block)

I’ll be honest, as a yaml noob, I thought I’d have a try but gave up after 10 minutes :frowning: - is this a possible feature that can be edited?

Hi, it may be possible to make the blinking turn on after a certain second interval?

@avviano & @markus78 - great work on this. It’s my first use of blueprints, and it looks like it could be very useful! I am intending to use this to ‘alert’ of different calls to action - ie doorbell/answer the front/back door, a meal is ready so come to eat, etc etc. Each circumstance uses lights which are already possibly in use.

I have found that if the lights are already on, and the boolean is turned off when the flashing is in the ‘off’ state, all works as it should.

However if the boolean is turned off when the flasher is in the ‘on’ state, the scene snapshot return doesn’t work, and the lights stay off - although the gui shows the light as having successfully returned to the snapshotted scene.

Also, if the lights are off but set to (eg) yellow, when a red flasher blueprint finishes, when the light is next turned on it will be red, not the previous colour.

Have i misunderstood the usage of this blueprint?

1 Like