# Replace these entities with your own:
# - binary_sensor.laundry_washing_machine_lid_contact
# - input_boolean.washer_finished
# - timer.washer_reminder_timer
# - notify.mobile_app_*
# - group.family
# - tts.piper
# - media_player.*

alias: Laundry - Washer - Reminder Loop
description: >
  Repeats reminders while the washer is marked as finished and the lid remains
  closed.

mode: queued
max: 10

variables:
  quiet_hours: >
    {% set start = states('input_datetime.quiet_hours_start') %}
    {% set end = states('input_datetime.quiet_hours_end') %}
    {% set now_time = now().strftime('%H:%M:%S') %}
    {% if start <= end %}
      {{ start <= now_time <= end }}
    {% else %}
      {{ now_time >= start or now_time <= end }}
    {% endif %}

trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.washer_reminder_timer
    id: reminder_timer_completed

condition:
  - condition: state
    entity_id: input_boolean.washer_finished
    state: "on"

  - condition: state
    entity_id: binary_sensor.laundry_washing_machine_lid_contact
    state: "off"

action:
  - service: notify.mobile_app_steve
    data:
      title: Washing machine reminder
      message: The washing machine is finished.

  - choose:
      - conditions:
          - condition: state
            entity_id: group.family
            state: home

          - condition: template
            value_template: "{{ not quiet_hours }}"
        sequence:
          - service: tts.speak
            target:
              entity_id: tts.piper
            data:
              cache: true
              media_player_entity_id: media_player.dining_speaker_2
              message: The washing machine is finished.

  - service: timer.start
    target:
      entity_id: timer.washer_reminder_timer
    data:
      duration: "00:10:00"