Window status open/close/tilt config

Hey,

I´ve checked plenty of topics regarding display the correct state of the window in the dash: Open Close Tilt
For sure it´s quite easy but I´m not well know in program, just find some code blocks but nothing works in my case.

My setup:
1 binary sensor on each window connected to knx bus (open close tilt)

#knx.yaml
- name: "Fenster Küche offen" #entity: binary_sensor.fenster_kuche_offen
  state_address: "5/2/20" #0=open, 1=closed
  device_class: window
  invert: true 

- name: "Fenster Küche gekippt" #entity: binary_sensor.fenster_kuche_gekippt
  state_address: "5/2/21" #1=tilt, 0=closed
  device_class: window

what is shown:
Screenshot_20240723-083013~2
I´d like to get instead of name: “Fenster gekippt” state: “offen”, name: “Fenster” state: “gekippt”

In the entity-ist it displays both. “…gekippt” offen and “…offen” offen

I try to copy the following code in the knx.yaml (beneath the sensor) → failed
I try to copy it to config.yaml → failed

{% if is_state('binary_sensor.fenster_kuche_offen', 'off') %} and is_state('binary_sensor.fenster_kuche_gekippt', 'off') %}  
      offen
{% elif is_state('binary_sensor.fenster_kuche_offen', 'on') and is_state('binary_sensor.fenster_kuche_gekippt', 'off') %}  
      geschlossen
{% else %}
      gekippt
{% endif %}

How can I get this?

Thanks for your help

regards
Stefan

You don’t have one binary_sensor, you have two. Otherwise this won’t work.

I assume/guess this should have been a template sensor, and you would need to put it under that domain. :slight_smile: As you said you aren’t an experienced user, please provide more information on where you got that code, to see how it was implemented there.

However, trusting in my guess (:laughing:), you’d need to setup a template binary sensor, that could be like this:

template:
  - sensor: 
      name: Fenster Küche
      state: >
        {% if is_state('binary_sensor.fenster_kuche_offen', 'off') %} and is_state('binary_sensor.fenster_kuche_gekippt', 'off') %}  
        offen
        {% elif is_state('binary_sensor.fenster_kuche_offen', 'on') and is_state('binary_sensor.fenster_kuche_gekippt', 'off') %}  
        geschlossen
        {% else %}
        gekippt
        {% endif %}

Now you’d use this sensor to show in the mushroom card, and not one or both of the original sensors.

Does that make sense to you? If not, please ask! :slight_smile:

Hey Patrick,
Thanks for helping.

Sure, If have two binary sensors, that´s clear so far :slight_smile: (it´s just one housing with two reeds)

I´ve create an helpster template entity, as you suggested:

create a new mushroom card:

mushroom

but
it just shows “geschlossen”, no matter wether the window is open/closed/tilted

we´re very close to the goal, I assume :slight_smile:

regards
Stefan

You are using a template binary sensor. This can only have 2 states. Use a template sensor instead.

If you are using the Helpers-UI, you only need to write the template itself, not the yaml-boilerplate → remove lines 1-4

There’s nothing to add to that, you beat me to the answer @farmio. :laughing:

As Matthias said, choose to setup a template sensor, not a template binary sensor. Then paste in this code:

{% if is_state('binary_sensor.fenster_kuche_offen', 'off') and is_state('binary_sensor.fenster_kuche_gekippt', 'off') %}  
offen
{% elif is_state('binary_sensor.fenster_kuche_offen', 'on') and is_state('binary_sensor.fenster_kuche_gekippt', 'off') %}  
geschlossen
{% else %}
gekippt
{% endif %}

Yes, well done. Works fine. Thanks a lot for your help and patience.

Just two thinks I´ve changed:

  • in the codes above there is one %} too much
  • and I deleted the “invert: ture” at the window sensors in the knx ymal. (I include that during my inital setup)

Final code for everyone:

{% if is_state('binary_sensor.fenster_kuche_offen', 'off') and is_state('binary_sensor.fenster_kuche_gekippt', 'off') %}  
offen
{% elif is_state('binary_sensor.fenster_kuche_offen', 'on') and is_state('binary_sensor.fenster_kuche_gekippt', 'off') %}  
geschlossen
{% else %}
gekippt
{% endif %}

Next step I´ll include an appropiate icon.

I must admit, I haven’t done such a thing via the UI, I just add that to my YAML files. In YAML you can easily set an icon, it is practically the same code, but instead of “offen”, you’d set an icon there, like “mdi:window-variant-open”.

{% if is_state('binary_sensor.fenster_kuche_offen', 'off') and is_state('binary_sensor.fenster_kuche_gekippt', 'off') %}  
mdi:window-variant-open
{% elif is_state('binary_sensor.fenster_kuche_offen', 'on') and is_state('binary_sensor.fenster_kuche_gekippt', 'off') %}  
mdi:window-open
{% else %}
mdi:whatever_fits_I_couldnt_find_a_fitting_icon
{% endif %}

:slight_smile:

Oh, I tought I just copy that in the sensor template and get some nice icons… but that doesn´t work.

Can you explain in detail what do you mean with “you just add that to the yaml files”?
(I know yaml files and where I find them… :slight_smile: )
sorry for asking these basics :confused:

Give me a few minutes, I’ll try to set this up in the UI. If not, I’ll give you a short explanation, why and how I handle my files in HA… :laughing:

Take your time. :slight_smile:
Honestly, I´m a bit ashamed. But I’m even happier that you’re helping me.

OK, I tried, I think it is simply not possible with the UI. At least I couldn’t find a way to template the icon… :open_mouth:

So here we go with the YAML files. :slight_smile: This will be a lot of new information, if you haven’t heard about that, so please take your time, and ask, if something is difficult! :slight_smile:

I have my files sorted and split up with packages. Packages are an underestimated feature of HA, that is not used by many people. But they’re so great! :slight_smile:

There are different ways to setup your files in HA, one is, to configure everything in configuration.yaml. I’m taking sensor as a very minimalistic example.

# configuration.yaml
sensor:
  here would be the configuration for one or more sensors

Another way would be to move all sensor config into a seperate file

# configuration.yaml
sensor: !include sensor.yaml

Now you’d have your sensor configurations in one file. Makes it way better readable!
But it has the disadvantage, that you have to configure every sensor in that file. In the end, you have a lot of different files, like sensor.yaml or binary_sensor.yaml, but they are sorted by their domain (domain is sensor, binary_sensor, light…) and not by logic.

And that’s where packages come in handy. There you can sort everything by logic. You put every domain you need in one neat file.

# my_first_package.yaml
sensor: 
  config here
binary_sensor:
  config here
input_boolean:
  config here

Makes it really easy to keep an overview of what you’re doing. :slight_smile:

That’s why I use packages. If you want to read more about this, see here

Let me know, what you think, and what your questions are. If you decide how you want to proceed, I can provide the correct code for that template sensor.

As I said, these are a lot of new things for a beginner, but just tell us, how you think you want to go further, than we can tell you the best way to do so! :slight_smile:

Hi Patrick,

once more, thanks a lot for your support an explanations. I´ve never been treated like this in other communities. Most of them refer me to the help-docs (which hard to unserstand, with more or less no knowledge of program)

What I understand:
In principle, all confirguration can be done in the configuration.yaml, but, that´s not very clean and bit confusing.

Unsing seperate files are better. That´s what I´ve already done with the KNX-addresses. (!include knx.yaml) and a create knx.yaml.

First question: You worte, " that you have to configure every sensor in that file". Means, when I create a sensor.yaml all sensor needs to be configure in that file, so the ones from the knx.yaml, too? I´ve several sensor in the knx,yaml (lux, wind, current,…).
e.g. from the knx.yaml

sensor:
  - name: "Flur OG Helligkeit"
    state_address: "5/1/4" #KNX group address
    type: illuminance
    state_class: measurement
    device_class: illuminanace

Need to delete them from the kny.yaml to put in the sensor.yaml when I create that?
Or is the knx.yaml just to create the HA-entitys from the KNX group addresses? If yes, this would mean that an additional sensor.yaml is just for configurations sensors independet from the knx.yaml. Hope you understand what I want so say.
knx.yaml → just for connection knx to HA (entity ↔ knx addresses)
sensor.yaml → to configure sensors which are mentioned in the knx.yaml (and stay there)

Packages:
Sort by logic. Logic can be, e.g. sort by rooms or anything else

#living-room_package.yaml
sensor:
  config

light:
  config

climate:
  config

From my point of view right now, I´ve not that many configurations to use packages but I´m in the beginning of my HA era :slight_smile: And I like clean and clear structures.
So, I prefer the way with the packages.
(And that´s what I´ve also done in my KNX config. I sort my group addresses by logic)

You’re welcome! :slight_smile: And thanks for the cudos! :smiley: HA has a steep learning curve at times, but we’re all (well, most users) here to help and enjoy our “hobby” Home Assistant! It’s like an addiction, you can’t escape after a while! :laughing: You’ll see, you’re already hooked up. :joy:

I’ll take a little detour, but that should explain how things work in HA. :slight_smile: In HA, all things are listed in so called domains, eg. light, sensor, binary_sensor and such. Everything starts with its domain. To know about this domain based system is what we need later on.

The configuration of HA is “normally” written in one file, configuration.yaml. If you split up this configuration, it will be later on combined into one large (internal) file, and will be worked with. Right were you set the include (compared to added at the end) in your configuration.yaml, the file will be included and used like it would be all one file from the beginning.

This approach means, you can only include a domain once. So sensor: can only be listed once. If you use it eg. in knx.yaml, you can’t use it anywhere else, neither in the “original” configuration.yaml, nor in any other file you split up.
That’s why people use sensor.yaml and set all their sensors in this file. Same goes for other domains.
And yes, that would mean, you’d have to delete the sensor configuration in knx.yaml and add it to sensor.yaml. As you already noticed, this seems kind of unintuitive, and that’s where packages come to the rescue.

Packages are a different system, and internally it will be handled differently, so you can use whatever domain you like in any package file you configure. That’s the beauty of it, and as you already noticed, it makes things way more logical.

You can setup your package files like you want to. Eg. with rooms, but also with other things. I personally like to set my packages up with their “meaning”. So I have a waste package, a router package, a flora package. I do have some room packages, but they are not a typical room package, in my case they’re for special things, like the kitchen. Eg. you have a fan, that “goes with you”. Mine is sitting on my desk right now, but if it’s to hot, I use it in the bedroom for the night. Where would you put that configuration? In “office_package” or in “bedroom_package”? Or much better, in a “fan_package.yaml”? You get the idea. :wink:

This gives you the flexibility to decide where you would remember it best - for me, this is the only logical approach on sorting things like that.

Long story short, if you use packages, you’ll have much more fun in the future! :slight_smile: And if you haven’t done a lot of configuration yet, it’s even more usable. You don’t have to move from a different system to packages…

So, start setting up packages in configuration.yaml, so you can use them. I use this approach:

homeassistant:
  packages: !include_dir_named packages

and then, in my folder /config/packages I just have all the different files for packages. Like this:

├── config
│   ├── packages
│   │   ├── flora_package.yaml
│   │   ├── waste_package.yaml
│   ├── configuration.yaml

Don’t take that the wrong way, but just start with packages, and if problems come up, just ask! :slight_smile:

But if I may suggest another good read, that will help you further down the road:

You might find some of the above advice in there to, eg. post #13:joy: :joy:

Adn one last thing, that people tend to forget: if you’re at the beginning, think of a naming convention for your devices. There’s not much, that is more tedious than renaming things later on…

I hope this gets you started, and as already said numerous times: Ask, if I’m not clear enough or if you have any other questions! :slight_smile:

Just to avoid misunderstandings:

This is why root level platform keys are “the old way” of yaml config. The current way is to have platforms under an integration key (template, knx, etc.). So each integration can have its own platform definitions in separate files.
This is perfectly fine

template:
  sensor:
    ...
knx:
  sensor:
    ...

and is also what happens when a root

knx: !include knx.yaml

is used. So whatever you do in your Knx.yaml file has no influence on the rest of your yaml configuration (except if you used a second root-level knx:, then it would overwrite the whole former one).

The “old” deprecated way, which is mostly obsolete nowadays looked like this:

sensor:
  - platform: some_integration
    ...

If you see this in a tutorial or so, better check the docs if this is still valid for that integration.

Packages are fine, but have a downside of unprecise error messages when validation errors happen. AFAIK the line numbers won’t be correct then.

1 Like

Thanks guys.
Honestly, I´m a bit confused about these infos in both posts :slight_smile:

I understand Patrick like this:
when I use the domain sensor in the knx.yaml e.g.

sensor:
  - name: "Flur OG Helligkeit"
    state_address: "5/1/4" #KNX group address
    type: illuminance
    state_class: measurement
    device_class: illuminanace

a can´t use the domain sensor somewhere else to configure an window sensor design e.g. in a seperate template.yaml

template:
  - sensor: 
      name: Fenster Küche
      state: >
        {% if is_state('binary_sensor.fenster_kuche_offen', 'off') and is_state('binary_sensor.fenster_kuche_gekippt', 'off') %}  
        offen
        {% elif is_state('binary_sensor.fenster_kuche_offen', 'on') and is_state('binary_sensor.fenster_kuche_gekippt', 'off') %}  
        geschlossen
        {% else %}
        gekippt
        {% endif %}

      icon: >
        {% if is_state('binary_sensor.fenster_kuche_offen', 'off') and is_state('binary_sensor.fenster_kuche_gekippt', 'off') %}  
        mdi:window-variant-open
        {% elif is_state('binary_sensor.fenster_kuche_offen', 'on') and is_state('binary_sensor.fenster_kuche_gekippt', 'off') %}  
        mdi:window-open
        {% else %}
        mdi:whatever_fits_I_couldnt_find_a_fitting_icon #find appropiate icon
        {% endif %}

But acc. to Matthias post. I can use sensor in both files… :woozy_face:

Let´s make it the patical way with the case of my window sensor:

knx.yaml:

binary_sensor:  
  - name: "Fenster EG Küche Offen"
    state_address: "5/2/20"
    device_class: window

  - name: "Fenster EG Küche Gekippt"
    state_address: "5/2/21"
    device_class: window

sensor: #just to show I´ve additional sensor in the knx.yaml.
  - name: "Küche Helligkeit"
    state_address: "5/2/14"
    type: illuminance
    state_class: measurement
    device_class: illuminance

to display the diffrenent state of the window I create template.yaml file (included in confi.yaml)

template:
  - sensor: 
      name: "Fenster Küche"
      state: >
        {% if is_state('binary_sensor.fenster_kuche_offen', 'off') and is_state('binary_sensor.fenster_kuche_gekippt', 'off') %}  
        offen
        {% elif is_state('binary_sensor.fenster_kuche_offen', 'on') and is_state('binary_sensor.fenster_kuche_gekippt', 'off') %}  
        geschlossen
        {% else %}
        gekippt
        {% endif %}

      icon: >
        {% if is_state('binary_sensor.fenster_kuche_offen', 'off') and is_state('binary_sensor.fenster_kuche_gekippt', 'off') %}  
        mdi:window-variant-open
        {% elif is_state('binary_sensor.fenster_kuche_offen', 'on') and is_state('binary_sensor.fenster_kuche_gekippt', 'off') %}  
        mdi:window-open
        {% else %}
        mdi:whatever_fits_I_couldnt_find_a_fitting_icon #find appropiate icon
        {% endif %}

Does it work (For sure I can try it, but I would like to understand why and how things working…)

And yes, this is the domain-way :slightly_smiling_face:
Next steps to understand a bit mor of the package way, especialle creating the files and struktur. But I would first study packages-help. After that I assume I come back with a lot of questions, don´t worry :slight_smile:

Tbh, for beginners, I wouldn’t recommend to use packages or even !include. This way you can see what’s what based on indentation at a glance.
Given you use a decent editor (eg. vs code - maybe as Addon) you can collapse (hide) contents of keys to better browse the single configuration.yaml file you have.

This would look like:

knx:
  # everything with this indentation is purely *knx*, and no concern of any other integration (root key). 
  # in Knx this is a key, so there can only be a single sensor key
  sensor:
    ...

something:
  # perfectly fine to have a "sensor" key in the "something" integration too
  sensor:
    ...

template:
  # everything with this indentation is purely *template*, and no concern of any other integration (root key). 
  # in template, these are lists (since "- " is used), so there can be multiple "sensor" list items
  - sensor:
    ...
  - sensor:
    ...
  ...

What !include does is just like copy and paste, but the copied files contents start at a different indentation level. So in your Knx.yaml you have a root sensor: key, but this will be a level below knx: when included.

Before starting to use packages, I’d strongly recommend to read its documentation. Everything works slightly different there then.

Ok understood. The intendation is significant. What !include do is just “cut” out the code out of the config.yaml to an seperat file to make the config.yaml a bit cleaner.

I use the studio code server it can also hide contents and show the !inculde content as well. great overview for me. (Just find out that funcion recently)

Now, I´ll setup all window states (as the inital post) in a template.

Is there in any lilst of all entities-id compared to the name in the yaml? In the UI I found them settings → entities sorted by Name and entity-id. Theres no way to print or copy to excel for e.g. That would be much more easier to handle all the entity-id

No. The name value in Knx yaml is only used for initial setup of the entity. Changing or removing it afterwards wouldn’t even make a difference.
The entity_id is set to a slugified version of that (“My Name” → “light.my_name”). So with vs code entity_id code completion you should be able to set them up pretty quickly.

ok thanks.
I´m on the right way.

that my code set in the template.yaml

sensor:

# Fenster Zustände darstellen
  - name: "Fenster Eltern"
    state: >
      {% if is_state('binary_sensor.fenster_eltern_offen', 'off') and is_state('binary_sensor.fenster_eltern_gekippt', 'off') %}  
      offen
      {% elif is_state('binary_sensor.fenster_eltern_offen', 'on') and is_state('binary_sensor.fenster_eltern_gekippt', 'off') %}  
      geschlossen
      {% else %}
      gekippt
      {% endif %}

    icon: >
      {% if is_state('binary_sensor.fenster_eltern_offen', 'off') and is_state('binary_sensor.fenster_eltern_gekippt', 'off') %}  
      mdi:window-open-variant
      color: red
      {% elif is_state('binary_sensor.fenster_eltern_offen', 'on') and is_state('binary_sensor.fenster_eltern_gekippt', 'off') %}  
      mdi:window-closed-variant
      color: green
      {% else %}
      mdi:window-open-variant
      color: red
      {% endif %}

  - name: "Fenster Büro"
    state: >
      {% if is_state('binary_sensor.fenster_buro_offen', 'off') and is_state('binary_sensor.fenster_buro_gekippt', 'off') %}  
      offen
      {% elif is_state('binary_sensor.fenster_buro_offen', 'on') and is_state('binary_sensor.fenster_buro_gekippt', 'off') %}  
      geschlossen
      {% else %}
      gekippt
      {% endif %}

    icon: >
      {% if is_state('binary_sensor.fenster_buro_offen', 'off') and is_state('binary_sensor.fenster_buro_gekippt', 'off') %}  
      mdi:window-open-variant
      {% elif is_state('binary_sensor.fenster_buro_offen', 'on') and is_state('binary_sensor.fenster_buro_gekippt', 'off') %}  
      mdi:window-closed-variant
      {% else %}
      mdi:window-open-variant
      {% endif %}

result is:
the state works fine - open/close/gekippt
and the icon is shown as well

I would like to color the icon.
red=open / orange=gekippt / green=closed

and in would like to have on the dash-overview an entity liste which shows the open/gekippt window.

fenster

(I´ve alread do that list just with the binary_sensor and works fine so far, but with the template sensor it deosn´t work properly.)