Solaredge Power excess Rule on/off Shelly for a minimum of time

Openhab 3, on a Raspberry 4 with opanhabian
rrd4j is installed without any changes

Hi folks, I’m a greenhorn in setting up rules and hope you can guide me…
I managed to get live date from my Powermeter over modbus tcp binding and know want to turn off/on equipment behind a shelly p1 when my solarplant produceses enough power.
Lets say the shelly should turn on if there is a Total Real Power Value from +2000w over the last minutes (and run for at least one hour - this has yet to be added to the code…) and turn off, if there isn’t any Power Value. I spent the whole day finding solutions, starting with blockly and moving the code to an other rule, but just ended up with…

triggers:
  - id: "1"
    configuration:
      itemName: SolarEdgeSEMTR3Y400VA_TotalRealPowerValue
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: >-
        var logger =
        Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' +
        ctx.ruleUID);



        if (itemRegistry.getItem('SolarEdgeSEMTR3Y400VA_TotalRealPowerValue.averageSince(now.minusMinutes(10), "rrd4j"').getState() >= '2000') {
          events.sendCommand('WPPoolShelly1_Betrieb', 'ON');
          logger.warn('WP-Pool ON');
        } else if (itemRegistry.getItem('SolarEdgeSEMTR3Y400VA_TotalRealPowerValue.averageSince(now.minusMinutes(10), "rrd4j"').getState() < '2000') {
          events.sendCommand('WPPoolShelly1_Betrieb', 'OFF');
          logger.warn('WP-Pool OFF');
        }

The rule triggers, but fails to execute action 2
Apparently there are some mistakes in the code somewhere here I guess: ), “rrd4j”’).getState()

I hope you can help me
Of course there are solaredge pros’ who have already coded such things - but I wasn’t able to find rules which I coult adopt.

Any help is appreciated!

When developing, go one step at a time rather than try to do everything in one line. Get it working, you can always come back after and compress it if you must (though there’s not much point in making it unintelligible to yourself later on).

You’ve asked for an Item from the registry named ‘SolarEdgeSEMTR3Y400VA_TotalRealPowerValue.averageSince(now.minusMinutes(10), “rrd4j”’)`
I shouldn’t think you have one like that.
Try-
getting the item from the registry
then
getting the averageSince

So I made some steps backwords…
Thank you @rossko57 for your advice.

This rule works:

if (itemRegistry.getItem('SolarEdgeSEMTR3Y400VA_TotalRealPowerValue').getState() >= '2000') {
          events.sendCommand('WPPoolShelly1_Betrieb', 'ON');
          logger.warn('WP-Pool ON');
        } else if (itemRegistry.getItem('SolarEdgeSEMTR3Y400VA_TotalRealPowerValue').getState() < '2000') {
          events.sendCommand('WPPoolShelly1_Betrieb', 'OFF');
          logger.warn('WP-Pool OFF');

Next step - I guess - is to add the “averagesince” or maybe even better the minimum.since part. I would like to use the rrd4j which is preinstalled in OH3. Do I have to make changes there?
How can I do that?

What are you having trouble with?

var x = yourItem.somePersistenceFunction
if (x == this) {
   // do stuff
} else if (x == that) {
   // do other stuff
}

Reduces both overheads and opportunity for typos.

@rossko57 Thank you for the quick answer. Sorry, I’m an absolut beginner. Copy@Paste - try and error…
I’m gonna try…

So I tried this…

var minEnergy =
        SolarEdgeSEMTR3Y400VA_TotalRealPowerValue.minimumSince(now.minusMinutes(2)

        if (minEnergy >= 2000) {
           events.sendCommand('WPPoolShelly1_Betrieb', 'ON');
           logger.warn('WP-Pool Testregel minSince ON');
        } else if (minEnergy <= 2000) {
           events.sendCommand('WPPoolShelly1_Betrieb', 'OFF');
          logger.warn('WP-Pool Testregel minSince OFF');
        }

But get this…
2:0 Expected , but found if
^ in at line number 2 at column number 0

I checked different rules in the web. They all seem to be similar. Where is the mistake?

Isn’t there missing a closing ) in the var section? → …minus.Minutes(2)) But if I add a closing ), then I get this error:
ReferenceError: “SolarEdgeSEMTR3Y400VA_TotalRealPowerValue” is not defined in at line number 1

Hope someone can help… Thanks!

Yes, that’s a straightforward one.

You might find using VSCode with the openHAB extension a useful editor, if you plan on writing more than just trivial rules.

Have you an Item by that exact name? This stuff is all case sensitive.
Although I think your problem is trying to use DSL methods in a javascript rule.
Can we see your whole rule now? It has become a bit of a guessing game since the first post, with many changes.

Might be reading DSL rules (the vast majority of openHAB rule examples are in DSL) and copying parts to a different language - you’ve chosen to use javascript.
Try this example -

The entire rule:

triggers:
  - id: "1"
    configuration:
      itemName: SolarEdgeSEMTR3Y400VA_TotalRealPowerValue
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: >
        var minEnergy =
        SolarEdgeSEMTR3Y400VA_TotalRealPowerValue.minimumSince(now.minusMinutes(2))

        if (minEnergy >= 2000) {
           events.sendCommand('WPPoolShelly1_Betrieb', 'ON');
           logger.warn('WP-Pool Testregel minSince ON');
        } else if (minEnergy <= 2000) {
           events.sendCommand('WPPoolShelly1_Betrieb', 'OFF');
          logger.warn('WP-Pool Testregel minSince OFF');
        }
    type: script.ScriptAction

Yes…

EDIT: Gonna try DSL…

Alright, still using javascript so you will need to use PersistenceExtensions as per the example.

Note also that you can’t compare 500W with just 2000. 2000 what, kW, horsepower?

Copied the code into a new DSL-rule

Make your mind up. The javascript stuff like logger isn’t going to work in DSL either.

I managed to get a rule to use excess solar power to produce warm water with my heating rod (3000W) controlled by a shelly 1. Starts working with more than 32000W excess power (averageSince or minimumSince would surely make sence here, but I wasn’t able to do it) and works forewer (This is not really good and should be improved although my heating rod turns off automatically when the water in the tank reached 65 degrees celsius.). It turns off after 15 Minutes when there is no more than 2800W of excess power.

It’s actually my first code I wrote. Any advices to make this code better are appreciated.

if (itemRegistry.getItem('SolarEdgeSEMTR3Y400VA_TotalRealPowerValue').getState() >= '3200') {
   events.sendCommand('WPPoolShelly1_Betrieb', 'ON');
  logger.warn('WP-Pool ON');
} else if ((itemRegistry.getItem('SolarEdgeSEMTR3Y400VA_TotalRealPowerValue').getState() <= '2800') && (itemRegistry.getItem('WPPoolShelly1_Betrieb').getState() == 'ON')) {
             java.lang.Thread.sleep(900000); //5 Minuten Verzögerung für weitere Befehle
             events.sendCommand('WPPoolShelly1_Betrieb', 'OFF');
             logger.warn('WP-Pool nach 15 Minuten OFF');
}