ECMAScript how to set a DateTime Item

How do I use JavaScript to set an Item of DateTime type to the date and time of now? Can someone please post an example?

Thanks

It seems like this is the solution:

events.sendCommand('sensor1_MotionWhen',  new Date().toISOString());

My rule ended looking like this:

triggers:
  - id: "1"
    configuration:
      itemName: sensor1_MotionAlarm
      state: ON
    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);

        events.sendCommand('sensor1_MotionWhen',  new Date().toISOString());
    type: script.ScriptAction

I’ve also implemented the same rule in a NodeRED flow that watches for a change in my sensor1_motionAlarm Item. Works as well. Decided to keep my NodeRED rule and ditch the internal OH one, as NodeRED to me is almost self-documented given the visual aspect of it.

To be honest, I’m wondering why use a DateTime Item if I could just as well use a String Item.

From what I’ve learned from the forums as I tried to solve this, if you’re not willing to learn and use Rules DSL, but intending to use Javascript (ECMAScript) then OpenHAB’s native types seem to be an overhead with little benefit.

Yes, I read about Helper Libraries, which apparently aren’t fully OH3 ready for Javascript. Seems like the effort to use them in most cases is larger than writing your small piece of code to do the things they help you with.

Maybe my use cases are too simple, hence I’m not seeing the value? I don’t know…

Unless you particularly want a command, you’d normally use a postUpdate to set an Item state.

Thanks @rossko57 . I’ve been using sendCommand as I learned it from watching what code Blocky built.

It would be good if some volunteer (I might try) starts to write a Javascript/ECMAScript section in the documentation, with those details. Similar to Rules | openHAB for Rules DSL. Maybe inside it, as a separate section.