Template sensor with two sensors

Hi!

I’ve put two reed relays on my garage door. One for fully open, and one for the fully closed state. With a Sonoff Basic and ESPHome, a managed to see if the door is open or closed. My goal is to make a Lovelace card, what shows, is the door open or closed, OR it’s on its way, or half-opened. So, I figured out, that I need a template sensor for this. Please, can you lead me to the right way? I can do a 2 way icon with the docs i’ve find, but I cannot make the third state, when the door isn’t fully closed and isn’t fully opened. My skills isn’t enough for this without help. Thank you!

post what you have so far in your template sensor code and others will be better able to help you.

Make sure you post your code code correctly formatted.

Now I have this, but it’s only shows, when the door is fully open, or fully closed.

  - platform: template
    sensors:
      garage_door:
        friendly_name: "Garázskapu"
        value_template: >-
          {% if is_state('binary_sensor.zarva', 'on') %}
            Zárva
          {% else %}
            Nyitva
          {% endif %}
        icon_template: >-
          {% if is_state('binary_sensor.nyitva', 'on') %}
            mdi:garage-open
          {% else %}
            mdi:garage
          {% endif %}

Here is a template that should work. I’m sorry but I’m not sure how you would say “half-open”:

{% if is_state('binary_sensor.zarva', 'on') %}
  Zárva
{% elif is_state('binary_sensor.zarva', 'off') %}
  Nyitva
{% else %}
  your phrase for half-open here
{% endif %}

Nono. This is too simple. It shows, it’s opened, even if it is just pulled up 10cm. I have two sensors, one on the closed state, and one on the opened state. In template checker, this works:

          {% if (states.binary_sensor.zarva.state == "on") and (states.binary_sensor.nyitva.state == "off") %}
            Zárva
          {% elif (states.binary_sensor.zarva.state == "off") and (states.binary_sensor.nyitva.state == "on") %}
            Nyitva
          {% else %}
            Köztes
          {% endif %}

But if i do the same in configuration.yaml, like this:

  - platform: template
    sensors:
      garage_door:
        friendly_name: "Garázskapu"
        value_template: >-
          {% if (states.binary_sensor.zarva.state == "on") and (states.binary_sensor.nyitva.state == "off") %}
            Zárva
          {% elif (states.binary_sensor.zarva.state == "off") and (states.binary_sensor.nyitva.state == "on") %}
            Nyitva
          {% else %}
            Köztes
          {% endif %}

        icon_template: >-
          {% if (states.binary_sensor.zarva.state == "on") and (states.binary_sensor.nyitva.state == "off") %}
            mdi:garage
          {% elif (states.binary_sensor.zarva.state == "off") and (states.binary_sensor.nyitva.state == "on") %}
            mdi:garage-open
          {% else %}
            mdi:garage-alert
          {% endif %}

Then it always shows the thrird state (“köztes” in my language)

Just cover all the bases.

          {% set zarva = is_state('binary_sensor.zarva', 'on') %}
          {% set nyitva = is_state('binary_sensor.nyitva', 'on') %}
          {% if zarva and nyitva %} #Both on
            Köztes 1
          {% elif not zarva and not nyitva %} #Both off
            Köztes 2
          {% elif not zarva and nyitva %}
            Nyitva
          {% else %}
            Zárva
          {% endif %}

The following example uses each binary_sensor as one bit of a two-bit binary number. So 1 is open, 2 is closed, and 0 is mid-way (3 should never happen).

z = zarva (closed)
n = nyitva (open)

   zn
0) 00 Köztes (intermediate)
1) 01 Nyitva (open)
2) 10 Zárva (closed)
3) 11 Unknown

I’ve tested the following template sensor and it works as per your requirements:

  - platform: template
    sensors:
      garage_door:
        friendly_name: "Garázskapu"
        value_template: >-
          {% set map = {0:"Köztes", 1:"Nyitva", 2:"Zárva", 3:"Unknown"} %}
          {% set n = 1 if states('binary_sensor.nyitva') == 'on' else 0 %}
          {% set z = 1 if states('binary_sensor.zarva') == 'on' else 0 %}
          {% set x = (z * 2) + n %}
          {{ map[x] if x in map.keys() else 'Error' }}
        icon_template: >-
          {% set map = {0:"mdi:garage-alert", 1:"mdi:garage-open", 2:"mdi:garage", 3:"mdi:help"} %}
          {% set n = 1 if states('binary_sensor.nyitva') == 'on' else 0 %}
          {% set z = 1 if states('binary_sensor.zarva') == 'on' else 0 %}
          {% set x = (z * 2) + n %}
          {{ map[x] if x in map.keys() else 'mdi:error' }}

Sorry, I’m an idiot… sometimes restart hass isn’t enough. After a full restart of the host machine, everything’s going as it should. Thanks you all for your help!

Here’s the config:

It should have been enough. I never have to restart the host machine.

And it looks like you ended up using the template I gave you and it worked?

I’m using the template, what I copied in my last reply. I don’t know, why I had to restart the host.

If you’re not using the example I posted then I’m not sure why you marked it as ‘Solution’. I know my example works, but you should mark whichever post provided you with the solution you are actually using. So if you are using finity’s example, mark it as the solution. That’s fair to the people who helped you and to other community members looking for an answer to your original question.

1 Like

It doesn’t look like they are using mine either.

the solution was sort of based on that but they added a couple of and’s in the template.

Sorry, if marking your code as a solution is misleading, but it’s working, so it’s a solution. Finity’s one isn’t, because it isn’t checking for the status of the “fully open” sensor, so, if I open up the gate a bit, it shows, it’s open.
So, your code is working, as it should, and as I wanted to. That’s why I’m marked it as a solution. The fact, that I’m using a third kind of code, is just a thing. For one problem, we have more solutions. (And the truth is, that your code is more elegant, than mine)

Elegant is in the eye of the beholder on this one I guess.

I think yours works, is easier to read and more straight forward so in my mind that is what counts as more elegant.

I’m definitely not saying it isn’t creative and uses all kinds of cool coding tho. you can always count on @123 for solutions to problems that are otherwise very obscure. :slightly_smiling_face:

It’s your decision and I respect it.

The only reason I brought it up is because I’ve seen the exact opposite situation happen far too often. A person is given a solution, they make one inconsequential change to it, then they mark their version as the ‘Solution’. That’s misleading to other users and unfair to the person who actually spent time and effort creating the original solution.

In this situation, I just wanted to make sure my offering wasn’t being given unfair credit.

As for ‘elegant’, I agree with finity that this one is ‘in the eye of the beholder’. Using if-elif-else accomplishes the same thing. This approach uses 5 lines of code to handle all door states plus 2 error conditions. It would take a few more lines to accomplish the same thing using the other approach. So the main difference is brevity.

1 Like

Thanks for all of you guys for helping me. I’m very happy to be here, and communicate with intelligent people, who can understand other’s. And as I said before, your code’s impressing me, because using a little math, and very flexible. I can use it in the future for some more complex things. So, thank you again! You teach me something new. Have a nice day guys! :slight_smile: