How to "calculate the time spent" above a set value in last 24 hours?

I am not sure where to begin with this or even if it is possible so I will just ask.

Purpose: I want to determine how much time my Fridge is running in the “Last 24 Hours”. I have it connected to a Power Monitor and know when idle = 2.4watts, Running = +/- 125watts.

Is there a way to calculated approx time the fridge is running i.e. above say above 5watts as anything less would be idle?

Sensor Name = sensor.meross_plug_02_power

Thanks in advance for any suggestions on method to achieve this if at all possible?

Suggest to create

  1. binary sensor that states ‘on’ when above XYZ watts
  2. history stats sensor on ‘time’ when the binary sensor is on (duration eq 24h)
    History Stats - Home Assistant (home-assistant.io)

Thanks for the tip. Will see if I can work my way through doing this.
I mucked around unsuccessful with History Stats prior to posting here. Will check out as per your suggestion what “Binary Sensors” are and try figure it out. Thanks.

something alike this

binary_sensor:
  - platform: template
    sensors:
      fridge_on:
        unique_id: fridge_on
        value_template:  "{{ (states('sensor.meross_plug_02_power') | float(0)) > 125 }}"

The value template has to result in a true or false and that value (in the sensor state) you can then use for the history stats sensor, do check if the BS throws on or true when the value goes to 125+, you can use devtools > states to manually set the sensor.meross_plug_02_power e.g. to 130

1 Like

Ha…I was just reading about creating Binary sensors and you posted.

Thanks for that especially for including the value template part. I never know what to included there. Will give that a go and see what shows up and report back. Thanks.

That worked nicely thanks. It went to “On” for 14mins.

Now I need to work on the history stats for something to tell me total mins on daily. Am new to all of this part so slowly learning each time I come up with an idea of how a stat could assist.

In this instance checking if my “14 year old Fridge” is turning on too often and how many hours it is “Running” per day.

This is what my son’s gaming PC ‘on’ is providing, it cuts on midnight along the definition of the sensor
You can of course create multiple hist stats sensors and try them out, delete them afterwards
image

Nice. yeap I have something similar now which is great and helps seeing the periods it is running.

I eventually worked things out and now have what I need. Took me time as stupidly kept forgetting to “restart” for my History Start to show up.

In Summary: Below is what I ended up with and the method.

Fridge On Stats

No.1 = using your suggested:
Purpose: Creates a Binary Sensor which shows Fridge as “On” when above 125w. In turn was then able to create a “History Stat” No3 below to add up total “On” time today.

binary_sensor:
  - platform: template
    sensors:
      fridge_on:
        unique_id: fridge_on
        value_template:  "{{ (states('sensor.meross_plug_02_power') | float(0)) > 125 }}"

No.2 = Just uses a “Statistic Card” for my “sensor.meross_plug_02_power” .showing the “Mean” power use per hour.

No.3 = Using the Binary Sensor created above, my end goal was sorted by adding a “History Stat” showing how many hours the Fridge is on for the day so far.
I’ll create another showing for yesterday later.

sensor:
  - platform: history_stats
    name: Fridge ON today
    entity_id: binary_sensor.fridge_on
    state: "on"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"

No doubt there might be a better way but this works well for my needs and confirms my Fridge is fine. Many thanks for your assistance. It was really appreciated,

1 Like