P1 meter to virtual zero export to CT/peak shaving only runs once, not continuously

If this isn’t the correct sub-forum, please let me know.

Short background on me: Been using HA for several years but I can’t say I understand the difference between entities and devices, nor how to script things outside of an automation.

Problem description: Several buildings behind one energy meter, yet only one of them has solar panels and inverter installed. The energy meter has a wifi P1 meter so HA knows how much power is being imported or exported at all times, but the inverter does not. The inverter does support peak shaving to CT and zero export to CT, but due to distance between where the inverter is situated and where the CT needs to be to capture all buildings, installation isn’t straight forward. If I manually look in HA on the P1 meter export and manually compute a PV max power limit, I can do virtual zero export to CT when electricity prices are negative, but I wish to automate this.

In pseudo-C-code, virtual zero export to CT would look something like this:

while (sensor.electricity_sale_price < 0){
  entity_id.number.inverter_pv_max_power = sensor.inverter_pv_power + sensor.p1_meter_active_power}

In my efforts, the problem is that sensor.electricity_sale_price only updates once every hour, so the calculation in my attempted automation only runs once. I have a feeling I need a helper or some other method than a straight-up automation to make this happen.

If I can make virtual zero export to CT happen, I should be able to figure out how to make virtual peak shaving happen too.

How to make the equivalent of a while-loop in HA?

Here is what I’ve tried:

alias: Zero export to CT
description: Zero export to CT
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.electricity_sale_price
    attribute: current_price
    below: 0
    for:
      hours: 0
      minutes: 59
      seconds: 55
conditions: []
actions:
  - action: number.set_value
    metadata: {}
    data:
      value: >-
        {{ states('sensor.p1_meter_active_power') | float
        +states(('sensor.inverter_pv_power')) | float }}
    target:
      entity_id: number.inverter_pv_max_power
mode: restart

The problem is that it only runs once once it triggers, like my previous attempts. “mode: restart” seems to be the obvious choice, but no difference observed between restart and single.

Problem solved. If anyone has the same issue, here is my YAML code to make it happen. Substitute the sensor names for yours.

alias: Zero export to CT
description: Zero export to CT
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.electricity_sale_price
    attribute: current_price
    below: 0
    for:
      hours: 0
      minutes: 59
      seconds: 50
conditions: []
actions:
  - repeat:
      sequence:
        - action: number.set_value
          metadata: {}
          data:
            value: >-
              {{ states('sensor.p1_meter_active_power') | float
              +states(('sensor.inverter_pv_power')) | float }}
          target:
            entity_id: number.inverter_pv_max_power
        - delay:
            seconds: 10
      until:
        - condition: numeric_state
          entity_id:
            - sensor.electricity_sale_price
          attribute: current_price
          above: 0
mode: restart