ADC to binary swtich

i’m trying to hook up some reed contact that are build in my home.
They came with resistors in it, so i hooked them up to a esp32 and read there analoge value.

now i need to turn on / off a swtich at certain value.

in the log’s it seems to work, but the switch isn’t changing its state in lovelace.
what can i do to see the changes in lovelace.

Next i wan’t to automate my heating, if a door or window is opend longer than x minutes, the heating should be turned off.

here is the config from esphome:

esphome:
  name: alarmpanel
  platform: ESP32
  board: nodemcu-32s

wifi:
  ssid: "##########"
  password: "#######"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Alarmpanel Fallback Hotspot"
    password: "#########"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "#########"

ota:
  password: "#########"

switch:
  - platform: template
    name: "Raam achter"
    id: raamachter

  - platform: template
    name: "Raam voor"
    id: raamvoor

  - platform: template
    name: "Achter deur"
    id: achterdeur

  - platform: template
    name: "Voor deur"
    id: voordeur


sensor:
  - platform: adc
    pin: GPIO36  
    name: "Raam voor"     #ingang 1
    attenuation: 11db
    update_interval: 5s
    on_value_range:
      - above: 2.1
        then:
          - switch.turn_on: raamvoor
      - below: 2.0
        then:
          - switch.turn_off: raamvoor
  
  - platform: adc
    pin: GPIO39  
    name: "Raam achter"    #ingang 2 
    update_interval: 5s
    attenuation: 11db
    on_value_range:
      - above: 2.1
        then:
          - switch.turn_on: raamachter
      - below: 2.0
        then:
          - switch.turn_off: raamachter
  
  - platform: adc
    pin: GPIO34  
    name: "Achterdeur"     #ingang 3
    update_interval: 5s
    attenuation: 11db
    on_value_range:
      - above: 2.1
        then:
          - switch.turn_on: achterdeur
      - below: 2.0
        then:
          - switch.turn_off: achterdeur
  
  - platform: adc
    pin: GPIO35 
    name: "Voordeur"      #ingang 4
    update_interval: 5s
    attenuation: 11db
    on_value_range:
      - above: 2.1
        then:
          - switch.turn_on: voordeur
      - below: 2.0
        then:
          - switch.turn_off: voordeur

do you really need to use ADC? What is the reed switch voltage swing from open to closed? You’d be better off using binary sensor instead of ADC. And they can be directly exposed to HA (lovelace) without having to create intermediate template sensors.

Your switch templates are not properly defined, they don’t have a pin number, hence they won’t show up in HA (lovelace).

If you must use ADC, then remove the on_value_range stuff and give each ADC sensor an ID.

Then convert your switch (platform templates) into binary sensors (platform templates). And make the template (lambda) true if ADC sensor > 2.1, and false if < 2.0. e.g.

binary_sensor:
  - platform: template
    name: "Raam voor"
    lambda: |-
      if (id(raam_voor).state > 2.1)
        return true;
      else if (id(raam_voor).state) < 2.0
        return false;

sensor:
  - platform: adc
    internal: true
    pin: GPIO36  
    id: raam_voor     #ingang 1
    attenuation: 11db
    update_interval: 5s

this is what i looked for, thanks.

1 Like