azogue / eventsensor

HomeAssistant custom sensor to track specific events
MIT License
88 stars 14 forks source link

possible to combine state data? #7

Closed GH2user closed 2 years ago

GH2user commented 4 years ago

Hi, I'm trying to use this with a xiaomi aqara cube using ZHA and it works just great in reporting flips and turns and stuff. Now I wonder if I can combine more arguments into the final output for the state mapping. Event received;

    "event_type": "zha_event",
    "data": {
        "device_ieee": "00:15:8d:00:04:21:53:9b",
        "unique_id": "00:15:8d:00:04:21:53:9b:2:0x0012",
        "endpoint_id": 2,
        "cluster_id": 18,
        "command": "flip",
        "args": {
            "value": 65,
            "flip_degrees": 90,
            "activated_face": 2
        }

Would like to combine the command "flip" and the argument " flip_degrees" or the "activated_face". Sofar I have not used the state mapping at all.

thanks, Nico

azogue commented 4 years ago

Hi @GH2user,

Right now the state is formed by pointing just to one value in the event data, I'm not sure how to define compositions like the one you're looking for (any suggestion?)

I suppose the quickest way to achieve that is with 3 sensors: 2x event sensors for the "command" and "flip_degrees" and a third one, a template sensor, to combine both. Or even just 1 event sensor and 1 template sensor, as all data is going to be available in the state attributes of the event sensor.

Anyway, I'm waiting for my own xiaomi aqara cube right now :), so in some weeks I'll be probably in the same point as you (I'm using the deconz integration instead of zha, but I suppose it's the same)

GH2user commented 4 years ago

Thanks for the quick reply @azogue. That's what I thought.

Now you are just copying one value into the new sensor state. How about creating a new state which could be an existing value in the data or build up from other values in the data. new sensor state = command (above example result: flip) current implementation new sensor state = command.flip_degrees.activated_face ( above example result: flip.90.2 )

That means a lot of parsing in your code but gives tremendous flexibility I think. Anyway will wait for you to get the cube and see how you want to tackle it, template sensor might be the way to go.

azogue commented 4 years ago

I could try to implement something like:

sensor:
  - platform: eventsensor
    name: Last aqara cube action
    event: zha_event
    state: "command, args.flip_degrees, args.activated_face"
    event_data:
      unique_id: "00:15:8d:00:04:21:53:9b:2:0x0012"
    state_map:
      flip_90_2: "FLIP: 90º, FACE 2"
      # ...

By using commas to introduce more than one key to generate the state, and then extract all and join with spaces or '_', so for the example above the state would be "flip_90_2", which could be also mapped to any custom state.

But I'm not sure if it is a good idea to add that extra complexity, as, anyway, any implementation done here will be always less powerful than what can be accomplished with a custom template:

sensor:
  - platform: template
    sensors:
      aqara_cube_action:
        value_template: >
          {%- if (states.sensor.last_aqara_cube_action.state != 'unknown') %}
          {% set attrs = states.sensor.last_aqara_cube_action.attributes %}
          {{ attrs.command | upper }}: {{ attrs.args.flip_degrees }}º, FACE {{ attrs.args.activated_face }}
          {% else %}
          {{ states.sensor.last_aqara_cube_action.state }}
          {% endif -%}

        entity_id:
          - sensor.last_aqara_cube_action

I'll wait for the cube to arrive so I can put my hands on it.

It was a compulsive purchase :), I don't know yet how I'm going to use it, but I'm very curious about it, is it reliable?

GH2user commented 4 years ago

That's indeed the thing I was after however might have to spend some time in learning to do templates. You see how you get along with your cube and then we'll see if it's still worthwhile to adjust the sensor.

For me the cube is ok, responding is fine. I have one on my desk approx 2m from the controller so distance is close. Use it to switch on/off (flip) or decrease/increase (rotate) the lightstrip on my desk. Shaking switches off all lights. I have it for only a few days yet, just testing.

azogue commented 4 years ago

Hi again, the cube just came in :)

I see the events now (but for the deCONZ integration, not the ZHA). They look like this:

{
    "event_type": "deconz_event",
    "data": {
        "id": "magic_cube",
        "unique_id": "00:15:8d:00:00:00:00:00",
        "event": 3000,
        "gesture": 5
    },
    "origin": "LOCAL",
    "time_fired": "2020-05-23T18:04:36.389828+00:00",
    "context": {
        "id": "...",
        "parent_id": null,
        "user_id": null
    }
}

IMO it is messed up to set the rotation value as the event field for gestures 7 and 8, it would be simpler to add an additional rotation field... but anyway, in the deconz it is easy to create a gesture sensor, I did it using the gesture as state key, with this custom mapping: 0: Wake, 1: Shake, 2: Drop, 3: Flip90, 4: Flip180, 5: Push, 6: DoubleTap, 7: RotCW, 8: RotCCW, and because the rest of the event data is in the state attrs, the information about the face orientation or the rotation value is also present there.

To do the state composition you were asking about I still think the best approach is to use a template sensor, as the alternative could only evolve to something similar (but much less powerful than templating)...

BTW, this device is awesome! I don't know yet where to use it, but there are a lot of possibilities :)

tumblr_onuiofKHKh1u0bi6jo4_400

GH2user commented 4 years ago

great your enjoying it. I still have some problems using it in ZHA. Might switch to deconz then as I got a Conbee2 stick. Thanks for the feedback.

azogue commented 4 years ago

I still have some problems using it in ZHA. Might switch to deconz then as I got a Conbee2 stick.

Well, the deCONZ way is far from being perfect, so I'd think twice before jumping on it :)

I say this because I think I decided to use deCONZ instead of ZHA too quickly, as I didn't investigate too much the open issues for both approaches. With the deconz I suffered a total loss of the Zigbee mesh config after rebooting my HA system, and I'm not really sure what happened yet, but I had to re-configure all devices (including re-pairing them), and I promised myself the next time that happens I'll try with ZHA. Also, in 2 other HA restart events (just upgrading HA Core, without reboots or restarting the deconz addon), all devices showed as unavailable, and I had to remove the integration in HA and add it again (re-authorizing the app in the deconz interface) to recover access to devices.

The second issue is no big deal, and I accept that issues like that are going to happen, but the 1st one was totally unexpected and made me very angry at the moment :), so right now I'm thinking seriously to switch to ZHA, as I don't even remember why I discarded it :)