Multiple instances of app

I’ve got some weird behaviour when trying to run multiple instances of an app. It’s probably something simple that I’ve missed, but I can’t figure it out.

It’s takes a name as an argument (‘wakee’) creates some variables to listen to an MQTT topic and control a hue light to turn on before their alarm.

lights_alarm_rise_ben:
  module: alarm_rise
  class: AlarmRise
  wakee: ben

lights_alarm_rise_emma:
  module: alarm_rise
  class: AlarmRise
  wakee: emma


class AlarmRise(hass.Hass):

  def initialize(self):

    self.mqtt_topic = "phone/alarm/{}".format(self.args["wakee"])
    # self.log("mqtt | {}".format(self.mqtt_topic))
    
    self.sensor = "sensor.mqtt_phone_alarm_{}".format(self.args["wakee"])
    # self.log("sensor | {}".format(self.sensor))
    
    self.light_name = "light.{}".format(self.args["wakee"])
    # self.log("light | {}".format(self.light_name))

    # self.sunrise_lights(self)
    
    self.handle = self.listen_state(self.sunrise_lights, attribute = "state", entity_id = self.sensor, old = "")
    self.log("LOG1 handle | {}".format(self.info_listen_state(self.handle)))

  def sunrise_lights(self, entity_id, attribute, old_state, new_state, kwargs):

    self.log("LOG2 entity | {}".format())
    self.log("LOG£ call | {} {} {} {} {}".format(self, entity_id, attribute, old_state, new_state))

However, when the listen_state is triggered (by a change in one of the MQTT topics) it triggers both instances of ‘sunrise_lights’ not just the one for the instance that has actually had a state change.

The entity ID looks fine in LOG1 i.e. the entity_id (sensor.mqtt_phone_alarm_ben) matches the instance name (lights_alarm_rise_ben:), but when the listen state is triggered both instances are passed sensor.mqtt_phone_alarm_ben.

As far as my understanding goes, LOG3 should only be triggered for lights_alarm_rise_ben when the corresponding MQTT topic is updated.

Hope that makes some kind of sense.

Output:

2018-03-01 14:03:13.613455 INFO AppDaemon: Loading Object lights_alarm_rise_ben using class AlarmRise from module alarm_rise
2018-03-01 14:03:13.615410 INFO lights_alarm_rise_ben: LOG1 handle | ('default', None, 'state', {'entity_id': 'sensor.mqtt_phone_alarm_ben'})
2018-03-01 14:03:13.615545 INFO AppDaemon: Loading Object lights_alarm_rise_emma using class AlarmRise from module alarm_rise
2018-03-01 14:03:13.616332 INFO lights_alarm_rise_emma: LOG1 handle | ('default', None, 'state', {'entity_id': 'sensor.mqtt_phone_alarm_emma'})
2018-03-01 14:03:20.633378 INFO lights_alarm_rise_ben: LOG2 entity | sensor.mqtt_phone_alarm_ben
2018-03-01 14:03:20.634318 INFO lights_alarm_rise_emma: LOG2 entity | sensor.mqtt_phone_alarm_ben
2018-03-01 14:03:20.635047 INFO lights_alarm_rise_ben: LOG3 call | <alarm_rise.AlarmRise object at 0x7f8f30c6a6d8> sensor.mqtt_phone_alarm_ben state  1/3/2018 16:50
2018-03-01 14:03:20.636091 INFO lights_alarm_rise_emma: LOG3 call | <alarm_rise.AlarmRise object at 0x7f8f30c6ac50> sensor.mqtt_phone_alarm_ben state  1/3/2018 16:50

There is a problem with your listen_state()

You need something like:

self.handle = self.listen_state(self.sunrise_lights, self.sensor, old = "")

You don’t need the attribute arg as the default is state, and you need to specify the entity as the second parameter. If you don’t, you are subscribing to every state change.

Thank you sir, much appreciated. The attribute wasn’t actually there originally it was something I tried when trying to fix the problem - I forgot to remove it when pasting the code.

I think originally I had explicitly stated the entity field as the second argument, but put entity_id instead, which I’m guessing messes it up.

Anyway, thanks for the help - all working as expected now.

1 Like