Show Last time the plants were watered

Hi Guys.

I’m having an issue.
I need to know the last time a switch was ON. I’ve been using the last_changed template, but it is not good enough because it resets every time HA restarts, and every time the device goes unavailable and the switch controls my plants watering. (Sometimes a few days pass between each watering).

sensors:
    last_run_orquideas:
        friendly_name: "Último Riego Orquideas"
        value_template: '{{ (as_timestamp(states.switch.orquideas.last_changed)) |   timestamp_custom("%A, %d %h %H:%M") }}'
        icon_template: mdi:calendar-clock

Also… This timestamp shows days in English and I like to display days in Spanish.

Can you help me?
Thanks a lot!

Have the state change of the switch reset a timedate helper, then use that to show how long ago it was watered

You could use the newly introduced trigger based sensors :

template:
  - trigger:
      - platform: state
        entity_id: switch.orquideas
        from: 'off'
        to: 'on'
    sensor:
      - name: "Último Riego Orquideas"
        state: '{{ (as_timestamp(states.switch.orquideas.last_changed)) | timestamp_custom("%A, %d %h %H:%M") }}'

This will only update when the switch is turned on and should restore after a restart.

I don’t know about the translation.

There doesn’t seem to be an easy answer for the language in this case. Far from ideal, but this update to @tom_l’s solution above should work:

        state: '{{ ["Lunes","Martes","Miércoles","Jueves","Viernes","Sábado","Domingo"][states.switch.orquideas.last_changed.weekday()] + (as_timestamp(states.switch.orquideas.last_changed)) | timestamp_custom(", %d %h %H:%M") }}'

Quite pleased I didn’t need to look up the Spanish day names, despite it being about 381 years since my last Spanish lesson… (edit: missed a couple of diacriticals :face_with_raised_eyebrow:)

1 Like

Thanks a lot! @Troon and @tom_l.
Maybe I was not clear enough. I want to keep the data (last watering event) after a restart if possible.

The translation works perfectly!

@tom_l suggested that the sensor should keep its value over a restart.

I’ve not used one of these new trigger-based sensors yet, but if it does not work you could use an input_datetime instead [docs], created under Helpers (Ayudantes) and updated via an automation. Let me know if you need more help with that.

Thanks @tom_l and @Troon. I recently come back to this “problem” and I come up with solution like this:

  - platform: mqtt
    name: "Último Orquideas"
    state_topic: ultimo/orquideas
    icon: mdi:calendar-clock
    value_template: '{{ value | int | timestamp_custom("%a %d %h %H:%M") }}'

Every time my switch is on, a tasmota rule publishes and retain the timestamp in UNIX like this:

rule2 on Power1#state=1 do publish2 ultimo/orquideas %utctime% endon

By doing this I can keep the date even after a power loss. The only “issue” is that the sensor is still in English. Any ideas on how to translate?

Thanks a lot!

Here’s a solution for weekday and month names that effectively duplicates the specific timestamp_custom output in Spanish, but it should be easier than this. I’m assuming your abbreviations are just the first three letters of each full name. Pop this in the template editor to adjust how you want, and swap in your timestamp in place of now() as required:

English: {{ now()|as_timestamp()|timestamp_custom("%a %d %h %H:%M") }}
Español: {{ "%s %d %s %02d:%02d" %
            (["Lun","Mar","Mié","Jue","Vie","Sáb","Dom"][now().weekday()],
             now().day,
             ["Ene","Feb","Mar","Abr","May","Jun","Jul","Ago","Sep","Oct","Nov","Dic"][now().month-1],
             now().hour,
             now().minute) }}