Sensor for recording single event values

Hi all. I’ve been tinkering with my HA setup for a good year now without coming across a problem that I couldn’t solve myself with assistance from this great community. However I am at a loss as to how to make use of the data coming from my Oral-B Toothbrush (via ESPHome Bluetooth Bridge).
The Oral-B integration seems to be working well, and I am receiving data whenever the toothbrush is active, however the main reason I wanted to integrate the toothbrush was to record my daily brushing time and use the data for averages, improvements etc. However I can’t seem to work out an efficient way of converting the raw sensor data from the integration to a useful format.
From what I can tell, the toothbrush time sensor is triggered at the end of a brushing cycle and updates the state of sensor.smart_series_6000_a9f0_time with the total time spent brushing. This state remains until after the next brushing, when the state is replaced with the last duration.
Screencap below shows my last two brushing cycles, last night at 42s and this morning at 57s.

As this sensor is not cumulative, whenever I activate the brush, it resets the last brushing duration value so I can’t really use it for averages and daily totals etc. What I would like to do is have a sensor that records the state change as a point ‘event’, that would allow me to do a daily sum, average or other such calculation.

  1. Total time brushing per day (recorded almost like a utility meter)
  2. Average time per brushing (over say a month)

So far I’ve looked at:

  • Utility Meter - Won’t work as it is not a cumulative sensor, also the ‘s’ measurement unit seems to break this.
  • Reimann integration - Does’nt work as it will result in erroneous data as the ‘state’ remains as the last used time until the next brushing cycle. Integrating this gives times in the millions of seconds.
  • Automation on state change - This automation would convert to a cumulative time by adding the state change to a previous sensor value at a certain time, but I don’t want to rely on automations, I’d rather have a template sensor working in the background.

Can anyone point me in the right direction on how to use this data?

Thanks in advance.

Just an idea, and I might be wrong, but:
Is the sensor.smart_series_6000_a9f0_time output always going to 0 (zero) at the end of each cycle?
If so, you could create an extra trigger based template sensor that normally has the value 0 (zero), but that is triggered by the toothbrush time senor value going to 0, than delays for a short moment, sets its value the current toothbrush time value, and then after a short moment goes back to zero again.
This new sensor now in fact shows the brushing time, but only for a short moment right after the brushing cycle is finished.
Something like this:

toothbrush

Now you should be able to use this new sensor as input for a Riemann sum integration.
Does that make sense?

Opening this up again. The method you mentioned does not seem to work as the time sensor is actually not a ‘point’ of data but changes during the brushing cycle.
What I observe is happening is that while the toothbrush is on, the sensor.smart_series_6000_a9f0_time sensor resets to zero and then counts up in seconds from when it turns on until the brush is turned off, at which point it holds the last value until the brush is turned on again and repeats from zero.

My next thought was to have a template sensor ‘sensor.toothbrush_time_all’ that triggers on a state change, and updates to be the previous value plus the new time.

I tried the following template sensor in my config yaml:

    - trigger:
      - platform: state
        entity_id: sensor.smart_series_6000_a9f0_time
        not_to:
        - "unknown"
        - "unavailable"
      sensor:
      # Increment the total active time by the new time
      - name: "Toothbrush Time All"
        unique_id: toothbrush_time_all
        state: "{{ states('sensor.toothbrush_time_all') | int + states('sensor.smart_series_6000_a9f0_time') | int }}"
        availability: "{{ is_number(states('sensor.toothbrush_time_all') | int) }}"
        unit_of_measurement: "s"
        state_class: total_increasing

The idea being that once a state change is triggered, a template sensor is updated to be a cumulative log of each state change value, however this does not work for two reasons:

  1. I assume that while the brush is on, the state is being constantly updated with the increasing time, so the summation will be wrong. (1s + 2s + 3s = 6s instead of 3s actual final total time)
  2. The value of the sensor.toothbrush_time_all seems to default to ‘unknown’ on HA restart, rendering the template sensor rather useless as restarts are frequent.

Another sensor that the toothbrush has is smart_series_6000_a9f0_toothbrush_state, which is either ‘running’ or ‘idle’ from what I can tell (possibly unknown on HA restart). I was thinking that I could use the template sensor to trigger only when the state changes back to ‘idle’ and then sum the current state with the previous count in sensor.toothbrush_time_all, but this does not solve the ‘unknown’ issue in #2 above.

What can I do to get this over the line?

Thanks!