Convert now.getWeekOfWeekyear for OH3

Hello,
I need your help because I’m disastrous with java.

I need to convert the openhab 2 instruction now.getWeekOfWeekyear
which return the number of the week (business speaking is very used to comunicate delivery estimation etc…) to OH3.

according to the OH3 Release documentation

  • getHourOfDay → getHour, getMinuteOfHour → getMinute, getMonthOfYear → getMonthValue, getDayOfWeek → now.getDayOfWeek.getValue

I havn’t find nothing about now.getWeekOfWeekyear → ?

I also search Here (java time docs) but as I said I’m not able to understand nothing about how to implement it …

many many thanks in advance guys.

I think this should work:

Hello,
I had already try to copy-paste the istruction on these page. I’m not able to understand Java, so i’m not able to manage eventual little adaptation.

I try inside a script these without success:

ZoneId zoneId = ZoneId.of ( "America/Montreal" );
ZonedDateTime now = ZonedDateTime.now ( zoneId );
int week = now.get ( IsoFields.WEEK_OF_WEEK_BASED_YEAR );
int weekYear = now.get ( IsoFields.WEEK_BASED_YEAR );

test.postUpdate(weekYear)

Or

ZoneId zoneId = ZoneId.of ( "America/Montreal" );
ZonedDateTime now = ZonedDateTime.now ( zoneId );
int week = now.get ( WEEK_OF_WEEK_BASED_YEAR );
int weekYear = now.get ( WEEK_BASED_YEAR );

test.postUpdate(weekYear)

logs gives

[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘test’ failed: var weekYear = now.get( ___ WEEK_BASED_YEAR)

I Think this is the implementation based on the stackoverflow example:

import java.time.temporal.IsoFields

    rule "Test"
    when
        Item swTestSwitch changed from OFF to ON 
    then
        val int week = now.get ( IsoFields.WEEK_OF_WEEK_BASED_YEAR )
        val int weekYear = now.get ( IsoFields.WEEK_BASED_YEAR )
        logInfo("Test", "Now = " + now.toString)
        logInfo("Test", "Week = " + week.toString)
        logInfo("Test", "WeekYear = " + weekYear.toString)
    end

For your own Timezone it should be so ok. Hope it’s helpful.

2 Likes

Thank you,
the code works if I copy it inside a .rule file but the “body” doesn’t work as script (don’t know why)

Read this topic about an issue