Time since specific state

I am trying to detect when the robot vacuum hasn’t been in the cleaning state for at least 12 hours. I want to check this at a specific time each day. My issue with the commonly suggested options is that the vacuum has several states (charging, docked, unknown), that I do not care for.

So if I use last_changed, it might be skewed by the robot reporting leaving the charging state and entering the docked state.

I imagine that for this, I would have to use a history_stats template, but am unsure how to define it, as they seem to require a specific state.

Here’s what I have so far:

sensor:
  - platform: history_stats
    name: Time since last clean
    entity_id: vacuum.brenda
    state: "cleaning"
    type: time
    start: "No clue what should go here. Not states.vacuum.brenda.last_changed, as that would be influenced by the charging and docked states"
    end: "{{ now() }}"

You can define your time period either with a template or by giving a duration

sensor:
  - platform: history_stats
    name: Time since last clean
    entity_id: vacuum.brenda
    state: "cleaning"
    type: time
    start: "{{ now() - timedelta(hours=12) }}"
    end: "{{ now() }}"

OR

sensor:
  - platform: history_stats
    name: Time since last clean
    entity_id: vacuum.brenda
    state: "cleaning"
    type: time
    end: "{{ now() }}"
    duration: "12:00:00"

This will provide you with a number representing how many hours the vaccuum was in “cleaning” in the last 12 hours. Since you want to check that it has not been running, you are looking for a value of 0.

1 Like

Genius. Thank you.