Power meter automation: Get the duration of the power consumption

Hey there!

I’ve got a Shelly Plug S with Tasmota that I’ve connected to our coffee maker. I want to get the time it took to boil the water, so that I can calculate in the automation when the coffee will be finished.

This is how a usual usage graph looks:

At the beginning the water is boling (> 1000 W), after that the heater for the coffeepot is on (~55 W).

I’ve created an automation with a trigger that triggers when the power comsumption is between 40-70 W:

alias: Kaffeemaschine ausschalten
trigger:
  - type: power
    platform: device
    device_id: c9e9e062cf754dfff249ce97da2b4fb7
    entity_id: sensor.shelly_plug_s_energy_power
    domain: sensor
    below: 70
    above: 40

I’ve tried using a template sensor which tracks the different states (water boiling, heating and off) and a history stats sensor, but you unfortunately with this method you have to define a time span and it wouldn’t work correctly if I use the coffee maker more than once in the defined time span.

I also tried to use the trigger.for Automation Trigger Variable in the action: of the automation, but in my example the value was None.

In the automation I need to get the duration of the last time the power consumption was above 1000 W. What’s the best way to achieve this?