Vibration/ Flash patterns Script

This script allows you to pass an entity and a “vibration pattern” (similar to the android mobile notification and Tasker vibration patterns, but the first delay is the on time in milliseconds instead of off first). The script doesn’t care if this is a buzzer or not, so it will also work with a light. The entity just needs an on and off state.

note: If you use this script for WIFI (and possibly other wireless devices), don’t expect the delays to be accurate as WIFI has inherent delays in communication time. So each event will have an additional slight delays and they will be random. I am using this for a buzzer directly wired to my HA raspberry pi using GPIO pins and a relay board, so there is almost no delay

buzzer_notify:
  description: 'Buzzer Notify Pattern'
  fields:
    buzzer_pattern:
      description: 'On, Off timing in milliseconds'
      example: 'Three beeps: 100, 50, 100, 50, 100'
    entity: 
      description: 'entity to toggle on/off'
      example: 'switch.buzzer'
  variables:
    delay_list: >-
      {{ buzzer_pattern.split(",") }}
    initial_entity_state: >-
      {% if is_state(entity, 'on') %}
        {{'on'}}
      {% else %}
        {{'off'}}
      {% endif %}
  sequence:
    - repeat:
        for_each: >-
          {{ delay_list }}
        sequence:        
          - variables:
              on_off: >-
                {% if repeat.index | int % 2 == 0 %}
                  {{ 'on' }}
                {% else %}
                  {{ 'off' }}
                {% endif %}
          - service: >-
              {{ 'homeassistant.turn_' + on_off }}
          
            target:
              entity_id: >-
                {{ entity }}
          - delay:
              milliseconds: >-
                {{ repeat.item | int(1) }}
    - service: >-
        {{ 'homeassistant.turn_' + initial_entity_state }}
    
      target:
        entity_id: >-
          {{ entity }}

call via script service example:

service: script.buzzer_notify
data:
  buzzer_pattern: "100, 50, 100, 50, 100, 50, 100, 50, 100, 50, 100, 50, 100, 50, 100"
  entity: switch.side_door_buzzer
1 Like

If you’re interested, you can shorten several of the templates.

buzzer_notify:
  description: 'Buzzer Notify Pattern'
  fields:
    buzzer_pattern:
      description: 'On, Off timing in milliseconds'
      example: 'Three beeps: 100, 50, 100, 50, 100'
    entity: 
      description: 'entity to toggle on/off'
      example: 'switch.buzzer'
  variables:
    initial_entity_state: '{{ states(entity) }}'
  sequence:
    - repeat:
        for_each: "{{ buzzer_pattern.split(',') }}"
        sequence:
          - service: "homeassistant.turn_{{ iif(repeat.index | int(0) % 2 == 0, 'on', 'off') }}"
            target:
              entity_id: '{{ entity }}'
          - delay:
              milliseconds: '{{ repeat.item | int(1) }}'
    - service: 'homeassistant.turn_{{ initial_entity_state }}'
      target:
        entity_id: '{{ entity }}'
1 Like