Measure energy usage... only while computer is mining?

Hello guys,

I’m trying to measure the energy consumption of my PC, but only while it is mining. Anyone done something similar before? Here are the relevant sensors I have in Home Assistant:

  • desktop_power (W)
  • desktop_energy (cumulative KWh)
  • desktop_is_mining (true/false)

Anyone have any suggestions, how I could measure energy usage only while desktop_is_mining is true? I’m doing this for tax purposes, so I can deduct energy costs.

One way I can think of, is to record desktop_energy when mining starts, and record it again when mining stops, and compare the two values. But I’m not really sure how I would go about storing the original value, doing the comparison, and saving the resulting value somewhere. Or if that’s even the best approach.