Change the default state pattern in 4.2.x?

Hi everyone,
does anybody know of a way to change the newly introduced default state patterns globally?
I have around 200+ Items which I want to display there normal stored value and now the new default state pattern (e.g. 20.31 °C instead of the new 20 °C / 20.1 W instead of the new 20W).
Is there really no other way then to set the metadata of all single items to %1.f %unit%?

Kind Regards

I know of no other way. I believe the pattern that gets pushed is hard coded by the binding. It’s not controlled by openHAB core so there’s no global setting that applies everywhere.

No, it’s a part of core.

Welp… time to stay on 4.1 till then.
Thanks

OK, I thought that was just a change on the MQTT binding.

Assuming you want to set the pattern to %.1f %unit% on all Number Items a simple script can do it.

In JS Scripting:

var newPattern = '%.1f %unit%';
    
items.getItems().filter(i => i.type.startsWith('Number'))
                .forEach(i => {
  let sd = i.getMetadata().stateDescription;
  if(sd !== undefined && sd.configuration.pattern != newPattern) {// If you have some other checks add them before setting the pattern (e.g. use %d for percents check the unit metadata)
    sd.configuration.pattern = newPattern; 
    i.replaceMetadata('stateDescription', sd.value, sd.configuration);
  }
  else {
    i.replaceMetadata('stateDescription', '', {'pattern': '%.1f %unit%'});
  }
});

NOTE: the script above is semi-tested. I didn’t test the second i.replaceMetadata line and might have the configuration part wrong. Obviously backup before running.

First, unless the item was not linked to a channel, there was always a default state pattern applied (it was in the channel state description provider code). What the referenced PR did was also set a default state descirption pattern if the item was not linked to a channel.
Second, that referenced PR cleaned up another issue: the default state pattern for a number item with dimension was before set to %.0f, dropping the unit. The PR now sets it to %.0f %unit%, also keeping the unit in visualisation. Are you sure you saw any unit before?

Making that default pattern configurable would be an enhancement.

I’m not arguing against the change or anything like that. I just missremembered the details of the PR.

I prefer to always explicitely define the state description so if it’s shown on a UI, I’ve manually set the state description pattern manually already. This is mostly left over from OH 3 times where bindings pushing state description metadata tended to cause problems. I think most of those problems have since been fixed but the habit remains.

Agreed, probably in the regional settings somewhere.

In the mean time, a script like the above can explicitly set the pattern.

Here’s a version that only set the pattern in cases where there isn;'t already a pattern set, which is probably closer to what @tekook needs.

var newPattern = '%.1f %unit%';
    
items.getItems().filter(i => i.type.startsWith('Number') 
                        && (i.getMetadata().stateDescription === undefined  
                            || i.getMetadata().stateDescription.pattern === undefined))
                .forEach(i => {
                  let value = '';
                  let conf = { 'pattern':  newPattern };
                  let sd = i.getMetadata().stateDescription;
                  if(sd !== undefined) {
                    value = sd.value;
                    conf = sd.configuration;
                    conf.pattern = newPattern;
                  }
                  i.replaceMetadata('stateDescription', value, conf);
                });

This version will not replace the pattern in places where a pattern is already defined.

1 Like

Thanks @rlkoshak for the script! That make it so much easier.

@Mherwege definitly seeing units and decimal points on 4.1.3.
My setup current looks something like this:
image
As soon as I update to 4.2.x it looks like this.
image

The only definition I have, is in the thing’s channel, there is a unit (°C) defined.
The item does not have any definition or state meta assigned to it:

label: Temperatur (Ankleidezimmer)
type: Number:Temperature
category: temperature
groupNames:
  - RPI01Sensors
tags:
  - Temperature
  - Measurement

Channel definition:

- id: Temperature
    channelTypeUID: mqtt:number
    label: Temperatur
    description: null
    configuration:
      stateTopic: tele/tasmota_881D66/SENSOR
      transformationPattern: JSONPATH:$.SHT4X.Temperature
      unit: °C

The sensor sends the data in format of xx.xx (e.g. 22.32)

OK, what you see is specific to MQTT. MQTT before added a default state description pattern of %s. That indeed kept the unit. The challenge with that was that adding a state description to an item then showed the unit twice. There was a change to MQTT to provide a more relevant state description pattern as well: [MQTT] Fix state description by mherwege · Pull Request #16866 · openhab/openhab-addons · GitHub

1 Like

Does the same apply to openweathermap? The temperature provided by OWM gets display as xx.x °C and the item also does not have any as state metadata.
Or the http-binding? I used a custom made API in the first days of using openhab (2-3 years ago) and I can’t remember ever setting a state description.

  - id: Temperature
    channelTypeUID: http:number
    label: Temperatur
    description: null
    configuration:
      mode: READONLY
      unit: °C
      stateExtension: sht4x
      stateTransformation: JSONPATH:$.result[0].valueR

valueR was again in format xx.xx

The OpenWeatherMap binding sets the state description pattern for temperature channels to %0.1f %unit%, so that would have been used as the default. And that would still be the case today.
I don’t know about the http binding, but I think the stateTransformation pattern may play a role. You can easily find out if there is a default state description pattern on the number channel type for the http binding using the API Explorer: do a GET on the /channel-types/{channelTypeUID} endpoint with channelTypeUID set to http:number.