How to derive an utility meter for a specific device by having a "total consumption" and a "mask"

Hi.

I have a Shelly Pro 3EM measurement on all three phases, so I can measure total energy consumption of our house.

I also have a helper to recognize when sauna is on (I do it by looking on consumption of all 3 phases and control input_boolean based on that, works), so basically I have the data I need:

Is there a way how I could now get a utility meter/sensor, that would only compute energy consumption of the sauna by integrating only over the time when it’s actually on?

My limited math says that I want basically want to “overlay” my “mask” over the consumption integral over time (1 for sauna is on, 0 for sauna is off). I know it won’t be exact, as there are some other devices, but I plan to just subtract that “base consumption per unit of time” from the result.

I would first try to do it in HA, and as the last resort, I could create an SQL sensor (I use TimescaleDB).

ChatGPT says doing something like this, but I am curious if there is a better way.

I am sorry, I figured that out after turning on my brain. I love home assistant.

I will document what I did here:

  1. Have an input helper:

Control it via automation:

alias: Deriving if sauna is on or not
description: >-
  Basically a sensor


  There is e.g. a 2 seconds lag between the total power and the individual
  phases updates, sometimes not triggering the on. I added 5 seconds delay to
  make sure the individual phases have time to update.
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.shellypro3em_0cb815fd6ab8_total_active_power
    above: 5200
    for:
      hours: 0
      minutes: 0
      seconds: 5
condition:
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.shelly_pro_3em_phase_a_active_power
        above: 1400
      - condition: numeric_state
        entity_id: sensor.shelly_pro_3em_phase_b_active_power
        above: 1500
      - condition: numeric_state
        entity_id: sensor.shelly_pro_3em_phase_c_active_power
        above: 2700
action:
  - service: input_boolean.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.sauna_is_on
  - wait_template: "{{ not ((states.sensor.shelly_pro_3em_phase_c_active_power.state | float >\_2700)\nand (states.sensor.shelly_pro_3em_phase_b_active_power.state | float >\_1500)\nand (states.sensor.shelly_pro_3em_phase_a_active_power.state | float >\_1400))\n}}"
    continue_on_timeout: true
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - service: input_boolean.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.sauna_is_on
mode: single

  1. create a template sensor

  1. Create a utility meter for that template sensor

Tadaaaa

img

Check out MeasureIt!
I wrote it for scenarios like yours. Let me know if you have questions!

Cool, thanks!