Binary_Sensor change state from on/off to open/closed

# 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.

Thanks, I’ve missed your reply!

This is the binary sensor (that works):

  - name: "Valvola PT"
    unique_id: "valvola_pt"
    state_topic: "home/centrale_termica/valvole/PT"
    value_template: "{{ value_json.value }}"
#    device_class: window
    payload_on: "1"
    payload_off: "0"

And based on the documentation, i’ve created this template:

template:
 - binary_sensor:
    - name: 'Valvola pt template'
      state: >
        {{states('binary_sensor.valvola_pt')}}
      icon: >
        {% if is_state('binary_sensor.valvola_pt', 'on') %}
            mdi:valve-open
        {% else %}
            mdi:valve-closed
        {% endif %}

But how can I use this new template? The binary sensor still has the default icon.
Thanks!

You created a new Template Binary Sensor called binary_sensor.valvola_pt_template and its icon is determined by the state of binary_sensor.valvola_pt.

binary_sensor.valvola_pt is an MQTT Binary Sensor and, according to its documentation, its icon can only be set to one image.

I’ve lost you.
I’ve discovered that the icon of a binary sensor can’t change when the state changes and when I’ve asked about the icon template you pointed me to the legacy and modern format, but maybe because I’m a newbie, I don’t know how to use the modern format.
The question is: how can I have an entity that changes the icon between valve-open to valve-closed based on a binary sensor?
THANKS!!!

Explanations have been already been provided (several times) in this topic. If they’re still unclear to you then I suggest you review the documentation (links posted above) while waiting until someone else volunteers to re-phrase the existing explanations.