Persistent last changed sensor

I have succesfully implemented some automation to notify me when my washing machine is finished. I used this configuration.

I want to create a sensor which I can use in my lovelace configuration. This sensor simply needs to display the last time a washing machine program finished. I tried looking at the last_changed value of input_boolean.wasmachine, but that reset after a reboot of HA. I want it to be persistent, so that even after a server reboot it still says i.e. “8 hours ago”. I can’t seem to figure it out. How should I do this?

You could store the time and date in an input_datetime when your machine finishes.

Isn’t that unnecessarily complex? There is already an automation for ‘Set washing machine as finished’. Can’t I just add an action that updates a value?

I have this right now:

      wasmachine_last_run_date:
        friendly_name: "Wasmachine datum laatste programma"
        value_template: "{{ as_timestamp(states.input_boolean.wasmachine.last_changed) }}"

But this just displays as 1570839077.300437 in Lovelace UI. Before I tried this:

input_datetime:
  wasmachine_last_run:
    name: Datum laatste programma
    has_date: true
    has_time: true
    initial: 2019-10-12 12:00

Automation action when washing machine finishes:

    - service: input_datetime.set_datetime
      entity_id: input_datetime.wasmachine_last_run
      data_template:
        datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

But this just displays multiple input fields, and I want an “8 hours ago” kinda thing. Like this (last boot):

image

No it is necessarily complex. As you have pointed out, the last changed attribute is altered at restart.

To get the format you want will be even more complex:

Alright, so I followed your lead and it worked, but I changed it a bit:

sensor:
  - platform: template
    sensors:
      wasmachine_last_run_date:
        friendly_name: "Date last program run"
        device_class: timestamp
        value_template: "{{ states('input_datetime.wasmachine_last_run_date') }}"

input_datetime:
  wasmachine_last_run_date:
    name: Date last program run
    has_date: true
    has_time: true

automation:
  - alias: Set washing machine as finished
    trigger:
    - platform: numeric_state
      entity_id: sensor.wasmachine_statistics_mean
      below: 5
      for:
        seconds: 30
    condition:
    - condition: state
      entity_id: input_boolean.wasmachine
      state: 'on'
    action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.wasmachine_stop
        value: >-
          {{ states('sensor.wasmachine_kw_today') }}
    # Set input_datetime to now()
    - service: input_datetime.set_datetime
      data_template:
        entity_id: input_datetime.wasmachine_last_run_date
        time: '{{ (as_timestamp(now()) | timestamp_custom("%H:%M:%S", true)) }}'
        date: '{{ (as_timestamp(now()) | timestamp_custom("%Y-%m-%d", true)) }}'
    - service: notify.family
      data_template:
        message: >-
          Washing machine finished. Runtime: {{ ((as_timestamp(now())-as_timestamp(states.input_boolean.wasmachine.last_changed))/60) | round | int }} minutes. Power usage: {{ states('sensor.wasmachine_verbruik') }} kWh.
    - service: homeassistant.turn_off
      data_template:
        entity_id: input_boolean.wasmachine

So it looks like this now (I’ll hide the input_datetime soon). Thank you!

image

I discovered an even better solution. Since I have an automation setup that runs when the program finishes, and I just want to know when the program ran for the last time I can just do this:

  - platform: template
    sensors:
      wasmachine_last_run_date:
        friendly_name: "Date last program run"
        device_class: timestamp
        value_template: "{{ state_attr('automation.set_washing_machine_as_finished', 'last_triggered') }}"

This is persistent through restarts. The end result looks like this:

image

Because I find this the most effective for my use case I’ll mark my answer as the solution.

3 Likes

Please upvote the feature request to help resolve this: Retain last state change data of a sensor after reboot