Duplicated callbacks from Aqara cube

Hello, everybody.
I used Appdaemon and very happy with it.
Now I running HA 0.87.1 on hass.io.
I got a issue with Aqara cube.
After callback registering with:

  def initialize(self):
    self.listen_state(self.action, "sensor.cube_action")

I got duplicated call with minimal interval:

2019-02-12 04:00:29.803222 INFO cube: callback test
2019-02-12 04:00:29.804065 INFO cube: callback test

Anyway Aqara motion sensor and Aqara door sensors callbacks works fine.

Any suggestions? Maybe someone could reproduce issue.

the listen_state picks up 2 state changes from the sensor.
that can be the state itself or 1 of the attributes from the sensor.
so you need to log new and attribute to see what changes and only listen for that.

New log:

2019-02-12 22:40:58.246418 INFO cube: new: shake, old:shake, attribute: state, entity:sensor.cube_action
2019-02-12 22:40:58.248260 INFO cube: new: shake, old:shake, attribute: state, entity:sensor.cube_action
2019-02-12 22:41:10.366004 INFO cube: new: shake, old:shake, attribute: state, entity:sensor.cube_action
2019-02-12 22:41:10.367843 INFO cube: new: shake, old:shake, attribute: state, entity:sensor.cube_action
2019-02-12 22:41:22.028746 INFO cube: new: tap, old:shake, attribute: state, entity:sensor.cube_action
2019-02-12 22:41:22.041915 INFO cube: new: tap, old:tap, attribute: state, entity:sensor.cube_action
2019-02-12 22:41:48.193834 INFO cube: new: slide, old:tap, attribute: state, entity:sensor.cube_action
2019-02-12 22:41:48.213922 INFO cube: new: slide, old:slide, attribute: state, entity:sensor.cube_action
2019-02-12 22:42:00.996714 INFO cube: new: slide, old:slide, attribute: state, entity:sensor.cube_action
2019-02-12 22:42:00.998153 INFO cube: new: slide, old:slide, attribute: state, entity:sensor.cube_action

Anyway I got duplicated entries.

zigbee2mqtt log, doesnt has duplicates.

thats what you get from home assistant.
but there is always less then a second in between, so you can tell the app only to listen if the last state change was more then 1 second ago.

does the sensor have attributes in HA?
something else then state must change to get a listen_state with old and new the same

I literally dump every variable, looks like some bug for me.

2019-02-12 23:53:34.674662 INFO cube: new: shake, old: shake, attribute: state, entity: sensor.cube_action, kwargs: {‘handle’: UUID(‘03ccad1e-16b7-4555-b09d-52c676b66c7e’)}
2019-02-12 23:53:37.601528 INFO cube: new: shake, old: shake, attribute: state, entity: sensor.cube_action, kwargs: {‘handle’: UUID(‘03ccad1e-16b7-4555-b09d-52c676b66c7e’)}
2019-02-12 23:53:37.603630 INFO cube: new: shake, old: shake, attribute: state, entity: sensor.cube_action, kwargs: {‘handle’: UUID(‘03ccad1e-16b7-4555-b09d-52c676b66c7e’)}
2019-02-12 23:53:41.832527 INFO cube: new: shake, old: shake, attribute: state, entity: sensor.cube_action, kwargs: {‘handle’: UUID(‘03ccad1e-16b7-4555-b09d-52c676b66c7e’)}
2019-02-12 23:53:41.846603 INFO cube: new: shake, old: shake, attribute: state, entity: sensor.cube_action, kwargs: {‘handle’: UUID(‘03ccad1e-16b7-4555-b09d-52c676b66c7e’)}
2019-02-12 23:54:04.424583 INFO cube: new: shake, old: shake, attribute: state, entity: sensor.cube_action, kwargs: {‘handle’: UUID(‘03ccad1e-16b7-4555-b09d-52c676b66c7e’)}
2019-02-12 23:54:04.436701 INFO cube: new: shake, old: shake, attribute: state, entity: sensor.cube_action, kwargs: {‘handle’: UUID(‘03ccad1e-16b7-4555-b09d-52c676b66c7e’)}

i didnt ask you for a dump but to look at the entity in home assistant (goto dev page, states and lookup the sensor)

i am certain its no bug in AD.
what you get come from home assistant.

I am checked attributes and there is no changes.:thinking:

what attributes are there?
they might change for a few microseconds and then change back.
which would explain it, but i cant help you trying to find that out, without knowing which attributes there are.

There is some way to change attribute except MQTT state change?
I have single MQTT event on singe action.

i am sorry i dont know how you did setup the sensor in home assistant and i have no experience with the cube.
all i can do is try to help you to come to a result you want.
and for that i need all information you can give me about the sensor.

appdaemon listens to statechanges in home assistant.
when you dont specify attributes in the listen_state it listens to all attribute state changes.
so thats the most common problem. (could be for instance that the attribute last_changed, changes even thought the state doesnt change)

i know that in case from the cube most people use listen_event, because you can shake, after shake, after shake, and that needs to be registered.

so you dont want only a statechange from shake to tap(or slide) to be registered.

If you thinks this is not bug, I will provide you all required information.
List of attributes changes depends of action. For shake are battery, voltage, linkquality and action. For rotate angle is additional atribute. And so on.

thats where the extra listen_state comes from.

attribute changes are state changes, so if there is a new attribute there is a new state change.

so you could try

self.listen_state(self.cb,sensor, attribute=some_attribute)

but then you probably also get the same kind of trouble. because there are 2 options:

  1. the attribute action probably doesnt change state when you do several times the same action
  2. any other attribute probably would give duplicate state changes also

in my opinion listen_state to a sensor is the wrong approach for the cube.

you need to use listen_event

I try
self.listen_state(self.action, "sensor.cube_action", attribute = "action")
Still have duplicated events.

like i already said