Help me with some Pyton syntax

I am passing in an argument to a function, but I need to reference the argument from my globals.py file - so I need to preced the argument with globals. (not self.)

The argument I am passing to my module is called: motion_detector_office

motion_detector_office references my actual motion detector sensor that is in my globals.py function:

# Global variables
motion_detector_office = "binary_sensor.motion_sensor_158d00ccccc4d3"

My code looks like this:

import appdaemon.appapi as appapi
import globals

class motion(appapi.AppDaemon):

  def initialize(self):
    self.log(self.args["binary_sensor"])  => This logs: 'motion_detector_office'
    self.xxx="globals." + self.args["binary_sensor"]
    self.log(self.xxx)  => This logs: 'globals.motion_detector_office'
    self.log(self.get_state(globals.motion_detector_office)) => This logs: 'off' (this is correct!)
    self.log(self.get_state(self.xxx)) => This logs: 'null' (I want it to log 'off' like above!)

I’m sure I’m making this too hard - any ideas for me?

Thanks!

You need to convert the contents of the string self.xxx into a variable, which is done with the eval() function.

Try

    self.log(self.get_state(eval(self.xxx)))
1 Like

Thank you very much!!! This was driving me crazy!

1 Like