Washing Machine Automation

The task

We have a Miele washing machine that does not have delay start, the objective is to create some automation flows to automate this. The goal would be to set a desired finish time and have the washing machine be done by then.

The problem

  • No delayed start
  • No interface to the washing machine itself

The solution

The solution is to use home assistants components and automations to allow this, together with a lovelace page for configuring / user input.

Components

Inputs

These are the inputs for our automations, we will need all of these

# Desired endtime
# file: input_datetime.yml
washing_machine_endtime:
  name: Washing Machine EndTime
  has_date: false
  has_time: true

# Component to set cycle time
# file input_number.yaml
washing_machine_time:
  name: "Washing Machine Cycle Time"
  min: 0.5
  max: 3
  step: 0.25
  unit_of_measurement: "h"

# Component to Arm Washing Machine
# file: input_boolean.yml
washing_machine_armed:
  name: Arm Washing Machine

# Shelly Plug for washing machine
# file: switch.yml
- platform: mqtt
  name: Washing Machine
  unique_id: shellyplug-s-XXXXXX
  command_topic: 'shellies/shellyplug-s-XXXXXX/relay/0/command'
  state_topic: 'shellies/shellyplug-s-XXXXXX/relay/0'
  payload_on: "on"
  payload_off: "off"
  state_on: "on"
  state_off: "off"

# Shelly Plug power
- platform: mqtt
  state_topic: 'shellies/shellyplug-s-XXXXXX/relay/0/power'
  name: 'Washing Machine Power'
  unit_of_measurement: 'W'

Lovelace Settings Dashboard

  - type: entities
    state_color: true
    entities:
    - entity: input_number.washing_machine_time
    - entity: input_datetime.washing_machine_endtime
    - entity: input_boolean.washing_machine_armed
    show_header_toggle: false
    title: Washing Machine Settings
  - type: markdown
    content: >-
      # Instructions

      ## Setting the Time and Activating

      1. Set requested end time, when you want the washing to be done by

      2. Set the cycle time

      3. Make sure to arm Washing Machine

      4. Start the washing machine

      5. Verify that the washing machine plug turns off

      ## Notes

      - The washing machine switch will turn on to aim to finish at the requested
      finish time

      - Manually turning on the washing machine switch is possible to override the
      start time

      - The Washing Machine Armed toggle ensure the automation to turn it off when
      the program has been selected and started runs.

Automations

alias: Turn off washing machine on cycle start when armed
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.washing_machine_power
    for:
      hours: 0
      minutes: 0
      seconds: 2
    above: 5
condition:
  - condition: state
    entity_id: input_boolean.washing_machine_armed
    state: "on"
action:
  - service: homeassistant.turn_off
    data: {}
    target:
      entity_id: switch.washing_machine
mode: single

alias: Turn off washing machine armed when washing machine turns on
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.washing_machine
    to: "on"
condition: []
action:
  - service: homeassistant.turn_off
    data: {}
    target:
      entity_id: input_boolean.washing_machine_armed
mode: single

Pyscript for easier logic

Some logic is easier in https://hacs-pyscript.readthedocs.io/en/latest/

import re
import datetime

def delta_until_end():
    (hour, minute) = input_datetime.washing_machine_endtime.split(':')[0:2]
    target = datetime.datetime.now().replace(hour=int(hour), minute=int(minute))
    return target - datetime.datetime.now()

@time_trigger("cron(* * * * *)")
@state_active("switch.washing_machine == 'off'")
def washing_machine():
    hours = float(input_number.washing_machine_time)
    hours = datetime.timedelta(hours=hours)
    d = delta_until_end()

    log.info(f'Time left: {d}, Hours: {hours}')
    if hours >= d and d.total_seconds() > 0:
        homeassistant.turn_on(entity_id='switch.washing_machine')