Code not elegant

Hi, I’m sure it is possible to short this code, but didn’t find how. Maybe someone can help.

This scripts turns a hue on and off. Switching from red to orange

blink_kugellicht:
  alias: Alarm Kugellicht
  sequence:
  - alias: Alarm start
    data:
      brightness_pct: 100
      color_name: orange
      entity_id: light.kugellicht
    service: light.turn_on
  - delay:
      seconds: 1
  - alias: Alarm stop
    data:
      entity_id: light.kugellicht
    service: light.turn_off
  - delay:
      seconds: 1
  - alias: Alarm start
    data:
      brightness_pct: 100
      color_name: red
      entity_id: light.kugellicht
    service: light.turn_on
  - delay:
      seconds: 1
  - alias: Alarm stop
    data:
      entity_id: light.kugellicht
    service: light.turn_off
  - delay:
      seconds: 1
  - alias: Alarm start
    data:
      brightness_pct: 100
      color_name: orange
      entity_id: light.kugellicht
    service: light.turn_on
  - delay:
      seconds: 1
  - alias: Alarm stop
    data:
      entity_id: light.kugellicht
    service: light.turn_off
  - delay:
      seconds: 1
  - alias: Alarm start
    data:
      brightness_pct: 100
      color_name: red
      entity_id: light.kugellicht
    service: light.turn_on
  - delay:
      seconds: 1
  - alias: Alarm stop
    data:
      entity_id: light.kugellicht
    service: light.turn_off
  - delay:
      seconds: 1
  - alias: Alarm start
    data:
      brightness_pct: 100
      color_name: orange
      entity_id: light.kugellicht
    service: light.turn_on
  - delay:
      seconds: 1
  - alias: Alarm stop
    data:
      entity_id: light.kugellicht
    service: light.turn_off
  - delay:
      seconds: 1
  - alias: Alarm start
    data:
      brightness_pct: 100
      color_name: red
      entity_id: light.kugellicht
    service: light.turn_on
  - delay:
      seconds: 1
  - alias: Alarm stop
    data:
      entity_id: light.kugellicht
    service: light.turn_off

Please post properly using the method at the top of the page.

Maybe you could just use the “flash” function in the light-component?

I already tried that, but I want it to end after X blinks without running another script. I think it will be something like “for i in range”, but I don’t know if or how this works in ha.
Also the flash function does not switch color.

I want this as an alarm for my CO2 values of netatmo

Sorry bud but mimicking a loop as a sequence is not elegant at all. The route you went is a good option. Your other option is to build a python script that handles this.

This script should work. Follow the directions located here for placing the script. Name it alarm_light_loop.

count = data.get('count')
color = data.get('color_name')
brightness = data.get('brightness')
entity_id = data.get('entity_id ')
delay = data.get('delay')

def tryConvert(name, what, towhat, default):
    if what:
        try:
            return towhat(what)
        except ValueError:
            logger.warning("{}: cannot convert {} to {} using {}.".format(name, what, str(towhat), default)
            return default
    else:
        return default

count = tryConvert('count', count, int, None)
brightness = tryConvert('brightness', brightness, int, 255)
delay = tryConvert('delay', delay, float, 1.0)

if not color:
    color = 'white'

state = hass.states.get(entity_id)

if state and count: #make sure the entity_id exists and the count is greater than 0.
    on_service_data = { 'entity_id': entity_id, 'color_name': color, 'brightness': brightness }
    off_service_data = { 'entity_id': entity_id }
    for i in range(count):
        hass.services.call('light', 'turn_on', on_service_data, True)
        time.sleep(delay)
        hass.services.call('light', 'turn_on', off_service_data, True)
        time.sleep(delay)
else:
    logger.warning("Did not execute loop.")

then your script would be:

blink_kugellicht:
  alias: Alarm Kugellicht
  sequence:
    - service: python_script.alarm_light_loop
      data:
          brightness_pct: 100
          color_name: orange
          entity_id: light.kugellicht
          delay: 1
          count: 6
1 Like