Run vacuum after 5 days idle?

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

Shouldn’t the binary sensor be called “Vacuum not run in last 5 days”? If (now - last run) > 5 days, the template evaluates to true, meaning the last cleaning was more than 5 days ago.

True True :grinning:

This has stopped working after latest update.

Any ideas?

The output right now seems to be what home assistant sets as a standard:

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 }}
    
    

    
    
sensor:
  - 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 }}
  - platform: template
    sensors:
      days_neato:
        friendly_name: "Days Since Neato Last Cleaned"
        unit_of_measurement: 'days'
        value_template: "{{ as_timestamp(states('sensor.date_time').replace(',','')) - as_timestamp(states('input_datetime.neato_last_run'))}}"
  - platform: template
    sensors:
      neato_last_run_rounded:
        friendly_name: "Days Since Neato Last Cleaned"
        value_template: >-
          {%- set uptime  = states.sensor.days_neato.state | round -%}
          {%- set sep     = ', ' -%}
          {%- set TIME_MAP = {
                        'week': (uptime / 604800) % 604800,
                        'day': (uptime / 86400) % 7,
                        'hour': (uptime / 3600) % 24,
                        'minute': (uptime / 60) % 60,
                        'second': (uptime % 60)
                    }
          -%}

          {%- for unit, duration in TIME_MAP.items() if duration >= 1 -%}
            {%- if not loop.first -%}
              {{ sep }}
            {%- endif -%}
              
            {{ (duration | string).split('.')[0] }} {{ unit }}

            {%- if duration >= 2 -%}
              s
            {%- endif -%}
          {%- endfor -%}

          {%- if uptime < 1 -%}
            just now
          {%- endif -%}
input_datetime:
  neato_last_run:
    name: Neato Last Run
    has_date: true
    has_time: true 

Did you manage to get this working again?