Count days since last time?

Is it possible to make a count days since last time I cleaned the pellets burner?

Example.
I press a button in my UI (cleaned burner) when I press that button today’s date and time is stored and showed in the UI. And also showing in the UI days since last cleaning.

Pellets burner:
Cleaned 2019-01-01 time: 19:00:10
Days since: 9 (changing over time)

Shouldn’t be too diffcult.

Set up an input_datetime that you push the date (maybe also the time) value to when you click on the ‘execute/activate’ button for a script that does exactly that.

Then you create template sensor that shows the difference of the datetime value between now and the datetime value of the value you just stored.

Might need a little formatting to show proper days and hours and such, though.

1 Like

Thanks for reply.
But I don’t see the big picture how to do it. Is it possible for you or anyone else to make an example?

This is the big picture :grinning:

Here are some details from my ‘dishwasher’ setup:

Creating an input_datetime requires 2 steps in the configuration.yaml or your split out files:

sensor:
    - platform: time_date
      display_options:
        - 'date_time'

input_datetime:
  dishwasher_last_run:
    name: DishWasher Last Run
    has_date: true
    has_time: true 

Using this helps to Push the date and time by a script, e.g.:

script_store_dishwasher_last_run:
  alias: 'Store Dishwasher Last Run'
  sequence:
    - service: input_datetime.set_datetime
      data_template:
        entity_id: input_datetime.dishwasher_last_run
        time: '{{ (as_timestamp(now()) | timestamp_custom("%H:%M:%S", true)) }}'
        date: '{{ (as_timestamp(now()) | timestamp_custom("%Y-%m-%d", true)) }}'

You can now place the input_datetime and the script in your frontend; invoking the script should store the current date and time in the inpt_datetime and show it straight away.

Hope that all works - I just pasted things together from various items because I don’t run it exactly that way.

And here’s an example I just crudely copied & pasted from the sensor I normally use to show how long HA has been running - you might have to use the Template Editor under /dev-template to make sure it works and to strip off what you don’t need:

- platform: template
  sensors:
    since_dishwasher_last_run:
      value_template:  >-
        {%- set slb = states.input_datetime.dishwasher_last_run.state.split(' ') -%}
        {%- set count = slb | length -%}
        {%- set hms = slb[count - 1] -%}
        {%- set hms_trimmed = hms.split('.')[0] -%}
        {%- set hms_split = hms_trimmed.split(':') -%}
        {%- set hours = hms_split[0] | int -%}
        {%- set minutes = hms_split[1] | int -%}
        {%- if count == 3 -%}
          {{ slb[0] ~ ' ' ~ slb[1] ~ ' ' }}
        {%- endif -%}
        {%- if hours > 0 -%}
          {%- if hours == 1 -%}
            1 h
          {%- else -%}
            {{ hours }} h
          {%- endif -%}
        {%- endif -%}
        {%- if minutes > 0 -%}
          {%- if hours > 0 -%}
            {{ ', ' }}
          {%- endif -%}
          {%- if minutes == 1 -%}
            1 min
          {%- else -%}
            {{ minutes }} min
          {%- endif -%}
        {%- endif -%}

If you put the sensor.since_dishwasher_last_run in your frontend it should show what you want to see.

1 Like

This i will try :slight_smile: now it’s a bit clearer. Thank you :+1:

Work perfectly :grin: Awsome help :+1:

EDIT: Copy & paste error, was missing .state. part from the first split. Got some values out now. Doesn’t seem right yet, but at least it’s a value :slight_smile:

Hi,

I’m trying to use this template for another similar purpose but seem to fall short. I only edited the sensor entity and my sensor has value “unknown”. When I put the sensor to the template editor, I get

Error rendering template: UndefinedError: 'homeassistant.core.State object' has no attribute 'split'

Any ideas?