Automation: trigger when variable is '>40' for a period of time

So, I have an automation which turns our airco on when we supply enough power back to our solar panels. This is measured at a value that our P1 meter gives: when we supply more than 1200w, switch on the air conditioning.

At the same time, an automation is active that switches the air conditioning off again when we start using electricity (>40w, this is so low because we also have an energy storage whose values ​​cannot be read by HomeAssistant)

Now it is the case that the air conditioning is regularly switched on and immediately switched off again. Very irritating.
for now I have simply installed a timer that determines the shutdown every 5 minutes to prevent constant switching on and off.

question: I would like to have the air conditioning switched off if the value behind ‘sensor.p1_meter_active_power’ is higher than 40w for a period of 5 minutes. How can I include this in the automation?

Turn the airco on

alias: Turn airo on
description: ""
trigger:
  - platform: time
    at: "06:00:00"
  - platform: time
    at: "18:30:00"
  - platform: state
    entity_id:
      - sensor.p1_meter_active_power
condition:
  - condition: and
    conditions:
      - condition: device
        device_id: 9978e82e13d276cc0acdfd4c6b2992a3
        domain: climate
        entity_id: climate.airco_child
        type: is_hvac_mode
        hvac_mode: "off"
      - condition: numeric_state
        entity_id: sensor.airco_child_room_temperature
        above: 21
  - condition: numeric_state
    entity_id: sensor.p1_meter_active_power
    below: -1200
action:
  - service: climate.set_temperature
    metadata: {}
    data:
      hvac_mode: cool
      temperature: 20
    target:
      device_id: f5b84d5290f1b0154c3f74a8f28f773a
  - service: input_datetime.set_datetime
    target:
      entity_id: input_datetime.airco_child
    data:
      datetime: "{{ now() + timedelta(minutes = 5) }} "
  - device_id: 26a429cf3ab32619274f9ae6346c6ce7
    domain: mobile_app
    type: notify
    message: Cooling - 20 degrees
    title: Airco Child
mode: single

Turn the airco off

alias: Child | Airco off
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.p1_meter_active_power
condition:
  - condition: numeric_state
    entity_id: sensor.p1_meter_active_power
    above: 40
  - condition: device
    device_id: f5b84d5290f1b0154c3f74a8f28f773a
    domain: climate
    entity_id: 8feeeef7f80c94fbaa13fd27ffc3bf03
    type: is_hvac_mode
    hvac_mode: cool
  - condition: time
    after: input_datetime.airco_child
action:
  - service: climate.turn_off
    metadata: {}
    data: {}
    target:
      device_id: f5b84d5290f1b0154c3f74a8f28f773a
  - service: input_datetime.set_datetime
    target:
      entity_id: input_datetime.airco_child
    data:
      datetime: "{{ now() + timedelta(minutes = 5) }} "
  - device_id: 26a429cf3ab32619274f9ae6346c6ce7
    domain: mobile_app
    type: notify
    message: Airco Child Off
mode: single

You can simply do:

trigger:
  - platform: numeric_state
    entity_id: sensor.p1_meter_active_power
    above: 40
    for: '00:05'

I’d also recommend swapping out all of your device conditions and actions for entity-based ones, but get the above change working first.

That’s so easy!
Thank you…