Automation to detect high power and run automation but ignore spikes

I want to run an automation when my EV car charger starts and then stops. The trouble is that there are times when it can spike for 1 minute which i want to ignore. The sensor gets updated every 30 secs.

I have built these two automations, the first one works great for when power increases:

- id: '1725743193929'
  alias: EV Socket is using more than 2kW (for 1 min)
  description: Detect when EV charging has started
  trigger:
  - platform: numeric_state
    entity_id:
    - sensor.sdm120_active_power
    above: 2000
    for:
      hours: 0
      minutes: 1
      seconds: 0
  condition: []
  action:
  - action: notify.mmyemail_gmail_com
    metadata: {}
    data:
      message: EV Socket is using more then 2kW at {{now().strftime("%H:%M %Y-%m-%d")}}
        active power is {{ states('sensor.sdm120_active_power') }}
      title: EV Socket is using more then 2kW at {{now().strftime("%H:%M %Y-%m-%d")}}
        active power is {{ states('sensor.sdm120_active_power') }}
      target: [email protected]
  mode: single

but this one runs for every spike:

- id: '1725787889752'
  alias: EV Socket is using less than 1W (for 1 min)
  description: Detect when EV charging has finished
  trigger:
  - platform: numeric_state
    entity_id:
    - sensor.sdm120_active_power
    for:
      hours: 0
      minutes: 1
      seconds: 0
    below: 1
  condition: []
  action:
  - action: notify.myemail_gmail_com
    metadata: {}
    data:
      message: EV Socket is using less than 1W at {{now().strftime("%H:%M %Y-%m-%d")}}
        active power is {{ states('sensor.sdm120_active_power') }}
      title: EV Socket is using less than 1W at {{now().strftime("%H:%M %Y-%m-%d")}}
        active power is {{ states('sensor.sdm120_active_power') }}
      target: [email protected]
  mode: single

Is there a way to have a single automation which has an action when it is detected and then runs when the condition returns to normal ?

There is no spike when itā€™s below 1 W. Itā€™s no power but I guess there is still some power. Check how your value is moving in the history and adapt the min. power.

Like your car never takes less then xxx W when charging.
I would put below 100W

1 Like

If the actual question is, ā€˜can i combined this into a single automation?ā€™, then from what I can see, this looks pretty straightforward.

Add both triggers to a single automation and give each a trigger id.

Then use an if or choose condition to run the appropriate action, based on the trigger id.

1 Like

Have a look at Filter - Home Assistant :wink:

1 Like

thanks @aceindy @Rofo @zigomatic

I think what i meant to say was:

  • There is a scenario where the car gets plugged in and it draws power for 1-2 minutes and I do not want any automation to run at all (on the up ā€œleading edgeā€ or on the down ā€œtrailing edgeā€).

  • The key senario i want to be able to run two automations on is:
    a) When it starts (and it has been drawing above 2000 watts for 1-2 minutes)
    b) When it finishes (when it returns to less than 1W).

Thanks in advance :slight_smile:

ok ?

The question is how to identify that scenario is happening ?
Itā€™s clear for me that you need to check history of the value and identify which part you want to receive a notification and which one you donā€™t want.

And as proposed, not use the real value but the filtered value where you can remove what you donā€™t want to see.

1 Like

So, I think what you are saying is that in the first 1-2 minutes there might be a spike of energy above 2000watts and you want to ignore that, you just want it to trigger when the power draw is at a steady stead above 200watts ?

If this is the case, then this should be fairly straightforward. You could extend this:-

    for:
      hours: 0
      minutes: 1
      seconds: 0

To be (3 minutes), but you might miss a minute or two of the steady state (not sure how consistent the power draw pattern is).

Alternatively, use something like the (low pass) filter as linked above, some other examples of how to use a filter are here:-

1 Like

Thanks to everyone for trying to help. I thought that this would do the trick:

sensor:
  - platform: filter
    name: "SDM120 Active Power Time Simple Moving Average (2 min)"
    entity_id: sensor.sdm120_active_power
    filters:
      - filter: time_simple_moving_average
        window_size: "00:02"
        precision: 2

BUT !

The source entity (sensor.sdm120_active_power) is from Modbus and that only seems to create a new datapoint in HA whenever there is a change. So any kind of filter based on average data isnā€™t going to work.

Any more ideas?

EDIT 1: Looks like somebody else had this issue and no reply:

EDIT 2: I also tried ā€œforce_update: trueā€ as per this thread and doenst work in modbus:

EDIT 3: Also tried to do a force refresh via this for both the modbus entity and the filter entity and no impact:

Just an update - i completely failed to get anything working in automation or filters due to the lack of datapoints in a time window. It would seem that the sdm120/modbus integration returns zero then there no more datapoints in HA that can be used (it can be like that for hours if the value doesnā€™t change).

I did manage to get a template binary_sensor time working great, this is my final solution:

template:
  - binary_sensor:
      - name: "EV Charging Socket"
        unique_id: "EV Charging Socket"
        device_class: battery_charging
        delay_on:
          seconds: 60
        delay_off:
          seconds: 60
        state: "{{ states('sensor.sdm120_active_power') | float > 2 }}"

Thanks everyone for helping.