Openhab thermostat and chart value

Thanx to my precious Google who decided to make my Nest thermostat dumb after 10 years,i now use the temperature sensors i already have in every room to calculate the average temperature and using a shelly relay to on/off my natural gas boiler.Works pretty well,no fancy programing with PID just 2 setpoints (night/day) and a hysteresis of 0.5 C.

I also persist an item to see how much time my heating is on.The item is in minutes so i use a .js tranformation to make it readable and nice.So the item’s displayState is in form of 00h00m.My problem is that when i create a chart with days(bars) per month ,the item’s value is the unformated one….Is there a way to show the displayState value in the chart?

If you use a Number:Time Item you can use Date Time formattings without the transformation. For example my UPS runtime is a Number:Time Item with a state of 2250 s (I use seconds but you could set the unit metadata to minutes) and it displays as 00:37:00 using a state description pattern of %1$tH:%1$tM:%1$tS. That will work in a sitemap label too, just put the pattern in the [ ] in place of the usual %s.

When you use units, by default the unit of the Item will be included as part of the Y-axis label.

But it’s not used anywhere else. All the display state and unit stuff is implemented on the server side and for the most part is unavailable to the charting library.

my item is a plain number without unit and my trans script is this

(function(input) {
  if (!input) return "";

  const value = parseFloat(input);
  const hours = Math.floor(value);
  const minutes = Math.round((value - hours) * 60);

  // If less than 1 hour → show only minutes
  if (hours === 0) {
    return `${String(minutes).padStart(2, '0')}m`;
  }

  // Otherwise show HHh MMm
  const h = String(hours).padStart(2, '0') + "h";
  const m = String(minutes).padStart(2, '0') + "m";

  return `${h} ${m}`;
})(input)

so i get a nice readble form everywher exept chart that shows the raw value.I think i might use somehow a proxy item to display the value nice in the chart…

My main point is that if you used units you’d at least get the unit displayed on the chart, and you wouldn’t need a SCRIPT transformation to see it displayed nicely.

I’m not sure how a proxy Item is going to help here. You can’t chart a String.

I’ve tried your suggestion but still the raw value appears in the chart without unit.Everywhere else i use the item,widgets/sitemaps etc,it shows the formated value with unit…

The unit is there in the upper left. You have the unit set toh.

That is the only place it’s going to show up.

Charts are never going to show the formatted value. They are always going to show the raw numbers retrieved from persistence. All that charts can show is what unit those raw numbers from persistence are in.

1 Like

can i format the tooltip value using the “formatter” option? I asked AI and gave me this but it is not working.

  tooltip:
    - component: oh-chart-tooltip
      config:
        show: true
        formatter: >
            function (value) {
              var h = Math.floor(value / 60);
              var m = value % 60;
              var hh = ("0" + h).slice(-2);
              var mm = ("0" + m).slice(-2);
              return hh + ":" + mm;
            }

or this

tooltip:
    - component: oh-chart-tooltip
      config:
        show: true
        trigger: axis
        # Use the pipe | character to start a multi-line string block
        valueFormatter: |
          =(x) => {
            var minutes = x % 60;
            var hours = (x - minutes) / 60;
            // Format minutes with leading zero if less than 10
            var formattedMinutes = minutes < 10 ? '0' + minutes.toString() : minutes.toString();
            return hours.toString() + ':' + formattedMinutes;
          }

No, and this is an example of how the AI chatbots are really bad at giving openHAB advice. They make stuff up. The charts are not actually implemented by the openHAB project. They come from a third party library.

If you look at the reference for oh-chart-tooltip you’ll see there is no formatter nor a valueFormatter config property.

But if there were, you’d use the second way to create the function. Arrow functions are supported in some places for widgets but not all.

1 Like

yes indeed!At first i was impressed with chatgpt or gemini how easy created java script rules or tranformations that really worked (i cant write java to save my life) but after that i did realize that ai keeps making stuff up just to continue interact with me…

so after all i found a proper solution ,i use this valueFormatter at tooltip’s config

valueFormatter: "= (val) => Math.floor(val / 60) + ' h ' + (Math.floor(val % 60)< 10 ? '0' : '') + Math.floor(val % 60) + ' m '"

and i get a hh mm format of my raw minutes number.