Roter
(Roter Raecher)
February 18, 2022, 5:15pm
1
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
Tinkerer
(aka DubhAd on GitHub)
February 18, 2022, 5:16pm
2
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 .
Roter
(Roter Raecher)
February 18, 2022, 5:19pm
3
Thank you for your fast answer! I’m a beginner
how do i change that?
Tinkerer
(aka DubhAd on GitHub)
February 18, 2022, 5:20pm
4
With customizing .
homeassistant:
customize:
binary_sensor.whale:
device_class: window
1 Like
Roter
(Roter Raecher)
February 18, 2022, 5:25pm
5
# 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.
farmio
(Matthias Alphart)
February 18, 2022, 7:18pm
6
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.
Tinkerer
(aka DubhAd on GitHub)
February 18, 2022, 7:51pm
7
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
tom_l
February 18, 2022, 11:07pm
8
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
Roter
(Roter Raecher)
February 19, 2022, 2:05pm
9
Thank’s at all!! That helped me. Now it works like I want!! THANKS
330chauf
(330chauf)
September 26, 2023, 7:33am
11
good morning,
Does this formatting still work or did this change with the new formatting?
greets
tom_l
September 26, 2023, 7:46am
12
As per 123’s link above, it is available in the new format.
330chauf
(330chauf)
September 26, 2023, 8:10am
13
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?
tom_l
September 26, 2023, 8:28am
14
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
330chauf
(330chauf)
September 26, 2023, 8:57am
15
Yep that did the Trick. Thanks a lot. Can I buy you a coffee?
You help so many people so many times
andrex
(Andrea)
January 11, 2024, 6:51pm
16
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?
123
(Taras)
January 11, 2024, 7:18pm
17
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.
tom_l
January 11, 2024, 9:26pm
18
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.
andrex
(Andrea)
January 12, 2024, 9:03am
19
Hemmm…could you point me to the new format for the binary sensors?
tom_l
January 12, 2024, 9:52am
20
Look at the post directly above mine. Taras linked to both.