Run vacuum after 5 days idle?

Hi!

I’m trying to figure out a way to see last time the vacuum has cleaned.
so the thing I’m trying to fix is

If vacuum has been idle for more than 5 days then give a sensor (or something) a TRUE State.
and when the vacuum has cleaned less than 5 days ago the state should be FALSE.

I was trying to do it with a compare of the last run and the current time but now I’m stuck.

Anyone having any idea?

My current code minus current time template:

- platform: template
  sensors:
    since_neato_last_run:
      value_template:  >-
        {%- set slb = states.input_datetime.neato_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 -%}
        
    correct_hour:
      value_template: >
        {% set current_time = strptime(states.sensor.time.state*, '%Y-%m-%d, %H:%M') %}
        {{ ((as_timestamp(current_time) - as_timestamp(input_datetime.neato_last_run))/60)|round|int }}

And then there’s a script that triggers the last run:

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

Thanks! :grinning:

The Template Sensor has only one identifiable entity_id in it:

input_datetime.neato_last_run

That means the Template Sensor will be evaluated by Home Assistant only when input_datetime.neato_last_run changes state. Is that how you want the Template Sensor to work?

Here’s how I do something similar:

First Automation:
Triggered when the vacuum starts cleaning.
Set an input_datetime with the current date and time plus 5 days.

Second Automation:
Triggered when sensor.time equals the input_datetime.
Start the Vacuum.

1 Like

What I want is to get a sensor that for example takes input_datetime.neato_last_run minus TODAYSDATE and therefore gives me a number of days since last time the neato run.

this is the output of input_datetime.neato_last_run right now:

EDIT:

So my idea was to have a automation trigged if the days is < 5

Try this:

binary_sensor:
  - platform: template
    sensors: 
      vacuum_run_in_last_5_days:
        friendly_name: "Vaccum run in last 5 days"
        value_template: >-
          {{ (as_timestamp(now()) - as_timestamp(states('input_datetime.neato_last_run'))) < 432000 }}

This will create a binary_sensor that is “on” if (current time - last cleaned) is smaller than 432’000 seconds (5 days) and “off” otherwise.

Does not seem to work.

changed the 432’000 to 40 and the binary sensor never changes to on.

If you enter this into the Developer Tools -> Templates what do you get as a result?

{{ (as_timestamp(now()) - as_timestamp(states('input_datetime.netato_last_run'))) }}

“Unknown error rendering template”

Change the name of the entity, I wrote “netato” instead of “neato” :upside_down_face:

I fixed my post now.

Okey this worked.

But it doesn’t seem to be super reliable. The binary sensor only seems to trigger about 50-70% of the time.
Tried changing the time to > 50
so it turns on the binary senor after 50 seconds, but after triggering the script so the counter starts counting up, it only triggers the sensor about 50-70% of the time.

tried putting it in a automation instead but seems to have the same issue there?

Read somewhere that HA can have issues sometimes to read “now()” as a Var.
Maybe better to set up a time_date with timestamp?

I don’t understand. What do you mean with it only triggers 50-70% of the time?

What script?

Yes, there’s a known ‘shortcoming’ with HA and using .now() as a TRIGGER. You have to setup and use sensor.date_time if you want it to trigger accurately.

When the Neato has done a clean it triggers a script that sets the input_datetime.netato_last_run.

so when I run:

{{ (as_timestamp(now()) - as_timestamp(states('input_datetime.netato_last_run'))) }}

After the script has been executed the template starts counting from 0 -> 1 -> 2 etc.
But when it hits over 40 the binary sensor only seems to be trigged about 50% to 70% of the time.

The binary sensor looks like this right now:

binary_sensor:
  - platform: template
    sensors: 
      vacuum_run_in_last_5_days:
        friendly_name: "Vaccum run in last 5 days"
        value_template: >-
          {{ (as_timestamp(now()) - as_timestamp(states('input_datetime.neato_last_run'))) > 40 }}

So here you can see that the template is working:

But if you go to the binary sensor and look it’s off:

Okey, anyway to do a sensor.date_time with timestamp then?

Yes, it will work in dev tools / templating b/c your ‘forcing’ HA to calculate the value of now, the issue is that .now() doesn’t work when used as a trigger reliably. Need to instead use the sensor.date_time

1 Like

Maybe this will help get you on the right track, sorry on phone and can’t be more specific!

{% set elapsed = (as_timestamp(states('sensor.date_time').replace(',','')) - as_timestamp(states.sensor['6900_kitchen_hvac_action'].last_changed| default(0)) | int ) %}
        {% set days = (elapsed / 86400) | int %}

Also, this thread: The EPIC Time Conversion and Manipulation Thread!

1 Like

Tried before to get a timestamp from sensor.date_time but never succeed. haha…

But will give it a try again, thanks!

in my sensors.yaml file:

- platform: time_date
  display_options:
    - 'time'
    - 'date'
    - 'date_time'

Just make sure to set them up and restart to get them to appear.

1 Like

I now remember the issue with now() :upside_down_face:
Sorry for leading you to the wrong path.

Use the suggestion from @Markus99

as_timestamp(states('sensor.date_time').replace(',',''))

This works for me to get a timestamp from sensor.date_time

1 Like

Thanks everyone for you’re help! Everything is working perfectly now!

Will post the code I ended up using if someone else is interested! :smiley:

Configuration.yaml

- platform: template
  sensors:
    since_neato_last_run:
      value_template:  >-
        {%- set slb = states.input_datetime.neato_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 -%}
        
#    sensors:
    correct_hour:
      value_template: >
        {% set current_time = strptime(states.sensor.time.state, '%Y-%m-%d, %H:%M') %}
        {{ ((as_timestamp(current_time) - as_timestamp(input_datetime.neato_last_run))/60)|round|int }}

binary_sensor:
  - platform: template
    sensors: 
      vacuum_run_in_last_5_days:
        friendly_name: "Vaccum run in last 5 days"
        value_template: >-
          {{ as_timestamp(states('sensor.date_time').replace(',','')) - as_timestamp(states('input_datetime.neato_last_run')) > 432000 }}

Script

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