Measuring Device On time for PS5 (or other devices)

Hi,

My goal is to measure the amount of time - per day - that the PS5 is running. I will determine if it is running when a power meter the PS5 is attached to exceeds 60W of draw.
Ultimately, I want to measure all the time spent in front of a screen (i.e. PS5 + Switch + TVs) within today, and then trigger something (a notification and/or even cutting the power to the PS5 entirely for the remainder of the day). But let’s just focus on getting the timer up and running for one device, namely the PS5.

I have googled this already, assuming people would have already done something similar.
The main one I was using as my template is this thread Count "on"-time of a binary-sensor - #4 by petro, but also Record TV ON hours and display as barchartH .
My understanding is that I should be able to do this with History Stats. But the sensor I created is not counting anything and just staying at 0.

This is where I am so far:

   - platform: history_stats
    name: "PS5 ON today"
    entity_id: sensor.power_monitoring_plug_0x00123456789_power
    state: "{{ states('sensor.power_monitoring_plug_0x00123456789_power') | float > 60 }}"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"  

Does that only work for binary sensors? Am I not allowed to only measure the power draw above 60 here?
How would I get this sensor (timer) to measure what I want it to measure?

TIA!

1 Like

To follow the logic of the sample you used, yes. The “state” doesn’t even accept templates, apparently.

See below to create a template binary sensor that does what you want.

1 Like

oh man, awesome! thanks so much.

I got it working now.

I created a binary sensor that just checks whether or not the PS5 is on or not.
Then I simplified my original ‘time on’ sensor from the OP to track this binary sensor.

This is what it looks like now in case anybody stumbles upon this thread in the future:
Templates.yaml

  - binary_sensor:
      - name: "PS5 ON binary"
        state: >
          {{ states('sensor.power_monitoring_plug_0x00123456789_power') | float > 60 }}

and sensors.yaml

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