How to rename sensor's state?

i bought a Besense door contact zwave sensor https://www.amazon.com/gp/product/B073RV9VYC/
i see only 2 states…“23” for when the contact is closed. “22” for when the contact is open.
so i want to rename 23 to closed. and 22 to open.
in my config, i have:

sensor:
  - platform: template
    sensors:
      basement_heat_door:
        value_template: '{% if is_state("sensor.kaipule_technology_co_ltd_im20_door_window_sensor_access_control_2", "State 23") %}close{% else %}open{% endif %}'
        friendly_name: 'basement heat door'

but that’s not working. any idea? or is the template im using only applies to binary_sensor?

Is the state of your sensor “23” or “State 23”?
In your template you have to check for the exact state value.

And, if this is a door sensor, maybe you want to define a template binary sensor instead where the template evaluates true or false, and set the device class to door. This does not only look prettier in the Ui, but may also simplify automations based on this sensor.

value_template: '{{ is_state("sensor.kaipule_technology_co_ltd_im20_door_window_sensor_access_control_2", "22") }}’
device_class: door

this is what i see in UI now:
image
and this is what i see in the dev-state:
sensor.kaipule_technology_co_ltd_im20_door_window_sensor_access_control_2 23
node_id: 23
value_index: 9
value_instance: 1
value_id: 72057594429849745
unit_of_measurement:
friendly_name: basement heat door

so my final code in config file should look like this:

sensor:
  - platform: template
    sensors:
      basement_heat_door:
        value_template: '{% if is_state("sensor.kaipule_technology_co_ltd_im20_door_window_sensor_access_control_2", "23") %}close{% else %}open{% endif %}'
        friendly_name: 'basement heat door'
        device_class: door

i tried the above code and that still does not work

Could you please be a bit more specific what exactly does not work? Is the configuration not accepted? Is the new template sensor not changing its value when you open the door? Is the template sensor’s state “close” or “open” when your door is closed?
Please also note that a template sensor does not support device class door. Only a template binary_sensor does.

the door sensor also have another binary sensor entity with either “off” or “on” so i tried that with this code:

binary_sensor:
  - platform: template
    sensors:
      basement_heat_door2:
        value_template: '{% if is_state("binary_sensor.kaipule_technology_co_ltd_im20_door_window_sensor_sensor", "off") %}close{% else %}open{% endif %}'
        friendly_name: 'basement heat door2'
        device_class: door

so this is now what i see in “unused entities”


the top and bottom entities are actually the same binary_sensor.kaipule_technology_co_ltd_im20_door_window_sensor_sensor. BUT when I open the door, only the bottom turned to “On,” while the top stayed “Close”

The reason for this is that you have defined a template binary_sensor with a value_template that outputs “close” or “open”, but it must output “True” or “False”. So your value_template is always interpreted as “False” which with device_class: door results in the state “Closed”.

So, if the device already creates a binary_sensor, what are you trying to achieve with the additional template sensor?

Is what you are looking for just having a nice name, having a nice icon and having it say “open/closed” instead of “on/off”? In that case you just need to customise your existing sensor:

homeassistant:
  customize:
    binary_sensor.kaipule_technology_co_ltd_im20_door_window_sensor_sensor:
      friendly_name: 'Basement Door'
      device_class: door

you are right! i used your code with customize and it looks much better and works too
image

thank you!

1 Like