Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- alias: Halloween
- description: "Play creepy sounds in the driveway"
- trigger:
- - platform: state
- entity_id:
- - binary_sensor.carport_sensor
- - binary_sensor.havelage
- - binary_sensor.mailbox
- to: "on"
- condition:
- - condition: time
- after: "08:00:00"
- before: "22:00:00"
- action:
- - service: input_select.select_option
- data:
- option: >-
- {# Pick a new random sound from the options #}
- {% set ns = namespace(new_sound=state_attr('input_select.halloween_sounds', 'options') | random) -%}
- {# In 10 tries check if the sound is in the list with 5 last played sounds #}
- {% for i in range(1,10) -%}
- {% if ns.new_sound in states('input_text.halloween_5_last_sounds') -%}
- {# Pick a new random sound from the options #}
- {% set ns.new_sound = state_attr('input_select.halloween_sounds', 'options') | random -%}
- {% endif -%}
- {% endfor -%}
- {{ ns.new_sound }}
- target:
- entity_id: input_select.halloween_sounds
- - service: media_player.play_media
- target:
- entity_id: media_player.carport
- data:
- media_content_id: >-
- media-source://media_source/local/audio/{{ states('input_select.halloween_sounds') }}.mp3
- media_content_type: audio/mpeg
- enqueue: next
- - service: input_text.set_value
- data:
- value: >-
- {% set max = 5 %}
- {% set ns = namespace(last_played = []) %}
- {# Add the new sound in the start of the list #}
- {% set last_played = [states('input_select.halloween_sounds')] + states('input_text.halloween_5_last_sounds').split(",") %}
- {# Have we exceeded the range of sounds #}
- {% if last_played | length > max %}
- {# Truncate the list to max allowed entries #}
- {% for i in range(0, max) %}
- {% set ns.last_played = ns.last_played + [last_played[i]] %}
- {% endfor %}
- {% set last_played = ns.last_played %}
- {% endif %}
- {# Join the list to a string separated with a ',' #}
- {{ last_played | join(",") }}
- target:
- entity_id: input_text.halloween_5_last_sounds
- mode: queued
- max: 10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement