Average usage over time in automation

Hi,

I have looked at the documentation on templates and I think this is a tempate application but one that is too complex for my current level of understanding. The end objective is to monitor the wattage of an energy monitoring plug and turn it off if the wattage averages a certain amount (300) for 2 minutes. I need to use average because the normal fluctuation is 20-40 watts (it is powering a water pump).

I am probably overthinking this but it seems I would need to look at the wattage reports every 30 seconds and use a certain number of those observations to get the average. The other idea I had was to have a counter that gets incremented every time the wattage goes below 300 and turn the switch off if the count goes above a certain amount.

Following the second approach, I got to this point but not sure how to check the counter value and get it to turn off if it’s 10. I added a Choose but get an error when saving (“Message malformed: Must contain at least one of below, above”):

alias: Pump Wattage Monitor
description: ""
trigger:
  - type: power
    platform: device
    device_id: 7094747e36478d716dd717c94952e5e6
    entity_id: c5dd3f4e74819b0f6b7d03f250bbdb20
    domain: sensor
    below: 300
condition: []
action:
  - service: counter.increment
    metadata: {}
    data: {}
    target:
      entity_id: counter.low_wattage_counter
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: counter.low_wattage_counter
            value_template: "10"
        sequence:
          - type: turn_off
            device_id: 7094747e36478d716dd717c94952e5e6
            entity_id: 8777f42ad86162c5c3a24e0d4ddc1386
            domain: switch
mode: single

Any other suggestions are welcome.

Use the Statistics integration to create a sensor that returns a running average of the value of your power sensor.

1 Like

Thanks, I did not know that integration existed.

Just for my information (I’m new to Choose also), will this do what I want if I intended to increment a counter entity every time the reported wattage goes below 300 and turn off the reporting device if the counter is 10?

action:
  - service: counter.increment
    metadata: {}
    data: {}
    target:
      entity_id: counter.low_wattage_counter
  - choose:
      - conditions:
          - condition: template
            value_template: low_wattage_counter = 10
        sequence:
          - type: turn_off
            device_id: 7094747e36478d716dd717c94952e5e6
            entity_id: 8777f42ad86162c5c3a24e0d4ddc1386
            domain: switch
mode: single

Your template isn’t a template. Luckily, what you want can be accomplished with a basic State condition:

...
  - choose:
      - conditions:
          - condition: state
            entity_id: counter.low_wattage_counter
            state: "10"
        sequence:
....

You may also want to consider adding a service call to reset the counter after the switch is turned off so that it’s ready for the next time.

1 Like

Thanks again. I find the concept of a Home Assistant template to be very confusing. I’ve read and re-read the template documentation and it hasn’t clarified much for me.