Blockly Datetime always has timezone, how can I remove it?

Hi All,

I am trying to create a datetime element set to midday on today’s date.
Here is a simple example:

This is the result in the logs:
2025-05-02 16:52:55.198 [INFO ] [nhab.automation.script.ui.926e58b43b] - 2025-05-02T12:00+02:00[Europe/Zurich]

When I look in my MariaDB, the entry is persisted at 10:00 (and sometimes 12:00 which I do not understand!).

Is there a way to create a datetime object and not a ZonedDateTime object?

Thanks in advance

That wouldn’t do you any good since the call to .persist requires a ZonedDateTime; Persistence | openHAB

<item>.persist(ZonedDateTime, State) Persists a past or future state for the Item

Furthermore, a ZonedDateTime is a datetime Object so I’m not sure what other Object you are wanting to use instead.

Are the timezones the same in the DB entries that are different? I wouldn’t be surprised if MariaDB saves everything using UTC or even epoch but maybe it’s not consistent with the timezone.

Another way to create a date time of today at noon is:

image

Hi @rlkoshak,

Ok, your version works better… but not perfectly (in my case).
Here is my actual blockly with your suggestion:

The idea is that I run this regularly, and it updates my electricity generated amount during the day.
I want just a single value persisted in my DB for each day, and I want this to be at midday which aligns nicely in a bar chart analyze.

Each time I run the above blockly, the milliseconds persisted are different:
2025-05-02 18:19:50.887 [INFO ] [ation.script.ui.UpdateDailyGenerated] - Updating Daily KwH Generation, delta is: 3.551 kWh at:2025-05-02T12:00:00.876+02:00[Europe/Zurich]
2025-05-02 18:24:53.177 [INFO ] [ation.script.ui.UpdateDailyGenerated] - Updating Daily KwH Generation, delta is: 3.551 kWh at:2025-05-02T12:00:00.166+02:00[Europe/Zurich]

This looks like the set 0 milli is not working.
I tried different combinations (just milli, just nano etc.)

The end result in my mariadb is this:

if I could get the time to be 12:00:00.000 then it would overwrite the existing value as more electricity is generated.

Any suggestions to make this work correctly?

Show the JS generated by the code. Everything is being set to 0 so it should be 0.

If I were writing this with plain JS I’d just use time.toZDT("12:00") to get noon today. Blockly seems to make it a little harder.

Hmm interesting!

    midday =
    (time.ZonedDateTime.now()).withHour(12).withMinute(0).withSecond(0);

So the mili, micro etc is ignored!

I will continue to play… and post any success!

What if you just do nano?

Ok, looks like a bug to me:

Produces this code:

        midday = (time.ZonedDateTime.now()).withHour(12);

        midday = midday.withMinute(0);

        midday = midday.withSecond(0);

        midday = midday;

        midday = midday;

        midday = midday;

Yep. Please file an issue on the openhab-webui repo. How to file an Issue

In the mean time, you can try to make it work using the block you originally used (that should work too) or use an inline script block with midday = time.toZDT("12:00");

One last update on this topic,

This blockly is interpreted to the below code:

var midday;
        midday = (time.ZonedDateTime.now()).withHour(12);
        midday = midday.withHour(12);
        midday = midday.withMinute(0);
        midday = midday.withSecond(0);
        midday = midday.withNano(1000000);
        midday = midday.withNano(1000);
        midday = midday.withNano(1);
        midday = midday;
        midday = midday;
        midday = midday;
        console.log(midday);```

So, the micro, nano and milli work fine for non zero values, but fail for 0.

However, for my purposes to store 12:00:00.000 to my persistence service this is good enough.
If I specify withNano(1) it produces this, and when rounded to 3 DP it works well.
2025-05-04T12:00:00.000000001+02:00[Europe/Zurich]