How to change Astro binding datetime format?

  • Platform information:
    • Hardware: Orangepi 3 LTS
    • OS: Armbian
    • openHAB version: 3.4.3

I am calling the Astro binding like so:

items.getItem("sunRise").state;

This produces the following:

2023-05-01T05:11:00.000+0300

I would need the datetime to be in the following format

Tue May 02 2023 00:00:00 GMT+0300 (EEST)

Is it possible to format the output of the Astro binding to conform to the latter format? If so, how can I do that?

By default all DateTime Items use ISOP8601 format (if JS Scripting) or an RFC format in all other cases (the main difference is the RFC uses the timezone name instead of an offset).

No, because the Astro binding has nothing to do with that.

Where do you want this format? In a widget on MainUI or a sitemap? If so, set the Item’s State Description metadata Pattern field following Items | openHAB and Formatter (Java SE 11 & JDK 11 ).

I am “developing” the heat pump control system created by @masipila Control a water heater and ground source heat pump based on cheap hours of spot priced electricity - #17 by masipila. The existing code calculates needed heating hours based upon average outdoor temperature and windchill. I would like to add compensation for passive heating caused by sunshine, too. I try to keep my house indoor temperature at 19-20 C, but on sunny days the temperature rises to 25 C. Hence, I would like to reduce the hours the heat pump is running on sunny days.

You can find the actual implementation at #13 of this megathread. The actual code I am trying to interact with is nibe.js that can be found under " 8. HELPER MODULES USED IN THE SCRIPT ACTIONS ABOVE in #13 of that thread.

The script is using a midnight to midnight timeslot to calculate average temperature over the day. I have copied the function with the intention of calculating average sunshine hours per day based upon a) the cloudiness forecast from the finnish national meterorolgical bureau FMI and b) the sunset and sunrise times from the Astro binding.

I’m not going to analyze all of the code posted there but from what I can see:

  1. everything is written in GraalJS JS Scripting rules
  2. the code is packaged as a node module
  3. all the date time Objects being created and used within the code are JavaScript Date Objects.

You don’t need a different string representation of the Astro DateTime, you need to convert the DateTimeType which is the state carried by a DateTime Item to a JS Date Object.

See JavaScript Date Objects for everything you can do with a JS Date and https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/ZonedDateTime.html for everything you can do with the ZonedDateTime carried by the DateTimeType. It’ll look something like:

    var sunriseZDT = items.getItem('sunRise').rawState.getZonedDateTime(); // note will fail is Item is NULL or UNDEF
    var sunriseDate = new Date(sunriseZDT.getYear(), sunriseZDT.getMonthValue() ...