Dynamically Setting a Group Item's Label

Platform Information

  • Hardware: Raspberry Pi 3B+
  • OS: Raspbian Buster
  • Java Runtime Environment:
    openjdk version “11.0.7” 2020-04-14
    OpenJDK Runtime Environment (build 11.0.7+10-post-Raspbian-3deb10u1)
    OpenJDK Server VM (build 11.0.7+10-post-Raspbian-3deb10u1, mixed mode)
  • openHAB 2.5.5 (Release Build)

Hello OpenHAB Community,

I am using the OpenWeather binding to display multi-day forecasts in a sitemap, with each day’s forecast in a separate frame. I’m using groups to do this, but the labels for the groups soon become rather confusing to the user. To overcome this, I’ve created a rule, which I’ve successfully implemented as a test for “Today”. See below.

One thing that puzzles me though is why the following does not set the label of gTodays_Weather_Forecasts as expected.

gTodays_Weather_Forecasts.label = gTodays_Weather_Forecasts_Label.label  // Results in "Label: Weather Forecasts for", instead of "Weather Forecasts for Wednesday, May 27, 2020 (Today)"

Does anyone have any explanations?

Items Snippet

DateTime Weather_Forecasts_Date "Date [%1$tA, %1$tB  %1$td, %1$tY]"
String gTodays_Weather_Forecasts_Label "Weather Forecasts for [%s] (Today)"
Group gTodays_Weather_Forecasts "Today's Weather Forecasts" <weather> (gDaily_Weather_Forecasts)

Rule

rule "Update Label Test"  // Test for dynamically updating the label of Today's weather frame.
  when
    Time cron "0/30 0/1 * 1/1 * ? *"  // Fires every 30 seconds for test puposes since I don't want to wait until midnight
    //Time is midnight
  then
    Weather_Forecasts_Date.postUpdate(now.plusDays(0).toString)
    gTodays_Weather_Forecasts_Label.postUpdate(Weather_Forecasts_Date.state.format("%1$tA, %1$tB  %1$td, %1$tY").toString)
    gTodays_Weather_Forecasts.label = String::format("Weather Forecasts for %s (Today)", gTodays_Weather_Forecasts_Label.state)
    logInfo("openweather.rules", "Update Label Test rule completed")
end

Regards,
Burzin

Could you perhaps tell us what you actually get that you do not expect?

However, there’s no reason to think that gTodays_Weather_Forecasts_Label.state has the value you expect when you use it.
postUpdate is asynchronous, the request goes on to openHABs event bus, and eventually the Item state actually gets updated. It likely only takes a few milliseconds, but the rule does not stop and wait.
That means that if you read the same Item’s state in the next line you will almost certainly get the “old” value.
That’s fine, you don’t need to read back the state because you already know exactly what you just postUpdated to it.

Note that in-flight editing of labels is an undocumented feature, and different UIs may be more or less effective at refreshing any changes.

HI @rossko57,

The asynchronous behavior is good to know and explains other minor anomalies I saw as well.

Regarding what I got verses what I expected, I commented it, but I think it got lost in the code fences. At any rate,

gTodays_Weather_Forecasts.label = gTodays_Weather_Forecasts_Label.label

The above results in the label of gTodays_Weather_Forecast set to “Weather Forecasts for”, instead of “Weather Forecasts for Wednesday, May 27, 2020 (Today)”.

What am I doing wrong, or is it part and parcel of an undocumented feature?

Regards,
Burzin

This does not work because the state presentation is not actually in the Item’s label, but used in the presentation of the state in the UI. Also, you should be using setLabel.

Why not use…

… and this?

1 Like

Thanks for the explanation Scott.

I somehow missed the rule you put together. I’ll take a look at that tonight along with implmenting Jython. (Jython is on my list, but I’m trying to clean things up first.)