Scripting with Blockely and Rules

Hello,

I am happily working with my Openhabian 3.2 running on a 2 Gbyte Raspi4.

I have written simple scripts with Blockely where I calculate specific PV parameters like kW/kWp for parts of my PV System for charting.

I am not quite sure if I am doing things correctly and have a question or two about scripting & rules.

I composed a Blockely thing where I compute stuff and write the result into an Item that I have freshly created as part of an existing thing or/and as part of a location. That seems to work and I guess that is ok?


image

I then compose a rule where I include the said script and execute it every 30 sec.

When I do that, I notice that I can not grab my Blockely script from “scripting” from the interface as it is not shown. As a workaround, I copy the code generated by Blockely and paste that into the rule-script window. Is that how it should be?

I then start my rule but am a bit surprised that is says “idle” even though it does its thing every 60 sec or so. How do I pause that script? Simply by pushing the pause button where it then says “uninitialised? I then havet o “unpause” it and then hit run to start it again. Is that OK?

If I then paste an update of the script and start it again, is then a new rule process started or is the old rule re-started? I am asking as I am a bit afraid that I might have a lot of rules running in the background that were never quite stopped and eventually flood the system.

I am happy about any advice or hint. thanks

That’s not how you are expected to write rules.

A Script is just a rule like any other. The only thing that makes it different is it consists of a single Script Action and is tagged with “Script” (no triggers, no conditions, nor more than one Action).

One of the things that a rule can do is call another rule. So if you had a Script that is somehow useful to be called from multiple rules, what you’ve done makes sense. There is an Action you can choose to call another rule and in Blockly there are blocks that can be used to call another rule.

However, if the script is intended to only be used by the one rule, just define it on that one rule.

It probably takes only a dozen milliseconds or so for the rule to run. It likely goes from running to idle faster than the UI can update the badges to show it was running. You can see the rules changing state between running and idle by watching the event stream in the developer sidebar (alt-shit-d) or by setting the logging to enable those to be logged to events.log.

Only long running rules (those that take half a second or more) would show in MainUI.

It’s OK but I think you have a miss understanding on how rules work. They are only running when actively processing an event. The rest of the time they are either idle (i.e. waiting to be triggered) or UNINITIALIZED (i.e. disabled). Click the pause icon and that disables the rule. Clicking the play button triggers the rule.

You don’t have to click play to cause the rule to start handling events. If it’s enabled (i.e. idle) it is already going to process the events that the rule defines. The play button is a way to manually cause the rule to trigger.

As soon as you hit save (ctrl-s) the changes will be applied to the rule.

Impossible. A rule is only running when it’s been triggered. Only one instance of a given rule can run at a given time. You can have no more rules running in your system than you have rules defined.

Thanks. I did not find a way to loop a script without a rule. I tried a loop with a count to 10000000 but that overfilled my log file which crashed ZRAM which crashed my influxdb´s data (should have included a delay…) - I have ordered a SSD now, having influxdb in Zram seems not to be wise for PV logging with the heavy datarates. Thanks or all the explanations.

Again, a script is a rule.

If you want to run something periodically, that’s what the Time rule triggers are for. Just create a rule, trigger it with a time cron trigger, and it will execute based on how you’ve configured the trigger.

It’s all 100% event driven. A rule triggers based on an event.

… I thought about a script that computes a value whenever the input value changes, that would save space. If I execute a script via a rule every 5 sec, but new data comes in 5 sec or in 50 sec, then the script is a bit a waste and the computed data suggests precisison which is not there (i,e, explicit sampled data is converted into implicit sampled data). But I get the idea, works for me.

In that case trigger the rule when the Item linked to the input value changes. No looping required.