Monitoring last device run time

One of the key benefits of Home Assistant is that you get access to all sorts of historical data and a number of ways to expose them. You can use this in your automation or simply display the last run time in your UI. Here are three examples from my setup.

1. Sprinkler Controller

I am using Rachio 3 to control my sprinkler system. While it is one of those devices where you set it and forget it, it is still nice to know if the grass might be wet today due to a recent run. Since Rachio exposes a switch for each zone with On / Off state, the easiest way is to check when was the last state change. I am using a function as_timestamp in a markdown card with the following code.

👉 Sprinklers last ran <b>{{ ((as_timestamp(now()) - as_timestamp(states.switch.rachio_grass.last_updated)) / (3600)) | float | round(0) }} hours</b> ago.

2. Robot Vacuum

My Roborock robot vacuum runs mostly whenever needed without any set schedule. Therefore, it is useful to know when it ran last time and for how long. Similarly to the previous example, I used a markdown card, but this time using attributes exposed by the entity. I am also using a conditional card that hides the markdown card when Roborock is running.

card:
  content: >-
    👉 Vacuum last ran <b>{{ ((as_timestamp(now()) -
    as_timestamp(states.vacuum.roborock_s4.attributes.clean_stop)) / (3600)) |
    float | round(0) }} hours</b> ago for <b>{{
    states.vacuum.roborock_s4.attributes.cleaning_time }} minutes</b>.
  type: markdown
conditions:
  - entity: vacuum.roborock_s4
    state: docked
type: conditional

3. TV Screen Time

My final example is my TV. In this case, it is less important to know when was the last time the TV was on (although you could easily achieve that by monitoring the state as described in example #1), but rather how much time it was on in a given period. In this case, I am using the platform history_stats to sum up the time for the past week. This platform has to be configured as a sensor.

  - platform: history_stats
    name: TV Time Last Week
    entity_id: media_player.sony_bravia_tv
    state: "on"
    type: time
    duration:
      days: 7
    end: "{{ now() }}"

This sensor is then exposed using a simple markdown card with the following content:

  👉 Living Room TV was on for <b>{{
  state_attr('sensor.tv_time_last_week', 'value') }}</b> last week.
Exit mobile version