Window with 3 states

Hi guys, I am new to Home Assistant and have a problem. I have the following code for my window.
There are three states.
Open, closed and tilted.
Here is my code:

 - sensor:
      - name: "Status Office Tassi"
        state: >
          {% if is_state('binary_sensor.fenster_buro_tassi_offen', 'on') and is_state('binary_sensor.fenster_buro_tassi_gekippt', 'on') %}
          open
          {% elif is_state('binary_sensor.window_buro_tassi_open', 'off') and is_state('binary_sensor.window_buro_tassi_tilted', 'off') %}  
          closed
          {% else %}
          tilted
          {% endif %}
        icon_template: >
          {% set open = states('binary_sensor.fenster_buerster_tassi_open') %}
          {% set tilted = states('binary_sensor.window_buero_tassi_tilted') %}
          {% if open == 'off' and tilted == 'off' %}
            mdi:window-closed-variant          
          {% elif open == 'on' and closed == 'on' %}
            mdi:window-open-variant
          {% else %}            
            mdi:angle-acute
          {% endif %}

This also works in the template editor, but not in the dashboard! Here it only knows the status open or closed . . . .
Where is my mistake here - or is it not possible to do what I want to do?
Unfortunately, I haven’t found anything suitable in the forum.

Many thanks for your help.

Are you certain that the dashboard is looking at that sensor, not one of your binary sensors?

In the state you have four binary sensors.
In the icon_template you have two.

1 Like

Good spot. Mixture of languages and spellings:

  • binary_sensor.fenster_buro_tassi_offen
  • binary_sensor.fenster_buerster_tassi_open
  • binary_sensor.window_buro_tassi_open
  • binary_sensor.fenster_buro_tassi_gekippt
  • binary_sensor.window_buro_tassi_tilted
  • binary_sensor.window_buero_tassi_tilted

How many sensors are there in reality?

there are two sensors with the status on/off
one is at the bottom of the window and one at the top on the side
bottom off top off = window closed
bottom on top on = window open
bottom off top on = window tilted

on dashboard:
the individual states are

  • binary_sensor.fenster_buro_tassi_gekippt = open
  • binary_sensor.fenster_buro_tassi_offen = closed

and the state of

  • binary_sensor.fenster_buro_tassi_status = open

Which is “bottom” and which is “top”?

What is this? It’s not the sensor from above, which will be sensor.status_office_tassi unless you’ve renamed it, and it’s certainly not a binary sensor.

Your template references six different sensors.

original code

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

          {% set offen = states('binary_sensor.fenster_buero_tassi_offen') %}
          {% set gekippt = states('binary_sensor.fenster_buero_tassi_gekippt') %}
          {% if offen == 'off' and gekippt == 'off' %}
            mdi:window-closed-variant          
          {% elif offen == 'on' and gekippt == 'on' %}
            mdi:window-open-variant
          {% else %}            
            mdi:angle-acute
          {% endif %}

probably badly translated when copying (German to English)

the helper is: fenster_buero_tassi_status

If all that is the original code, to what? the state or the icon?
because this will mix state and icon

Wait, this is a helper? Have you created a binary sensor helper and pasted all of that into the state template box?!

You need to create a template sensor helper (not a binary one) and paste just this into the state template box:

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

(showing “gekippt” for me because I don’t have your sensors, so they’re both unknown).

If you need the icon to change also, then you will have to learn how to edit your configuration.yaml. Let us know if you want to go down that path…

@Troon
THX it works . . . very well

1 Like

Is it possible for me to change the icons? Depending on the state.

Yes, but not with a helper. You will need to edit your configuration.yaml file.

You will need to re-create your helper in that file, or in a file that is included in it. You need to add this, which may look familiar :slight_smile:

template:
  - sensor:
      - name: "Status Office Tassi"
        state: >
          {% if is_state('binary_sensor.fenster_buro_tassi_offen', 'on') and is_state('binary_sensor.fenster_buro_tassi_gekippt', 'on') %}
                offen
          {% elif is_state('binary_sensor.fenster_buro_tassi_offen', 'off') and is_state('binary_sensor.fenster_buro_tassi_gekippt', 'off') %}  
                geschlossen
          {% else %}
                gekippt
          {% endif %}
        icon: >
          {% if is_state('binary_sensor.fenster_buro_tassi_offen', 'on') and is_state('binary_sensor.fenster_buro_tassi_gekippt', 'on') %}
                mdi:window-open-variant
          {% elif is_state('binary_sensor.fenster_buro_tassi_offen', 'off') and is_state('binary_sensor.fenster_buro_tassi_gekippt', 'off') %}  
                mdi:window-closed-variant   
          {% else %}
                mdi:angle-acute
          {% endif %}

Make sure you only have one top-level template: heading. If you have a line like:

template: !include templates.yaml

then the code above needs to go into templates.yaml without the first line.

that’s how i had it at the beginning - but it didn’t work - it didn’t know the status tilted and closed - but only open.
Ok, then I’ll leave it with the template sensor.
thanks again

Did you really put it in your configuration.yaml file? That is not the same as pasting it into a helper box.

If you did put it in the file, please paste the entire file here with the sensor included.

config

Here you can see the state of the 2 sensors, they are closed, but the binary_sensor shows on for open - all time

Please post code as formatted text not screenshots.

Your templates.yaml must not start with template: as that is already in the !include line. Put this in templates.yaml (same as before but without the first line, and un-indented):

- sensor:
    - name: "Status Office Tassi"
      state: >
        {% if is_state('binary_sensor.fenster_buro_tassi_offen', 'on') and is_state('binary_sensor.fenster_buro_tassi_gekippt', 'on') %}
              offen
        {% elif is_state('binary_sensor.fenster_buro_tassi_offen', 'off') and is_state('binary_sensor.fenster_buro_tassi_gekippt', 'off') %}  
              geschlossen
        {% else %}
              gekippt
        {% endif %}
      icon: >
        {% if is_state('binary_sensor.fenster_buro_tassi_offen', 'on') and is_state('binary_sensor.fenster_buro_tassi_gekippt', 'on') %}
              mdi:window-open-variant
        {% elif is_state('binary_sensor.fenster_buro_tassi_offen', 'off') and is_state('binary_sensor.fenster_buro_tassi_gekippt', 'off') %}  
              mdi:window-closed-variant   
        {% else %}
              mdi:angle-acute
        {% endif %}

Restart HA (or reload template entities) and that will give you sensor.status_office_tassi.

I don’t know what your red-highlighted binary sensor (binary_sensor.fenster_buero_tassi_status) is or where that has come from. I do not recognise its structure with the entity_id list — unless it is a very old legacy format template binary sensor, which should be visible later on in your config after the screenshot.

Binary sensors are only on or off. You cannot make a binary sensor show three states, which is why we are trying to set up a normal sensor.

that works well. Thank you
but I cannot assign this to the Office card. Or is there
a code snippet for this?

sorry, but i am an absolute beginner and come from the edomi system