# Replace these entities with your own:
# - sensor.laundry_washing_machine_plug_power
# - binary_sensor.laundry_washing_machine_vibration
# - input_number.washer_finish_power_threshold
# - input_boolean.washer_running
# - input_boolean.washer_finished
# - timer.washer_finish_delay
# - timer.washer_reminder_timer
# - notify.mobile_app_*
# - group.family
# - tts.piper
# - media_player.*

alias: Laundry - Washer - Finish Detection
description: >
  Starts a finish delay when power is low and vibration has stopped. If the
  washer is still idle when the timer completes, it marks the wash as finished.

mode: queued
max: 10

variables:
  power: "{{ states('sensor.laundry_washing_machine_plug_power') | float(0) }}"
  vibrating: "{{ is_state('binary_sensor.laundry_washing_machine_vibration', 'on') }}"
  finish_threshold: "{{ states('input_number.washer_finish_power_threshold') | float(0) }}"
  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: numeric_state
    entity_id: sensor.laundry_washing_machine_plug_power
    below: input_number.washer_finish_power_threshold
    id: power_fell_below_finish_threshold

  - platform: state
    entity_id: binary_sensor.laundry_washing_machine_vibration
    to: "off"
    for: "00:01:30"
    id: vibration_cleared_for_1m30s

  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.washer_finish_delay
    id: finish_timer_completed

condition: []

action:
  - choose:
      - alias: Start finish timer
        conditions:
          - condition: trigger
            id:
              - power_fell_below_finish_threshold
              - vibration_cleared_for_1m30s

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

          - condition: template
            value_template: "{{ power < finish_threshold and not vibrating }}"
        sequence:
          - service: timer.start
            target:
              entity_id: timer.washer_finish_delay
            data:
              duration: "00:10:00"

      - alias: Mark as finished and notify
        conditions:
          - condition: trigger
            id: finish_timer_completed

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

          - condition: template
            value_template: "{{ power < finish_threshold and not vibrating }}"
        sequence:
          - service: input_boolean.turn_off
            target:
              entity_id: input_boolean.washer_running

          - service: input_boolean.turn_on
            target:
              entity_id: input_boolean.washer_finished

          - service: notify.mobile_app_steve
            data:
              title: Washing machine finished
              message: The washing machine has 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 has finished.

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