Help with converting automation to sensor template

I have an setup based on 2 sensors:

  1. sensor.washing_machine with 2 states "On" and "Off"
  2. binary_sensor.lumi_lumi_sensor_magnet_aq2_opening door sensor on washing machine with 2 states "Open" and "Closed"

And have perfectly working automation with notification when I did not pick up laundry after 1 hour after the end of washing. This automation:

alias: Notification pick up wet laundry 
trigger:
  - platform: state
    entity_id:
      - sensor.washing_machine
    from: "On"
    to: "Off"
action:
  - delay:
      hours: 1
      minutes: 0
      seconds: 0
      milliseconds: 0
  - type: is_not_open
    condition: device
    device_id: 9d418eb2aee29d6e3bb11e8cfb6e7b73
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening
    domain: binary_sensor
    for:
      hours: 1
      minutes: 0
      seconds: 0
  - service: notify.mobile_app_sm_g950f
    data:
      message: Washing finished 1 hour ago
      title: Pick up wet laundry 
mode: single

My goal here is to extract from above automation new custom template sensor or binary_sensor with value of wet laundry and use it with simplified automation like below or create some custom card on dashboard.

alias: Notification pick up wet laundry (refactored)
trigger:
  - platform: state
    entity_id:
      - sensor.wet_lanudry
    from: "Off"
    to: "On"
action:
  - delay:
      hours: 1
      minutes: 0
      seconds: 0
      milliseconds: 0
  - service: notify.mobile_app_sm_g950f
    data:
      message: Washing finished 1 hour ago
      title: Pick up wet laundry 
mode: single

Can you help me with writing this template sensor? There is a catch: "wet_loundry" state "On" is only once after "washing_machine" goes from "On" to "Off" until "binary_sensor.lumi_lumi_sensor_magnet_aq2_opening" is not "Open", so when someone close washing machine door after collecting laundry I do not want to see "wet_loundry" state as "On".

I think the following should work, but it may not depending how your washing machine sensor works. If it isn’t actually monitoring the washing machine, but is instead more of an inferential value there may be a case where opening the door to add a couple extra items or adjust the load would negate the condition based on last_changed.

template:
  - trigger:
      - platform: state
        entity_id: sensor.washing_machine
        from: "Off"
        to: "On"
        for: "01:00:00"
        id: "On"
      - platform: state
        entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening
        from: "off"
        to: "on"
        id: "Off"
    binary_sensor:
      - name: Wet Laundry
        state: >         
          {% set current = this.state | default('off', 1) %}
          {% if trigger.id == 'On' and 
          now() >= states.binary_sensor.lumi_lumi_sensor_magnet_aq2_opening.last_changed + timedelta(minutes=60) %} 
            on
          {% elif trigger.id == 'Off' 
          and current == 'On' %} 
            off
          {% endif %}

Oh I’ve tried your solution @didgeridrew but without luck: binary_sensor.wet_laundry went from "Unknown" to "Off" once and did not change at all. I’ve tweaked you template to 1 minute and tried different combination of running washing machine and opening/closing door - sill binary_sensor.wet_laundry is always in "Off" state. I do not know what

default('off', 1)

line do, is it something related to 1 hour? Should I change it when I downgrade this example to 1 minute for test?


My sensor.washing_machine is a proxy for shelly 1PM in socket. Template for this sensor looks like this:

template:
  - sensor:
      - name: Washing Machine
        state: >
          {% if states("sensor.shellyplus1pm_4417939544f0_switch_0_power")|float <= 2 %}
            Off
          {% else %}
            On
          {% endif %}

There is one problem in your solution: you have tried to replicate whole situation from my automation. As I can understand your solution can be described like:

binary_sensor.wet_laundry is on when loundry was not picked by 1 hour.

This is not what I was looking for, I was looking for simpler sensor described by conditions:

  1. binary_sensor.wet_laundry is “On” immediately after sensor.washing_machine went from “On” to “Off”
  2. binary_sensor.wet_laundry stays as “On” until binary_sensor.lumi_lumi_sensor_magnet_aq2_opening went from “Closed” to “Open” once
  3. After binary_sensor.lumi_lumi_sensor_magnet_aq2_opening state “Open” future changes to “Open” and “Close” state changes will not change binary_sensor.wet_laundry state (when someone picked up laundry and in the future close door I do not want to see binary_sensor.wet_laundry as “On”)
  4. when sensor.washing_machine go to state “On” this whole story reset to point 1.

So @didgeridrew I’m looking for solution without any time signatures.

Take this as a starting point, it fulfills my points except 3.

template:
  - binary_sensor:
      - name: Wet Laundry
        state: >
          {% if states("sensor.washing_machine") == "Off" and states("binary_sensor.lumi_lumi_sensor_magnet_aq2_opening") == "off" %}
            On
          {% else %}
            Off
          {% endif %}

But in spite of everything, thank you very much for your help and I hope for a happy solution of the puzzle!

Ok, you basically want a SET and RESET construction. I think you can do that by just adding the triggers from my first post to a slightly modified version of the sensor configuration of your second post.

template:
  - trigger:
      - platform: state
        entity_id: sensor.washing_machine
        from: "On"
        to: "Off"
        id: "on"
      - platform: state
        entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening
        from: "off"
        to: "on"
        id: "off"
    binary_sensor:
      - name: Wet Laundry
        state: >
          {% if trigger.id == 'on' and 
          is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening', 'off') %} 
            on
          {% else %} 
            off
          {% endif %}
1 Like

Simplify things by making a Template Binary Sensor, as opposed to a Template Sensor, for Washing Machine thereby helping to streamline your other Template Binary Sensor.

template:
  - binary_sensor:
      - name: Washing Machine
        state: "{{ states('sensor.shellyplus1pm_4417939544f0_switch_0_power') | float(0) > 2 }}"

      - name: Wet Laundry
        state: "{{ is_state('binary_sensor.washing_machine', 'off') and is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening', 'off') }}"

Thanks @Didgeridrew. This is exactly what I was looking for.