I created test sensors for different dev_classes - here is a code-generator below.
Probably my test is wrong since I am using input_number
values which do not have device_class
specified, but they have UoMs specified.
{% set CLASSES_SENSOR = [
("aqi","AQI"),
("apparent_power","VA"),
("battery","%"),
("carbon_dioxide","CO2"),
("carbon_monoxide","CO"),
("current","A"),
("duration","h"),
("energy","kWh"),
("frequency","MHz"),
("gas","\u006d\u00b3"),
("humidity","%"),
("illuminance","lx"),
("monetary","€"),
("nitrogen_dioxide","µg/m³"),
("nitrogen_monoxide","µg/m³"),
("nitrous_oxide","µg/m³"),
("ozone","µg/m³"),
("pm1","µg/m³"),
("pm10","µg/m³"),
("pm25","µg/m³"),
("power_factor","%"),
("power","W"),
("pressure","hPa"),
("reactive_power","var"),
("signal_strength","dB"),
("sulphur_dioxide","µg/m³"),
("temperature","°C"),
("volatile_organic_compounds","µg/m³"),
("voltage","V")
] %}
{% set CLASSES_BINARY_SENSOR = [
"battery",
"battery_charging",
"cold",
"connectivity",
"door",
"garage_door",
"gas",
"heat",
"light",
"lock",
"moisture",
"motion",
"moving",
"occupancy",
"opening",
"plug",
"power",
"presence",
"problem",
"running",
"safety",
"smoke",
"sound",
"tamper",
"update",
"vibration",
"window"
] %}
{% set code_input_boolean = '
test_device_class_{0}:' %}
{% set code_input_number = '
test_device_class_{0}:
min: -1000000
max: 1000000
step: 0.01
mode: box
initial: 0
unit_of_measurement: "{1}"
' %}
{% set code_binary_sensor = '
test_device_class_{0}:
device_class: {0}
value_template: >-
{{% set INPUT_NAME = "input_boolean.test_device_class_{0}" -%}}
{{{{ states(INPUT_NAME) }}}}
' %}
{% set code_sensor = '
test_device_class_{0}:
device_class: {0}
unit_of_measurement: "{1}"
value_template: >-
{{% set INPUT_NAME = "input_number.test_device_class_{0}" -%}}
{{{{ states(INPUT_NAME) }}}}
' %}
input_boolean:
{% for class in CLASSES_BINARY_SENSOR -%}
{{ code_input_boolean.format(class) }}
{%- endfor %}
############################################################################
input_number:
{% for class,uom in CLASSES_SENSOR -%}
{{ code_input_number.format(class,
uom) }}
{%- endfor %}
############################################################################
binary_sensor:
- platform: template
sensors:
{% for class in CLASSES_BINARY_SENSOR -%}
{{ code_binary_sensor.format(class) }}
{%- endfor %}
############################################################################
sensor:
- platform: template
sensors:
{% for class,uom in CLASSES_SENSOR -%}
{{ code_sensor.format(class,
uom) }}
{%- endfor %}
Here is a card - use it in Panel mode:
type: horizontal-stack
cards:
- type: custom:auto-entities
card:
type: entities
title: input_boolean
unique: entity
show_empty: true
filter:
template: |-
{% for state in states.input_boolean -%}
{%- if state.entity_id | regex_match("input_boolean.test_device_class_", ignorecase=False) -%}
{{
{
'entity': state.entity_id,
'name': state.entity_id.split('test_device_class_')[1]
}
}},
{%- endif -%}
{%- endfor %}
sort:
method: name
- type: custom:auto-entities
card:
type: entities
title: binary_sensor
unique: entity
show_empty: true
filter:
template: |-
{% for state in states.binary_sensor -%}
{%- if state.entity_id | regex_match("binary_sensor.test_device_class_", ignorecase=False) -%}
{{
{
'entity': state.entity_id,
'name': state.entity_id.split('test_device_class_')[1]
}
}},
{%- endif -%}
{%- endfor %}
sort:
method: name
- type: custom:auto-entities
card:
type: entities
title: input_number
unique: entity
show_empty: true
filter:
template: |-
{% for state in states.input_number -%}
{%- if state.entity_id | regex_match("input_number.test_device_class_", ignorecase=False) -%}
{{
{
'entity': state.entity_id,
'name': state.entity_id.split('test_device_class_')[1]
}
}},
{%- endif -%}
{%- endfor %}
sort:
method: name
- type: custom:auto-entities
card:
type: entities
title: sensor
unique: entity
show_empty: true
filter:
template: |-
{% for state in states.sensor -%}
{%- if state.entity_id | regex_match("sensor.test_device_class_", ignorecase=False) -%}
{{
{
'entity': state.entity_id,
'name': state.entity_id.split('test_device_class_')[1]
}
}},
{%- endif -%}
{%- endfor %}
sort:
method: name
So, I do not see any changes in presentation if UoM is specified - only difference is that UoM is displayed after the value.
Exceptions:
- “duration” is displayed w/o UoM in “hh:mm:ss” format (which does not occur if UoM is not specified);
- “monetary” - no UoM is displayed for sensor:
Also, I changed UoM for some values to see what could happen.
For example, for “frequency” - the value was “xxx MHz”, I changed UoM to “Hz” and reloaded templates - the displayed value is “xxx Hz”:
As for temperature: initially the sensor has UoM =C, then I changed to F - and the value is changed (not correct), UoM not changed: