Advertisement
JLindvig

Untitled

Oct 16th, 2022 (edited)
1,534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 2.10 KB | None | 0 0
  1. alias: Halloween
  2. description: "Play creepy sounds in the driveway"
  3. trigger:
  4.   - platform: state
  5.     entity_id:
  6.      - binary_sensor.carport_sensor
  7.       - binary_sensor.havelage
  8.       - binary_sensor.mailbox
  9.     to: "on"
  10. condition:
  11.   - condition: time
  12.     after: "08:00:00"
  13.     before: "22:00:00"
  14. action:
  15.   - service: input_select.select_option
  16.     data:
  17.       option: >-
  18.         {# Pick a new random sound from the options #}
  19.         {% set ns = namespace(new_sound=state_attr('input_select.halloween_sounds', 'options') | random) -%}
  20.         {# In 10 tries check if the sound is in the list with 5 last played sounds #}
  21.         {% for i in range(1,10) -%}
  22.           {% if ns.new_sound in states('input_text.halloween_5_last_sounds') -%}
  23.             {# Pick a new random sound from the options #}
  24.             {% set ns.new_sound = state_attr('input_select.halloween_sounds', 'options') | random -%}
  25.           {% endif -%}
  26.         {% endfor -%}
  27.         {{ ns.new_sound }}
  28.     target:
  29.       entity_id: input_select.halloween_sounds
  30.   - service: media_player.play_media
  31.     target:
  32.       entity_id: media_player.carport
  33.     data:
  34.       media_content_id: >-
  35.         media-source://media_source/local/audio/{{ states('input_select.halloween_sounds') }}.mp3
  36.       media_content_type: audio/mpeg
  37.       enqueue: next
  38.   - service: input_text.set_value
  39.     data:
  40.       value: >-
  41.         {% set max = 5 %}
  42.         {% set ns = namespace(last_played = []) %}
  43.         {# Add the new sound in the start of the list #}
  44.         {% set last_played = [states('input_select.halloween_sounds')] + states('input_text.halloween_5_last_sounds').split(",") %}
  45.         {# Have we exceeded the range of sounds #}
  46.         {% if last_played | length > max %}
  47.           {# Truncate the list to  max allowed entries #}
  48.           {% for i in range(0, max) %}
  49.             {% set ns.last_played = ns.last_played + [last_played[i]] %}
  50.           {% endfor %}
  51.           {% set last_played = ns.last_played %}
  52.         {% endif %}
  53.         {# Join the list to a string separated with a ',' #}
  54.         {{ last_played | join(",") }}
  55.     target:
  56.       entity_id: input_text.halloween_5_last_sounds
  57. mode: queued
  58. max: 10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement