A sensor that tells me : "open or closed" , lambda or value template?

hello ,
i tried 100 of codes and yaml entities in my esphome yamlcode.

i want to add a sensor that displays : open ( or closed ) , depending of my esphome-cover.

i tried many codes such as {{ states(‘cover.kippenluik_endstop_cover’) }}.
and 100 times i had compiling error during installing on my esp32.

my 101 attempt code

  - platform: template
    name: "Cover State kipluikje"
    lambda: return states('cover.endstop_cover') 

my working cover code :

# not working lamdas..  value_template : "{{states('cover.endstop_cover') }}"
#"{{ state_attr('chickencoop', 'cover.kippenluik_endstop_cover') }}"
#value_template: "{{states('cover.endstop_cover') }}"    
  - platform: endstop
    name: "Endstop Cover"
    id : chickencoop
    max_duration: 15s      
      - switch.turn_on: Luik_Openen
    open_duration: 60s
    open_endstop: opened_cover_switch

      - switch.turn_on: Luik_sluiten
    close_duration: 59s    
    close_endstop: closed_cover_switch

      - switch.turn_off: Luik_Openen
      - switch.turn_off: Luik_sluiten




Why not just use opened_cover_switch or closed_cover_switch (expose to Home assistant)?

Set a suitable device class to see open/closed not On/Off.

But if you really want to get the state from the cover rather than directly from the endstops, this should work.

  - platform: template
    name: "Cover State"
    device_class: DOOR
    lambda: |-
        return id(chickencoop).position == COVER_OPEN ;

i had something similar , but my own made code was wrong

1 Like

“Sensor” is primarily used for things that return a numeric value such as a temperature value, humidity number, a percentage, etc. If you want On/Off. Open,Closed, Home/Away, etc. These are binary sensors as the word “Binary” should have registered as a clue because, it literally means, one or the other, ( On or Off, Open or closed) while “Sensor” is more of a generic generic term. The best thing you can do is go through the documentation. The examples are meant to be a clue as well. When all the exaples for “Sensor” are working with numbers and what you need a sensor that uses words. Thats a clue that your using the wrong sensor platform.


(using dutch to differ between a name and a “action”
lets say you buy 2 door sensors at the “ACtion” one is called “deur open”, the other one is called “deur closed”, place it on the bathroom door (example) if you would install them in such a way that the door is closed or open 1 is turned off
you would have 3 “states”

  • door is (fully) closed → the one called “deur closed” will give an alarm
  • door is (fully) open → the one called “deur open” will give an alarm
  • when you open or close, both “deur” alarms will go off since neither is switched.

you can exchange the deur(switch) name for “endstop”,
Door sensors are just switches that are closed by a magnet nearby.

in your post, you only have 0,015seconds (example) pointing at "max duration time " while in real life you give 60 seconds to either open/close the door
so the motor might stop after 15seconds, in this case if the deur sensor is not triggerd it will stop anyway.

the max duration should be a bit longer than the time it takes to open the door/cover a couple of seconds, (for example a bit of strong wind increases the time to close/open the door… or a chicken is stuck in the door (both deur sensors alarm bathroom example above) … to prevent motor burnout (to much current drawn or force applied) you limit the time, same happens when motors controll a solar screen/blinds or doors you will always hoor the motor humming a bit after its fully closed an turn off “automagicly”

i hope this makes sense and will give you new insight how to fix your code

it was my first yaml code and i didnt know any better then .
but im very happy with this info/new insight


1 Like

we al start somewhere and best is to keep complex things small, break down in steps i having my own troubles with a 4x pwm to light thingy … I left it on the side for a bit .