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:
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?
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:
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?
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.
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 }}
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
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 %}