AppDaemon Q&A

nope, they are not new :wink:
in the api they were there all along as possibility to set from the config.

They were in the 1.0 release IIRC.

cool, didn’t notice them in the documentation before, I only remember seeing the ones on the listeners. Learn something new every day. Like today I also learned that Python gives really strange errors sometimes.

Is there a way to make the self.turn_on or turn_off an argument Item?

I made a little App to work with a MySensors 4 Button scene controller (very simple) but I want to flip the status of each button to be an on or off from arguments.

[office_4_button]
module = mys_4_button
class = Mys4Button
sensor = sensor.scene_controller_1_95
entity_1 = group.office
stat1 = self.turn_on
entity_2 = switch.office_switch
import appdaemon.appapi as appapi

class Mys4Button(appapi.AppDaemon):


  def initialize(self):
    
    self.handle = None
    
    # Check some Params

    # Subscribe to sensors
    if "sensor" in self.args:
      self.listen_state(self.motion, self.args["sensor"])
    else:
      self.log("No sensor specified, doing nothing")
    
  def motion(self, entity, attribute, old, new, kwargs):
    if new == "1":
      if "entity_1" in self.args:
        self.log("Button 1 Pressed: turning {} on".format(self.args["entity_1"]))
        self.turn_on(self.args["entity_1"])

    if new == "2":
      if "entity_2" in self.args:
        self.log("Button 2 Pressed: turning {} on".format(self.args["entity_2"]))
        self.turn_off(self.args["entity_2"])

    if new == "3":
      if "entity_3" in self.args:
        self.log("Button 3 Pressed: turning {} on".format(self.args["entity_3"]))
        self.turn_on(self.args["entity_3"])

    if new == "4":
      if "entity_4" in self.args:
        self.log("Button 4 Pressed: turning {} on".format(self.args["entity_4"]))
        self.turn_on(self.args["entity_4"])

Also the scene controller/4 button does actually 8 button actions 4 short press and 4 long press but the long press are seen by HA as same sensor ID but suffixed with “_2” I will try to fix that in the arduino code so the all come over as 1 ID/Device.

I do in a few apps, and the code looks the same as yours as far as I can see. I assume the log messages are getting printed out?

@gpbenton
No what I want is to define the turn_on inside the config not the python

like stat1 = turn_on and stat2 = turn_off

Ahh, sorry I should have read more carefully.

I don’t think know of any way of defining a function to call from the config.

The only way I know is to have some branching or constraints to pick what function you want from the arguments like

def switch(self, entity):
  if self.args["stat1"]:
      self.turn_on(entity)
  else:
      self.turn_off(entity)

Or perhaps:

def switch(self, entity):
  if "stat1" in self.args:
    if self.args["stat1"] == "turn_on":
      self.turn_on(entity)
    else:
      self.turn_off(entity)

Yes, that looks better although I think

  if "stat1" in self.args:

will cause the code to do nothing silently? If its going to fail, I’d like it to say what was wrong.

I suppose an else with an error would do the same thing, but I can’t see any advantage in adding the line in the first place. Or maybe I’m missing something.

you know you can use any cheap remote, dont you?

i use this one

for a lot of things those kind of remotes are not really usefull, but i use this one to turn of our bedheating.
that functions better then a tablet on the wall or a mobile in bed.

It’s how I usually code things - if an arg isn’t present, just ignore all of the logic, makes it more versatile. That may not be what you want of course :slight_smile:

1 Like

could you pass in the service through an arg and use the self.call_service function?

in config use

stat1 = light/turn_on   for example
entity1 = group.office

and in your code use

self.call_service(stat1, entity_id = entity_1)

or something like that

1 Like

Yep, that would work.

Yes, thanks guys this was what I was looking for!

@aimc

I’m missing something obvious here what?
self.call_service(stat1, entity_id = entity_1) NameError: name 'stat1' is not defined

in config I set
stat1 = switch/turn_on
What do I need?

Try

self.call_service(self.args["stat1"], entity_id = entity_1)

but then it gives the next error :wink:

self.call_service(self.args["stat1"], entity_id = self.args["entity1"])

if entity is also set in the config (which i suspect from reading all before)

1 Like

Yep, Just did this : self.call_service(self.args["stat1"], entity_id = self.args["entity_1"])

1 Like

@gpbenton just the right Push! @ReneTode and then made it connect.

Now I have a way to deploy these in my house. Bedside remotes. Now that this works time for the next challenge.

1 Like

I think the clever bit was @turboc’s idea to use call_service.