appdaemon icon indicating copy to clipboard operation
appdaemon copied to clipboard

Issue with "listen_state" on attributes

Open joydashy opened this issue 8 years ago • 7 comments

Am trying to make this work:

self.listen_state(self.set_volume, "media_player.denon_avr", attribute="source", duration=1)

Which results in:

2017-12-23 19:25:16.460054 WARNING Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 763, in process_message
    process_state_change(data)
  File "/usr/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 714, in process_state_change
    callback["kwargs"]
  File "/usr/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 639, in check_and_disapatch
    new_state=new, **kwargs
TypeError: insert_schedule() got multiple values for keyword argument 'attribute'

This seems like a bug perhaps, as there's not much I can change in my code, I'd say.

joydashy avatar Dec 23 '17 18:12 joydashy

Hi - the stack trace doesn't match what you showed me above - looks like you are trying to create a scheduled event inside the callback when it fails? Can you show me the rest of the code please.

acockburn avatar Dec 23 '17 20:12 acockburn

No update - closing for now, will happily reopen when you get me the requested info.

acockburn avatar Dec 29 '17 17:12 acockburn

I'm seeing something similar:

2018-01-13 11:25:03.015260 WARNING AppDaemon: ------------------------------------------------------------
2018-01-13 11:25:03.015491 WARNING AppDaemon: Unexpected error during state_update()
2018-01-13 11:25:03.015651 WARNING AppDaemon: ------------------------------------------------------------
2018-01-13 11:25:03.016144 WARNING AppDaemon: Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 1841, in state_update
    self.process_state_change(namespace, data)
  File "/usr/local/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 1820, in process_state_change
    callback["kwargs"]
  File "/usr/local/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 1746, in check_and_disapatch
    new_state=new, **kwargs
TypeError: insert_schedule() got multiple values for keyword argument 'attribute'

2018-01-13 11:25:03.016336 WARNING AppDaemon: ------------------------------------------------------------

This occurs when this listener triggers: self.listen_state(self.harmony_state_cb, "remote.harmony_hub", attribute = "current_activity", new = "Cast Audio", duration = 60)

A bit of context on what i'm trying to do: I have a Harmony Hub in the living room that controls the TV, Receiver, etc. There is a tablet with HADashboard and a couple of buttons to trigger activities. The Harmony Hub is also linked to Alexa (but Alexa is not linked to HA yet), so you can start activities by voice command. When this happens, my dashboard buttons are out of sync with the actual state, so I want to run a check on them when an activity changes and stays active for at least 1 minute.

The duration keyword does not seem to work; the callback is triggered immediately when the current_activity attribute of the harmony hub changes, and then throws the above error. If I remove the duration from the parameters, everything works as expected.

The harmony_state_cb currently only logs a few things so I'm fairly positive the problem is in the listener:

def harmony_state_cb(self, entity, attribute, old, new, kwargs):
    self.log(attribute)
    self.log(new)

maaso avatar Jan 13 '18 10:01 maaso

OK, I'll reopen - this is a sightly different but related issue and seems to be caused when an attribute is a dict instead of a regular value. I'll try and reproduce.

acockburn avatar Jan 13 '18 13:01 acockburn

OK, reproduced and fixed in 3.1.0

acockburn avatar Nov 12 '18 17:11 acockburn

Hi I currently run version 4.1.0 as Hassio add-on. And get the same error. I want to use attribute and duration only in listen_state:

2019-09-29 11:04:31.892739 WARNING AppDaemon: ------------------------------------------------------------
2019-09-29 11:04:31.893229 WARNING AppDaemon: Unexpected error during state_update()
2019-09-29 11:04:31.907109 WARNING AppDaemon: ------------------------------------------------------------
2019-09-29 11:04:31.909233 WARNING AppDaemon: Traceback (most recent call last):
  File "/usr/lib/python3.7/site-packages/appdaemon/appdaemon.py", line 2346, in state_update
    self.process_state_change(namespace, data)
  File "/usr/lib/python3.7/site-packages/appdaemon/appdaemon.py", line 2314, in process_state_change
    uuid_
  File "/usr/lib/python3.7/site-packages/appdaemon/appdaemon.py", line 2234, in check_and_disapatch
    new_state=new, **kwargs
TypeError: insert_schedule() got multiple values for keyword argument 'attribute'

My problem is that I have listen_state for my illumination sensor in HA, and since both state of the sensor and its attribute 'illumination' are updated within 10 milliseconds callback is triggered twice. I have tried different combinations of attribute='state', duration=1 or attribute='illuminance', duration=1, if I have both attribute and duration in the liste_state I get this error. My current test example is like this:

def initialize(self):
    self.listen_state(self.on_illu_change,
                              'sensor.motion_mi_3_illuminance',
                              attribute='illuminance')

def on_illu_change(self, illu_sensor_id, attribute, old, new, kwargs):
    self.log('ID: {}, attribute: {}, old: {}, new: {}'.format(illu_sensor_id, attribute, old, new))
  • gives no error but produces two calls within 10 milliseconds
2019-09-29 11:24:32.711605 INFO auto_lightning: ID: sensor.motion_mi_3_illuminance, attribute: illuminance, old: 198, new: 251
2019-09-29 11:24:32.737943 INFO auto_lightning: ID: sensor.motion_mi_3_illuminance, attribute: illuminance, old: 251, new: 251

since first attribute is updated in HA, and then the state of entire sensor.

If I change the code to:

def initialize(self):
    self.listen_state(self.on_illu_change,
                              'sensor.motion_mi_3_illuminance',
                              attribute='illuminance',
                              duration=1)

def on_illu_change(self, illu_sensor_id, attribute, old, new, kwargs):
    self.log('ID: {}, attribute: {}, old: {}, new: {}'.format(illu_sensor_id, attribute, old, new))

I get above error two times:

2019-09-29 11:27:37.280027 WARNING AppDaemon: ------------------------------------------------------------
2019-09-29 11:27:37.288356 WARNING AppDaemon: Unexpected error during state_update()
2019-09-29 11:27:37.289207 WARNING AppDaemon: ------------------------------------------------------------
2019-09-29 11:27:37.290926 WARNING AppDaemon: Traceback (most recent call last):
  File "/usr/lib/python3.7/site-packages/appdaemon/appdaemon.py", line 2346, in state_update
    self.process_state_change(namespace, data)
  File "/usr/lib/python3.7/site-packages/appdaemon/appdaemon.py", line 2314, in process_state_change
    uuid_
  File "/usr/lib/python3.7/site-packages/appdaemon/appdaemon.py", line 2234, in check_and_disapatch
    new_state=new, **kwargs
TypeError: insert_schedule() got multiple values for keyword argument 'attribute'

2019-09-29 11:27:37.295961 WARNING AppDaemon: ------------------------------------------------------------
2019-09-29 11:27:37.364386 WARNING AppDaemon: ------------------------------------------------------------
2019-09-29 11:27:37.366404 WARNING AppDaemon: Unexpected error during state_update()
2019-09-29 11:27:37.367681 WARNING AppDaemon: ------------------------------------------------------------
2019-09-29 11:27:37.370490 WARNING AppDaemon: Traceback (most recent call last):
  File "/usr/lib/python3.7/site-packages/appdaemon/appdaemon.py", line 2346, in state_update
    self.process_state_change(namespace, data)
  File "/usr/lib/python3.7/site-packages/appdaemon/appdaemon.py", line 2314, in process_state_change
    uuid_
  File "/usr/lib/python3.7/site-packages/appdaemon/appdaemon.py", line 2234, in check_and_disapatch
    new_state=new, **kwargs
TypeError: insert_schedule() got multiple values for keyword argument 'attribute'

2019-09-29 11:27:37.371823 WARNING AppDaemon: ------------------------------------------------------------

For obvious reasons I cannot use new and old attributes values.

Please help me to avoid this double triggering of callback.

sergiigladchuk avatar Sep 29 '19 09:09 sergiigladchuk

in appdaemon 3.0.5 (the version you are running, the version you mention is from the addon) there are some old issues that are already taken care of in the AD 4 beta version (for which there is no addon at this point.

your best solution for now is:

def initialize(self):
    self.listen_state(self.on_illu_change,
                              'sensor.motion_mi_3_illuminance',
                              attribute='illuminance')

def on_illu_change(self, illu_sensor_id, attribute, old, new, kwargs):
    if new == old:
        return
    self.log('ID: {}, attribute: {}, old: {}, new: {}'.format(illu_sensor_id, attribute, old, new))

ReneTode avatar Sep 29 '19 16:09 ReneTode