State for washing machine based on power

Hi,

I’m new to Home Assistant and I’m trying to create some sensors for the washing machine based on the power provided by a smart plug.

I’m trying to have two sensors.

  • One for the washer with on/off statuses if the power > 0 and only if the status didn’t changed for 45 seconds.
  • Then the second sensor to have multiple statuses. Running (when the main sensor is on), finished for 30 seconds since the main sensor switched to off and none if the main switch is off and the 30 seconds passed.

What I did:

template:  
  - trigger:
      - platform: state
        entity_id: sensor.washer_power
        for: "00:00:45"
    sensor:
      - name: Washer
        unique_id: washer
        state: >
          {% if states('sensor.washer_power')|int > 0 %}
            on
          {% else %}
            off
          {% endif %}
  - sensor:
    - name: Washer Job State
      unique_id: washer_job_state
      state: >
        {% if is_state('sensor.washer', 'unknown') %}
        unknown
        {% elif is_state('sensor.washer', 'on') %}
        running
        {% elif as_timestamp(now()) - as_timestamp(states.sensor.washer.last_changed) <= 30 %}
        finished
        {% else %}
        none
        {% endif %}

Now I have two problems and don’t know how to resolve them.

  • I want that the delay (45 seconds) of the main sensor to apply only if power goes to 0 (just a nice to have)
  • When the main sensor is unavailable and then change to off the Job State sensor will change the state to finished. I don’t know how to check if the previous state was on together with the last_change

Thank you for the help in advance :slight_smile:

Found this when trying to solve a different problem. Do you still need help?

Hei. Thank you! I have solved it :slight_smile:

I have forgot about this post, thank you for reminding me so I can post the solution, maybe it will help others.

template:
  - trigger:
    - platform: state
      entity_id: sensor.washer_power
    binary_sensor:
      - name: Washer
        unique_id: washer
        state: "{{ states('sensor.washer_power')|float(0) > 0 }}"
        delay_on: "00:00:10"
        delay_off: "00:01:00"
        attributes:
          from: "{{ trigger.from_state.state }}"

  - sensor:
    - name: Washer Job State
      unique_id: washer_job_state
      state: >-
        {% if is_state('binary_sensor.washer', 'on') %}
          cleaning
        {% elif is_state('binary_sensor.washer', 'off') and not is_state_attr('binary_sensor.washer', 'from', 'unknown') and (as_timestamp(now()) - as_timestamp(states.binary_sensor.washer.last_changed) <= 30) %}
          finished
        {% else %}
          none
        {% endif %}

Hey guys,

I like to solve such problems following the classic state machine approach. There are more compact solutions to this, but this one serves it’s purpose well, is super clean, and easily extendable.
Yes I know that the use of input_select is a bit unclean. I would do it differently today.

input_select:
  waschmaschine_opstate:
    name: Waschmaschine OpState
    icon: mdi:washing-machine
    options:
      - "Off"
      - "Standby"
      - "Active"
      - "Finished"
    initial: "Off"

################################################################################

## Stephan Waschmaschine AEG
## 0.06 W      = Off
## 4.3..6.3 W = Standby
## Bis 2000 W  = Active

automation:
  - id: "1609362853001"
    alias: Regel Waschmaschine verbraucht ganz wenig (Off)
    trigger:
      - platform: numeric_state
        entity_id: sensor.waschmaschine_schaltmessmodul_power
        below: 0.1
        for: "00:03:00"
    condition:
      condition: not
      conditions:
        - condition: state
          entity_id: input_select.waschmaschine_opstate
          state: "Off"
    action:
      - service: input_select.select_option
        data:
          entity_id: input_select.waschmaschine_opstate
          option: "Off"

  - id: "1609362853002"
    alias: Regel Waschmaschine verbraucht 4.3W (Standby)
    trigger:
      - platform: numeric_state
        entity_id: sensor.waschmaschine_schaltmessmodul_power
        above: 4.0
        below: 7.0
        for: "00:01:00"
    condition:
      condition: state
      entity_id: input_select.waschmaschine_opstate
      state: "Off"
    action:
      - service: input_select.select_option
        data:
          entity_id: input_select.waschmaschine_opstate
          option: "Standby"

  - id: "1609362853003"
    alias: Regel Waschmaschine verbraucht 4.3W (Finished)
    trigger:
      - platform: numeric_state
        entity_id: sensor.waschmaschine_schaltmessmodul_power
        above: 4.0
        below: 7.0
        for: "00:01:00"
    condition:
      condition: state
      entity_id: input_select.waschmaschine_opstate
      state: "Active"
    action:
      - service: input_select.select_option
        data:
          entity_id: input_select.waschmaschine_opstate
          option: "Finished"

  - id: "1609362853004"
    alias: Regel Waschmaschine verbraucht >5W (Active)
    trigger:
      - platform: numeric_state
        entity_id: sensor.waschmaschine_schaltmessmodul_power
        above: 7.0
        for: "00:01:00"
    condition:
      condition: not
      conditions:
        - condition: state
          entity_id: input_select.waschmaschine_opstate
          state: "Active"
    action:
      - service: input_select.select_option
        data:
          entity_id: input_select.waschmaschine_opstate
          option: "Active"
1 Like