Window sensor reporting three states

Hello all!

I’ve built a Window-sensor that should report the three possible states (OPEN/TILTED/CLOSED) of a window to HA using an esp8266/NodeMCU.

So I have the following config in ESP:

  binary_sensor:
    - platform: gpio
      device_class: window
      publish_initial_state: True
      pin:
        number: GPIO12
        mode:
          input: True
          pullup: True
      name: tilted
      id: tilted
    - platform: gpio
      device_class: window
      publish_initial_state: True
      pin:
        number: GPIO13
        mode:
            input: True
            pullup: True
      name: open
      id: open

  text_sensor:
    - platform: template
      name: "Window"
      id: window_state
      update_interval: 5s
      lambda: |-
        if (id(open).state == true) {
          return {"OPEN"};
        } else {
            if (id(tilted).state == true) {
              return {"TILTED"};
            } else {
              return {"CLOSED"};
            }
        }

With the above config I get the correct state displayed in HomeAssistent… BUT: Each state-statechange is reported after 5 seconds (update_interval of the text_sensor).
Is there any possibility to get any state-change immediatly after occurring without sending the unchanged state every few seconds? Or is there any other way to perform this task?

Why don’t you try an on_state: trigger in your binary sensors with an action to publish the template sensor:

And welcome to HA forums…

1 Like

Thanks! That was the trick… :wink: Sometimes I can’t see the forrest for the trees… :wink: