Change date to current date by helper button

Hi,

I’ve managed to create watering counter for my plants- it counts number of days since last watering.

  • platform: template
    sensors:
    last_watering:
    friendly_name: “Days since last watering”
    icon_template: mdi:calendar
    value_template: “{{ ((as_timestamp(now()) - as_timestamp(states(‘input_datetime.watering’))) | int(0) / 86400 + 1) | round(0, default=1) }}”

Now i’d need to reset this sensor every watering by setting input_datetime.watering to current date.
So i created a helper button: input_button.watering_button, but I don’t know how do I make automaton to do this.

Did anyone here make something like this and would like to share?

If you are just starting out using template sensors you should be using the current format, not the legacy format shown in your example. The state of an input button entity is a datetime string which can be converted to a datetime and subtracted from now() using the | as_datetime filter. The following will return the number of whole days since the button was pressed or the input_button.press service was called on the entity.

template:
  - sensor:
      - name: “Days since last watering”
        unique_id: last_watering
        icon: mdi:calendar
        state: >
          {{ (now() - states(‘input_button.watering_button’)|as_datetime).days }}
1 Like