Search code examples
pythonhome-automation

NameError: name 'process_button16' is not defined


I am getting NameError: name 'process_button16' is not defined error when running the following code.

It also turns on the LEDs without pushing the button....that shouldn't do that...

I am trying to implement a button press action using an app in appdaemon for Home Assistant. I'm a former developer, but not python so I'm banging my head against the keyboard here.... Any help would be most appreciated.

import appdaemon.appapi as appapi
from gpiozero import Button
import RPi.GPIO as GPIO

global button    
BUTTON_PIN = 16
button = None



class ButtonSense(appapi.AppDaemon):
    GPIO.output(17,GPIO.LOW)
    GPIO.output(27,GPIO.LOW)

    def initialize(self):
        self.log("-------  Hello -------")
        button = Button(BUTTON_PIN)
        button.when_pressed = process_button16()

    def process_button16():
            GPIO.output(17,GPIO.HIGH)
            GPIO.output(27,GPIO.HIGH)
            self.log("-------  Pressed -------")

Solution

  • replace button.when_pressed = process_button16() with button.when_pressed = self.process_button16(), since you have defined process_button16() as a private function of the class ButtonSense.

    button.when_pressed = process_button16() tries to assign a global function named process_button16() to button.when_pressed and since you have not defined any such global function, it throws the error NameError: name 'process_button16' is not defined