'AND' of two binary sensors (solved)

I have two binary sensors on a NodeMCU and am trying to create an ‘and’ condition that sets a template sensor if, and only if, both binary sensors are on but just can’t get it right.

I think I need a template sensor with lambda but can’t figure out the code so would appreciate any help, please.

The ‘input’ binary sensors are:

binary_sensor:

  - platform: gpio
    pin:
      number: D6
      mode: INPUT_PULLUP
      inverted: FALSE
    id: microwave
    name: "Garage MW"
    device_class: motion

  - platform: gpio
    pin:
      number: D1
      mode: INPUT_PULLUP
      inverted: FALSE
    id: pir
    name: "Garage PIR"
    device_class: motion

I thought this was as simple as putting both in as triggers and conditions?

A template binary_sensor with

value_template: "{{ is_state('binary_sensor.garage_mw', 'on') and is_state('binary_sensor.garage_pir', 'on') }}"

should do it

I don’t see a platform gpio.

Really?

Sorry, my bad.
I was looking on the component list :blush:

Thanks, all, for your responses and @VDRainer for that line. I think I spent too long on it and was over complicating it last night! I have added it as follows:

  - platform: template
    name: "Garage Movement"
    device_class: motion
    lambda: |-
      if  ("{{ is_state('binary_sensor.garage_mw', 'on') and is_state('binary_sensor.garage_pir', 'on') }}") {
        return true;
      } else {
        return false;
      }

It now appears in HASS but does not update - it is stuck on ‘on’ and does not reflect the states of the other two sensors as seen by HASS.

Garage

I was hoping to do the ‘and-ing’ on the NodeMCU and ESPHome to minimise network traffic and HASS.IO processing but unfortunately can’t do that if it doesn’t update reliably.

In the hope that I need to update the template sensor status in HASS I’ve searched for a command to ‘push’ the template_sensor status to HASS but can’t find anything so maybe I should just use HASS automation to do the AND-ing.

Thanks for the help, all!

I tried this in esphome and it seems to works fine for me:

You can uncomment the internal: true to not have the 2 original binary sensors in HA and only show the combined one.

binary_sensor:
  - platform: gpio
    pin:
      number: D6
      mode: INPUT_PULLUP
      inverted: FALSE
    id: microwave
    #internal: true
    name: "Garage MW"
    device_class: motion

  - platform: gpio
    pin:
      number: D1
      mode: INPUT_PULLUP
      inverted: FALSE
    id: pir
    #internal: true
    name: "Garage PIR"
    device_class: motion

  - platform: template
    name: Garage Movement
    device_class: motion
    lambda: !lambda |-
      if (id(microwave).state) and (id(pir).state) {
        return true;
      } else {
        return false;
      }

Thank you, @Holdestmade, that works. I had to add another set of brackets to stop compile errors but it now updates in real time on HASS. Once I’m happy with the behaviour of the automation I’ll do as you say and make the two ‘input’ binary sensors local only.

This is what I ended up with:

  - platform: template
    name: "Garage Movement"
    device_class: motion
    lambda: !lambda |-
      if ((id(microwave).state) and (id(pir).state)) {
        return true;
      } else {
        return false;
      }

Thanks again!

Thanks a lot from me as well.
I also wanted to combine a Microwave and PIR sensor and it was as simple as copying this code :slight_smile: