Close

Before and after

A project log for Laundry monitoring

A collection of notes and pointers for the usual laundry monitoring project.

wjcarpenterWJCarpenter 08/19/2023 at 18:010 Comments

I set up an action to send a notice to the Home Assistant app on my phone when the laundry automation runs. The message reports the washer and dryer states. I noticed right away that I was getting some notifications when neither of those states changed. I'm not completely sure, but I think that happens because the contact sensors on the washer and dryer doors periodically wake up and send their states, and I theorize that that's so they can report their battery levels occasionally even if they haven't been triggered by opening or closing.

To get rid of these spurious notifications, I wanted to define some variables in the automation: washer_before, washer_after, dryer_before, and dryer_after. I could then use a condition to only do notifications if either of the pairs showed a state change. I had a dickens of a time figuring out the right syntax for this. An ambitious person would deeply study the Home Assistant documentation, along with callouts to various YAML and Jinja template documents. Instead, I used the tried and true method of googling around and trial and error. The Home Assistant automation trace UI is very helpful for that kind of experimental approach.

Recall that the state changes are done in a massive "choose" action. I set the "before" variables just before that and the "after" variables just after that. Here's the before action; the after action is similar in the obvious way. I also do a logbook entry for diagnostic purposes.

- variables:
    washer_before: "{{ states('input_select.washer_state') }}"
    dryer_before: "{{ states('input_select.dryer_state') }}"
- service: logbook.log
  data:
    name: Laundry
    message: BEFORE washer {{ washer_before }}; dryer {{ dryer_before }}

 The condition and notification actions then looks like this:

- condition: template
  value_template: "{{ washer_before != washer_after or dryer_before != dryer_after }}"
- service: notify.mobile_app_sm_g981u
  data:
    title: Laundry status
    message: >-
      washer {{ washer_before }} --> {{ washer_after }}; dryer {{ dryer_before
      }} --> {{ dryer_after }}

In an automation, a condition evaluating to false stops the sequence of action, in which case the  notification action that follows is not executed. If I needed more elaborate logic, I'd use a "choose" action instead of a condition.

Discussions