thank you all. I got it working with the trim method @CentralCommand mentioned. The string is coming from my KNX bus where it is a 14 * 8 byte string in hex. It seems that every byte needs to be set to something so the string is filled up with blanks.
here’s the template using a mapper, which makes it better readable, and easy to add conditions and translations:
{% set state = states('sensor.rollladen_status_gaste_bad_fenster')|trim %}
{% set mapper = {'Up':'Auffahrt',
'Locked':'Sperre ist aktiv',
'Alarm End':'Alarm beendet',
'Wind Alarm':'Windalarm wurde ausgelöst'} %}
{{mapper[state] if state in mapper else 'unbekannt: ' ~ state}}
not sure what you mean by ‘globally’?
do you wish to create other translations using other base sensors, and use the same framework? Don’t think there is a way out of the core HA box, other than creating several of these template sensors.
I see, and I know what you mean in that case… I have tried very long to use a ‘global’ setup for all of my motion sensors (20+) which use a friendly_name template:
not possible, until we have a variable option in the template sensors, or some other way we can create these on the fly. Even considered Yaml anchors, but that wont help in this case either. Or maybe a Python script of sorts.
Long story short: I have 1 package with all motion sensors and their friendly names
Or, you have to be prepared to use custom_ui and customize the state. But that is against my own default advise to people using custom_ui and only for the adventurous.
in which case you can do something like this, and _glob it for all sensors you throw at it:
homeassistant:
customize:
sensor.*_buiten_sensor_light_level_raw:
templates:
friendly_name: >
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
var id = entity.entity_id.split('.')[1].split('_sensor_light_level_raw')[0].replace(/_/g,' ');
var id = capitalizeFirstLetter(id);
if (state < 1) return id + ': dark';
if (state < 3000 ) return id + ': bright moonlight';
if (state < 10000) return id + ': night light';
if (state < 17000) return id + ': dimmed light';
if (state < 22000) return id + ': `cosy` living room';
if (state < 25500 ) return id + ': `normal` non-task light';
if (state < 28500) return id + ': working / reading';
if (state < 33000) return id + ': inside daylight';
if (state < 40000) return id + ': maximum to avoid glare';
if (state < 51000) return id + ': clear daylight';
return id + ': direct sunlight';
If you’d be willing to consider using these translations only in the Frontend, you might want to have a look at a template-entity-row and create a template for the sensor (above).
You could then use that in an auto-entities card, in which you list all of your covers.