Contact Sensor Automation

Hello Everyone,

Trying to get the automation below to work, basically, I want to set the condition such that the automation only triggers if the dishwasher contact has been closed for between 2 hours and 2 hours and 15 mins. The below doesn’t seem to work.

alias: Dishwasher finished
description: ""
trigger:
  - type: opened
    platform: device
    device_id: 59c0901a2ab618afd1d999f8a6bd92bd
    entity_id: binary_sensor.dishwasher_contact
    domain: binary_sensor
condition:
  - condition: and
    conditions:
      - condition: template
        value_template: >-
          {{ now().timestamp() -
          states.binary_sensor.dishwasher_contact.last_changed.timestamp() >=
          1}}
      - condition: template
        value_template: >-
          {{ now().timestamp() -
          states.binary_sensor.dishwasher_contact.last_changed.timestamp() <=
          8400 }}
action:
  - service: notify.alexa_media
    data:
      target: media_player.everywhere
      data:
        type: announce
      message: The dishwasher cycle has completed
  - delay:
      hours: 0
      minutes: 20
      seconds: 0
      milliseconds: 0
  - service: notify.alexa_media
    data:
      message: This is a reminder, the dishwasher has finished, and can be emptied
      target: media_player.everywhere
      data:
        type: announce
mode: single

Can you explain what do you want to do with dishwasher?

When that triggers, the last_changed will have just updated. You want to:

  • use a State trigger rather than a Device trigger to ensure you have access to the trigger variables you need (plus, it’s a lot neater);
  • look at the trigger.from_state object in your condition to see what it was before, rather than what is now.

Something like this, assuming that the contact entity goes 'off' when opened and using template condition shorthand:

alias: Dishwasher finished
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.dishwasher_contact
    to: 'off'
condition:
  - "{{ 7200 < (now()|as_timestamp - trigger.from_state.last_changed|as_timestamp(0)) < 8100 }}"
action:
  - service: notify.alexa_media
    data:
      target: media_player.everywhere
      data:
        type: announce
      message: The dishwasher cycle has completed
  - delay:
      minutes: 20
  - service: notify.alexa_media
    data:
      message: This is a reminder, the dishwasher has finished, and can be emptied
      target: media_player.everywhere
      data:
        type: announce
mode: single

Basically, I know my dishwasher cycle runs for between 2 hours to 2 hours 15 mins when it is done it automatically opens the door, so when the dishwasher door opens if it’s been closed for the previously mentioned amount of time, consider the cycle finished and send a notification.

I might have to get a smart plug as that may be a better way currently.

New to HASS so the language is a bit confusing currently

I keep getting
Error occurred while testing condition

In ‘template’ condition: UndefinedError: ‘trigger’ is undefined

You cannot test that in the template editor directly, nor can you run the automation manually. See here:

Troubleshooting Automations - Home Assistant (home-assistant.io)

If you’re not doing that and that’s an error message from an automation run, paste the full automation YAML.

You can trigger it by opening the dishwasher. Your automation trace should then show you the condition preventing the notifications being sent (assuming not in the 2:00 to 2:15 hour range).

You can test it in the template editor to some extent by faking up the trigger variable, like this (with one of my sensors):

Just realised i can’t test haha, obviously so yeah changed it a bit and then tested seems to be working now

Thank you kindly

1 Like

I have a dumb dishwasher but I found a way to make it smart. I use contact sensor on dishwasher knob and smart plug for power.
I created dropdown helper. In this helper I defined dishwasher statuses. Using automations I managed to change status of a dishwasher, when it is ready, finished, running or off based on smart plug power and knob position. I created automation for fingerbot to turn on dishwasher when certain conditions are met ie. turn it on after 10 pm if status is ready.
When you do this, try to keep it simple aka KISS, keep it simple and stupid.