Rule script failing to run

Hi All, Ive setup the following rule:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: all_time_energy_solar
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: |-
        (function(i) {
            var total = (parseFloat(i) * 0.38);
            logInfo("SOLAR","total for solar - " + total);
            items.getItem('total_energy_savings_amount').postUpdate(total);
            return;
        })(input)
    type: script.ScriptAction

Unfortuntely in the log it just shows as failed, is there any clear reason why it isnt working?

total_energy_savings_amount is just a number type item.
The rule is triggered and all_time_energy_solar is updating every 10 seconds.

This seems to be a JavaScript fonction while it is expected as a dsl :

type: application/vnd.openhab.dsl.rule

In addition to the mime type being wrong as @glhopital points out, the code itself looks like a transformation, not a script action. There is no such variable input in a script action. You have event instead.

Did you try to use a chatbot to create this rule? Mixing up rules languages like this (logInfo also doesn’t exist in JS Scripting) is one of the many reasons these chatbots are worthless for OH rules.

Always post the error. Failing to do so is like going to the Dr. and refusing to tell them where it hurts.

I recommend the following:

  1. Create a new rule
  2. Read and understand the UI Rules section of the JS Scripting Docs
  3. In the new rule, when you create an Inline Script Action, choose JS Scripting from the list of languages. If you don’t have that as an option, you haven’t installed the JS Scripting add-on.
  4. The code should look something like:

(Assuming you are not using units)

var total = event.itemState.floatValue * 0.38;
console.info("total for solar - " + total);
items.total_energy_savings_amount.postUpdate(total)

If you are using units we need more info.

Or if you really mean this to be a transformation:

  1. Create a new JS Script transformation (Settigns → Transformations)
  2. The code should look something like this:
var total = parseFloat(input) * 0.38;
console.info("total for solar - " + total);
return total;

All that function stuff doesn’t add anything in an OH context, there is no global environment to pollute so there’s no need to create a self executing function. But if you insist:

(function(reading) {
  var total = parseFloat(reading) * 0.38;
  console.info("total for solar - " + total);
  return total;
})(input)

If you get rid of the log statement it can be an inline script transform.

JS:| parseFloat(input) * 0.38
  1. Add a link between the Channel that feeds “all_time_energy_solar” to “total_energy_savings_amount” and add a Script profile that calls the transformation created in 2 as the “Thing to Item” transformation.

You’ve not provided enough details to say which approach is better.

If you are suing modbus, it comes with an offset profile you can use instead of a custom JS Scripting rule or transformation.