Energy Card

@hmerk Can you please post the latest code or is the code updated in the first post? I was not able to see the code over the Martket Place, is this even possible.

Is there actually somthing like a Blockly to DSL converter, as I would need DSL code :grinning: :grinning:

The rule templates in the marketplace are updated.

No, Blockly generates Javascript code. It cannot directly be converted.

Can you please send the updated Rules Code gain, like here:

Don’t know how I could get the code from the market place.

The rules are in the other topics i posted…
I will check later if I can find my old DSL code…

You need to install the templates…

See https://www.openhab.org/docs/concepts/rules.html#rule-templates and https://www.openhab.org/docs/tutorial/rules_basic.html#rule-templates.

For these templates you need the JS Scripting add-on installed.

At a high level the steps are:

  1. Navigate to Settings → Automation (OH 4.0) or Add-on Store → Automation (OH 4.1+)

  2. Under “Rule Templates” find “Actual Energy Consumption” and “Historical Energy Consumption”

  3. Click “Add” and confirm you want to install for both templates. Now you can create one or more rules based on them.

  4. Navigate to Settings → Rules → + to create a new rule. Give the rule a meaningful UID and name.

  5. Choose one of the rule templates (e.g. Actual Energy Consumption) and fill in the properties.

  1. Click save or hit ctrl-s to save the new rule based on the template.

  2. Repeat 4-6 for Historical Energy Consumption.

Rule templates are a huge improvement over sharing blocks of code that end users need to copy/paste/edit to get working for both the developers and the end users. For example, as an end user you don’t even need to type in the names of the Items. You can select them from a list.

1 Like

Yes I know, but how can I see the rule code :grinning: I want to change it :slight_smile:

When you create a new rule from the template, you can change the blocks in the blockly editor.

Just to elaborate, a rule created from a template is a rule like any other. It can be viewed and changed like any other UI created rule.

I don’t have the Items to generate @hmerk’s rules yet but I do have a bunch of others. Here’s what a rule created from my Delay Start [4.0.0.0;4.9.9.9] template looks like once I’ve created the rule:

Note I use pure JS Scripting. You’ll see Blockly if you click on the script action to edit it.

The whole rule in the code tab:

configuration:
  pause: ""
  rules:
    - humidity_low_detect
    - thing_status
triggers:
  - id: "1"
    configuration:
      startlevel: 40
    type: core.SystemStartlevelTrigger
  - id: "2"
    configuration:
      startlevel: 70
    type: core.SystemStartlevelTrigger
conditions: []
actions:
  - inputs: {}
    id: "3"
    configuration:
      type: application/javascript
      script: >-
        var {helpers} = require('openhab_rules_tools');

        console.loggerName = 'org.openhab.automation.rules_tools.Delayed Start';

        //osgi.getService('org.apache.karaf.log.core.LogService').setLevel(console.loggerName, 'DEBUG');

        helpers.validateLibraries('4.1.0', '2.0.1');


        // Properties

        var disabledRules = '[hum-low-detect, sensor-status-detection, service-status-detectio, thing_status]'.replace('[','').replace(']', '').split(', ');

        var delay = 'PT1M30S';


        // First time the rule is called at runlevel 40 the flag will be initialized to false

        var alreadyCalled = cache.private.get('flag', () => false);

        var timerTime = (alreadyCalled) ? delay : 0; // run immediately the first run of the rule

        var activity = (alreadyCalled) ? 'enabling' : 'disabling';

        var when = (alreadyCalled) ? 'after ' + delay : 'immediately';


        console.info('Delayed Start triggered, ' + activity + ' rules ' + when);


        helpers.createTimer(timerTime, () => disabledRules.forEach(rule => {
          console.info(activity, rule);
          try {
            rules.setEnabled(rule, alreadyCalled);
          }
          catch(e) {
            console.error('Failed to enable/disable rule ' + rule + ': ' + e);
          }
        }), 'delayedStart');

        cache.private.put('flag', true);
    type: script.ScriptAction

Except for the configuration block at the top, this rule is indistinguishable from a rule created by hand in every meaningful way. The template gives you a complete rule and what you do with the rule once it’s created is up to you.

If I create a Rule like this and click “Save”, then I get a completely empty rule. What am I doing wrong?

You haven’t filled in the Template Configuration. Both “Rules” is requires property. It won’t create the rule without all required properties filled in.

It should generate an error though instead of creating an empty rule. That’s a bug that needs to be filed.

In the mean time, make sure to fill in all the required properties for any rule you create from a template.

Yes, you are right! Thanks for clarification!

Ok. This is my DSL code now:

Actual (without billing cycle):

itemEnergyToday.postUpdate(itemEnergyReading.deltaSince(now.withHour(0).withMinute(0).withSecond(0), "jdbc") as Number)
itemEnergyThisWeek.postUpdate(itemEnergyReading.deltaSince(now.minusDays(now.dayOfWeek.getValue - 1).withHour(0).withMinute(0).withSecond(0), "jdbc") as Number)
itemEnergyThisMonth.postUpdate(itemEnergyReading.deltaSince(now.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0), "jdbc")as Number)
itemEnergyThisYear.postUpdate(itemEnergyReading.deltaSince(now.withMonth(1).withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0), "jdbc") as Number)

Historical, but I have used deltaBetween().

itemEnergyYesterday.postUpdate(itemEnergyReading.deltaBetween(now.minusDays(1).withHour(0).withMinute(0).withSecond(0),now.withHour(0).withMinute(0).withSecond(0), "jdbc") as Number)
itemEnergyLastWeek.postUpdate(itemEnergyReading.deltaBetween(now.minusDays(now.dayOfWeek.getValue - 1).minusWeeks(1).withHour(0).withMinute(0).withSecond(0),now.minusDays(now.dayOfWeek.getValue - 1).withHour(0).withMinute(0).withSecond(0), "jdbc") as Number)
itemEnergyLastMonth.postUpdate(itemEnergyReading.deltaBetween(now.minusMonths(1).withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0),now.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0), "jdbc") as Number)
itemEnergyLastYear.postUpdate(itemEnergyReading.deltaBetween(now.minusYears(1).withMonth(1).withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0),now.withMonth(1).withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0), "jdbc") as Number)

The only issue what have found is, that if you want to have ThisYear/LastYear data you also have to have energy readings in January.

What unit definition are you actually using, like this?

Add Metadata → State Description → Pattern → %.2f kWh

Why do have a “,” instead a “.”. Can this be changed?

Where is that?

Here:

Those are the system wide settings, nothing hardcoded in the widget.

Strange. You have “Number” items and State Description set like this?

Add Metadata → State Description → Pattern → %.2f kWh

Yes, 2f and 3f

Do you have any idea, which system wide settings to change?

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.