Numeric state of event

This seems very obvious, but I can’t seem to find it… How can I extract the number out of an event itemState which contains a unit?

This rule:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: zonnepanelen_Current_Output
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: >-
        var javaobject = this.event;


        Object.keys(javaobject).forEach(k => {
          if (k != "equals") {
            var v = this.event[k];
            console.log("javaobject" + "." + k + (typeof v === "function" ? "() = " + v() : " = " + v));
          }
        });

        console.log("event.itemState = "+event.itemState);

        console.log("typeof event.itemState = "+typeof event.itemState);

yields this output:

18:24:55.203 [INFO ] [omation.script.ui.donker-zonnepanelen] - javaobject.getOldItemState() = 1
18:24:55.205 [INFO ] [omation.script.ui.donker-zonnepanelen] - javaobject.getLastStateUpdate() = 2025-12-11T18:21:12.237252885+01:00[Europe/Brussels]
18:24:55.206 [INFO ] [omation.script.ui.donker-zonnepanelen] - javaobject.getLastStateChange() = 2025-12-11T18:21:12.237252885+01:00[Europe/Brussels]
18:24:55.208 [INFO ] [omation.script.ui.donker-zonnepanelen] - javaobject.toString() = Item 'zonnepanelen_Current_Output' changed from 1 W to 0 W
18:24:55.209 [INFO ] [omation.script.ui.donker-zonnepanelen] - javaobject.getType() = ItemStateChangedEvent
18:24:55.210 [INFO ] [omation.script.ui.donker-zonnepanelen] - javaobject.getItemName() = zonnepanelen_Current_Output
18:24:55.212 [INFO ] [omation.script.ui.donker-zonnepanelen] - javaobject.getTopic() = openhab/items/zonnepanelen_Current_Output/statechanged
18:24:55.213 [INFO ] [omation.script.ui.donker-zonnepanelen] - javaobject.getSource() = null
18:24:55.214 [INFO ] [omation.script.ui.donker-zonnepanelen] - javaobject.getPayload() = {"type":"Quantity","value":"0 W","oldType":"Quantity","oldValue":"1 W","lastStateUpdate":"2025-12-11T18:21:12.237252885+01:00[Europe/Brussels]","lastStateChange":"2025-12-11T18:21:12.237252885+01:00[Europe/Brussels]"}
18:24:55.216 [INFO ] [omation.script.ui.donker-zonnepanelen] - javaobject.hashCode() = -2106727463
18:24:55.218 [INFO ] [omation.script.ui.donker-zonnepanelen] - event.itemState = 0
18:24:55.219 [INFO ] [omation.script.ui.donker-zonnepanelen] - typeof event.itemState = object

How do I extract the number 0?

Which version of OH are you running?

In OH 5.1+ the event Object is now wrapped and converted to JS for you same as it’s done for file based rules. See JavaScript Scripting - Automation | openHAB and there is a link after that table which is useful too: JavaScript Scripting - Automation | openHAB.

From this we learn that newState et. al. are all Strings. So to get the number or Quantity you need to parse it.

let newStateNum = parseFloat(event.newState);
let newStateQuantity = Quantity(event.newState);

If running in OH 5.0 or before or with the new event Object conversion turned off (Settings → JavaScript Scripting → * Convert Event from Java to JavaScript type in Script Actions & Script Conditions scripts) you get the raw Java event Object. This means they basically behave and are used the same as you’d do so in Rules DSL.

This is documented at JavaScript Scripting - Automation | openHAB.

Take note of the warning:

Note that in UI based rules event.itemState , event.oldItemState , and event.itemCommand are Java types (not JavaScript), and care must be taken when comparing these with JavaScript types:

NOTE : Even with String items, simple comparison with == is not working as one would expect!

You can go through the pain of working with the raw Java Objects and classes, but it’s almost always easiest to convert it to a JS String and then deal with the String.

var itemStateNum = parseFloat(event.itemState.toString());
var itemStateQuantity = Quantity(event.itemState.toString());

Right, I had read that somewhere. So I’ll wait a bit :slight_smile:

Coming back to this, now that I’m on OH5.1:

console.log("Quantity(event.newState) = "+Quantity(event.newState));
console.log("Quantity(typeof event.newState) = "+typeof Quantity(event.newState));

… logs:

17:32:45.229 [INFO ] [.jsscripting.rule.donker-zonnepanelen] - Quantity(event.newState) = 34 W
17:32:45.230 [INFO ] [.jsscripting.rule.donker-zonnepanelen] - Quantity(typeof event.newState) = object

parseFloat does the trick:

console.log("parseFloat(event.newState) = "+parseFloat(event.newState));
console.log("parseFloat(typeof event.newState) = "+typeof parseFloat(event.newState));

… logs:

17:35:56.402 [INFO ] [.jsscripting.rule.donker-zonnepanelen] - parseFloat(event.newState) = 35
17:35:56.404 [INFO ] [.jsscripting.rule.donker-zonnepanelen] - parseFloat(typeof event.newState) = number