Getting GPIO state

Hya,

in the process of leaning ESPHome, i’m trying to get the gpio state ( $RES_PIN) , but it’s failing.

When compiling, i get:

Compiling .pioenvs/education/src/main.cpp.o
<unicode string>: In lambda function:
<unicode string>:74:32: error: return-statement with a value, in function returning 'void' [-fpermissive]
*** [.pioenvs/education/src/main.cpp.o] Error 1
========================== [FAILED] Took 3.38 seconds ==========================

the code:

  LED_PIN: GPIO13 # D7
  BUT_PIN: GPIO12 # D6
  RES_PIN: GPIO14 # D5
    
binary_sensor:    
  - platform: gpio
    pin: $BUT_PIN
    name: "Test LED Button"
    id: toggle_led
    on_press:
      then:
        switch.toggle: led_red

  - platform: gpio
    pin: $RES_PIN
    name: "Resistance Wire"
    id: resistance_wire
    on_press:
      then:
        lambda: |-
          return (id(resistance_wire).state);      

  - platform: template 
    name: "Led Status"
    lambda: |-
      return (id(led_red).state); 

switch:
  - platform: output
    id: led_red
    name: Output Led
    output: output_led
          
output:
  - platform: gpio
    pin: $LED_PIN 
    inverted: false
    id: output_led 

Advises, please?

Hello…

You don’t need this bit. You just set it up per the docs.

If you need to access the state for automations then start with a read of this.

Then this…

yes, i’m reading but not understanding how it works. i want to get the state (on / off) of gpio14. how shall i make use (initially displaying) without “return (id(resistance_wire).state)” ?

this is what i’m not getting.

now it works.

  LED_PIN: GPIO13 # D7
  BUT_PIN: GPIO12 # D6
  RES_PIN: GPIO14 # D5

binary_sensor:    
  - platform: gpio
    pin: 
      number: $BUT_PIN
      mode: 
        input: true
    name: "Test LED Button"
    id: toggle_led
    on_press:
      then:
        switch.toggle: led_red

  - platform: gpio
    pin: 
      number: $RES_PIN
      inverted: false
      mode: 
        input: true
        pullup: true
    name: "Resistance Wire"
    id: resistance_wire

  - platform: template 
    name: "Led Status"
    lambda: |-
      return (id(led_red).state); 

  - platform: template
    name: "Resistance Status"
    lambda: |-
      return (id(resistance_wire).state);

switch:

  - platform: output
    id: led_red
    name: Output Led
    output: output_led
          
output:
  - platform: gpio
    pin: $LED_PIN 
    inverted: false
    id: output_led 
1 Like