Binary_Sensor change state from on/off to open/closed

Hello, I am a beginner and have a small question about a binary sensor. I would like to output opened/closed instead of on/off on the start page.

I added the following code to the configuration.yaml:

binary_sensor:
  - platform: template
    sensors:
      fenster_kuche_template:
        friendly_name: "Fenster Küche"
        unique_id: "DG2_FensterStatus"
        value_template: |
          {% if states.binary_sensor.fenster_kuche.state == 'off' %} Geschlossen
          {% elif states.binary_sensor.fenster_kuche.state == 'on' %} Offen
          {% else %} Unbekannt
          {% endif %}
        icon_template: |
          {% if states.binary_sensor.fenster_kuche.state == 'off' %} mdi:window-closed
          {% elif states.binary_sensor.fenster_kuche.state == 'on' %} mdi:window-open
          {% else %} mdi:alert-circle
          {% endif %}

But my element card do not show open/close
It always show on/off

Do I still have to activate a trigger or an automation?

Best regards
Roter

Binary sensors are always on or off, what you can do though is define the device class to be one that shows open or closed in the UI.

Thank you for your fast answer! I’m a beginner :pensive:
how do i change that?

With customizing.

homeassistant:
  customize:
    binary_sensor.whale:
      device_class: window
1 Like
# Den Ordner mit den KNX Geräten einbinden
homeassistant:
  packages: !include_dir_named KNXDevices

customize:
  binary_sensor.fenster_kuche_template:
    device_class: window

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

# Custom Panels
panel_custom:
  - name: Supervisor
    url_path: hassio/system # url_path needs to be unique for each panel_custom config
    sidebar_title: Supervisor
    sidebar_icon: mdi:home-assistant # https://materialdesignicons.com/
    module_url: /local/panel-redirect.js # 'local' is '/config/www/'

binary_sensor:
  - platform: template
    sensors:
      fenster_kuche_template:
        friendly_name: "Fenster Küche"
        unique_id: "DG2_FensterStatus"
        value_template: |
          {% if states.binary_sensor.fenster_kuche.state == 'off' %} Geschlossen
          {% elif states.binary_sensor.fenster_kuche.state == 'on' %} Offen
          {% else %} Unbekannt
          {% endif %}
        icon_template: |
          {% if states.binary_sensor.fenster_kuche.state == 'off' %} mdi:window-closed
          {% elif states.binary_sensor.fenster_kuche.state == 'on' %} mdi:window-open
          {% else %} mdi:alert-circle
          {% endif %}

I added it to my configuration.yaml but I get this error message:

Integration error: customize - Integration ‘customize’ not found.

customize should be a subitem of homeassistant, not on the same level.

That said, if your binary_sensor.fenster_kuche is a KNX entity you can just configure that directly. See KNX BinarySensor

If not, you can still customize it directly without the need for a template.

Because you missed the formatting, as farmio said.

homeassistant:
  packages: !include_dir_named KNXDevices

  customize:
    binary_sensor.fenster_kuche_template:
      device_class: window

is what you need. YAML is picky about these things

You don’t have to use customize for this. The device_class can be added to the template:

binary_sensor:
  - platform: template
    sensors:
      fenster_kuche_template:
        friendly_name: "Fenster Küche"
        unique_id: "DG2_FensterStatus"
        device_class: window #### <------ HERE ###
        value_template: |
          {% if states.binary_sensor.fenster_kuche.state == 'off' %} Geschlossen
          {% elif states.binary_sensor.fenster_kuche.state == 'on' %} Offen
          {% else %} Unbekannt
          {% endif %}
        icon_template: |
          {% if states.binary_sensor.fenster_kuche.state == 'off' %} mdi:window-closed
          {% elif states.binary_sensor.fenster_kuche.state == 'on' %} mdi:window-open
          {% else %} mdi:alert-circle
          {% endif %}

https://www.home-assistant.io/integrations/template/#device_class

If all you wan to do is change the original sensor ( fenster_kuche.state ) then use customize, unless it too is defined in YAML (in which case it probably also supports a device_class option).

1 Like

Thank’s at all!! That helped me. Now it works like I want!! THANKS

1 Like

good morning,

Does this formatting still work or did this change with the new formatting?

greets

As per 123’s link above, it is available in the new format.

Thanks.

So in frontend it indeed shows open or closed while the state is on or off.

However when I try to add the state to a Notification it shows on or off.

{{
    states('binary_sensor.lumi_lumi_magnet_acn001_f2bf2500_ias_zone',
     'state') }}

how do I get it to show open or closed without creating a sensor of a sensor?

The state is always on or off.

The device class just tells the dashboard how to translate it.

If you want to use something other than the state in your message you have to explicitly choose what to display. Also that template is not the correct way to use states().

{{ 'Open' if is_state('binary_sensor.lumi_lumi_magnet_acn001_f2bf2500_ias_zone','on') else 'Closed' }}
2 Likes

Yep that did the Trick. Thanks a lot. Can I buy you a coffee?
You help so many people so many times :pray:

Using icon_template in a binary sensor it trows me the error

[icon_template] is an invalid option for [binary_sensor]

Has something changed since your post?

tom_l’s example is for a Template Sensor defined using legacy format which supports icon_template. In contrast, a Template Sensor defined in modern format supports icon and not icon_template.

So if you are using modern format, you have to convert tom_l’s example from legacy to modern format.

That’s the trouble with old forum posts. Home Assistant changes fast and they go out of date.

You should use the modern format for new sensors.

Hemmm…could you point me to the new format for the binary sensors?

Look at the post directly above mine. Taras linked to both.