Sian-Lee-SA / Home-Assistant-Switch-Manager

Switch manager is a centralised component to handle button pushes for your wireless switches. This includes anything passed through the event bus. The component relies on switch blueprints which is easily made to allow GUI configuration of your switches and their button pushes. This helps remove clutter from the automations.
Other
269 stars 88 forks source link

Need Help working on ZHA Tradfri Remote #62

Closed LennardPlay closed 1 year ago

LennardPlay commented 1 year ago

I got it working a little bit, but I'm not able to use "args" or "params" as conditions. Can someone help me with this problem?

Sadly ZHA only seperates the left and right button by these parameters:

Left Press:

event_type: zha_event
data:
  device_ieee: XXXXXXXXXXXXXXX
  unique_id: XXXXXXXXXXXXXXX
  device_id: XXXXXXXXXXXXXXX
  endpoint_id: 1
  cluster_id: 5
  command: press
  args:
    - 257
    - 13
    - 0
  params:
    param1: 257
    param2: 13
    param3: 0
origin: LOCAL
time_fired: "2023-01-07T08:58:36.536641+00:00"
context:
  id: XXXXXXXXXXXXXXX
  parent_id: null
  user_id: null

Right Press:

event_type: zha_event
data:
  device_ieee: XXXXXXXXXXXXXXX
  unique_id: XXXXXXXXXXXXXXX
  device_id: XXXXXXXXXXXXXXX
  endpoint_id: 1
  cluster_id: 5
  command: press
  args:
    - 256
    - 13
    - 0
  params:
    param1: 256
    param2: 13
    param3: 0
origin: LOCAL
time_fired: "2023-01-07T08:58:37.675986+00:00"
context:
  id: XXXXXXXXXXXXXXX
  parent_id: null
  user_id: null

Can someone help me with the right way to write these conditions? It's probably just a formatting issue.

Sian-Lee-SA commented 1 year ago

Could you try the lastest version and give template conditions a go? template conditions. Currently the key value pair conditions doesn't traverse data properties but will try to implement this in the near future

Sian-Lee-SA commented 1 year ago

I'm also just curious as to how ZHA handles switches etc. Is that really how the data comes in for all devices? there doesn't seem to be a friendly way to determine the button or action, do you literally have to interporate the args and params like you showed to determine what button or action was pushed?

Update: I've just looked at the other two blueprints for ZHA currently in the repo and they reference everything via the command property. Can you not just reference that? considering it already states command: press.

Sorry just noticed you mentioned "Sadly ZHA only seperates the left and right button by these parameters". Another suggestion would be to request the developers via their github page and ask to make the device event data a bit more fulfilled and easier to handle. Or even better contribute to their code and add it in yourself ;)

LennardPlay commented 1 year ago

Your Idea about reaching out to the ZHA folks is great, but I don't think changing this is a possibility, because it would break all of the existing automations and HA blueprints. As far as I know only this remote has this problem, and only the left and right button share the same command. The button release command is the same for all the buttons, only seperated by the args/params.

Sian-Lee-SA commented 1 year ago

I understand that but there's also the possiblity that they'll change it in a future update to be more towards the norm and how other devices data is sent... Eitherway the next update you can access those properties from the key, value conditions using dot notation like you were originally doing

LennardPlay commented 1 year ago

I've tried template condition, but hadn't had any luck. Could you please look over this code? `#left

Sian-Lee-SA commented 1 year ago

You would do data.args[0] == 257 and data.args[1] == 13 and data.args[2] == 0 as I'm not sure jinja or even python will determine two different lists being the same if they're not the same reference

Sian-Lee-SA commented 1 year ago

I also haven't done extensive testing on the templated conditions but the simple test I did worked, so if that code I sent doesn't work then might be an issue with Switch Manager

LennardPlay commented 1 year ago

Sadly didn't work for me :/ ` - d: "m 54.065279,125.9253 a 226.71716,226.71716 0 0 0 -36.948383,123.9048 226.71716,226.71716 0 0 0 37.042197,123.841 l 66.872437,-44.9577 a 145.95894,145.95894 0 0 1 -23.15251,-78.8846 145.95894,145.95894 0 0 1 23.2763,-78.8011 z" shape: path actions:

Sian-Lee-SA commented 1 year ago

WIth the latest version you can also do

# I can't be certain this one will work as I haven't tested arrays with dot notation only objects
conditions:
  - key: args.0
    value: 256
  - key: args.1
    value: 13
  - key: args.2
    value: 0

or

# This one should definitely work
conditions:
  - key: params.param1
    value: 256
  - key: params.param2
    value: 13
  - key: params.param3
    value: 0
LennardPlay commented 1 year ago

Thank you :D Second one works

Sian-Lee-SA commented 1 year ago

If possible, could you send your blueprint as a zip so I can do testing on it, will help me find the issues with templating and key arrays. And I can just emulate the event data you have above

LennardPlay commented 1 year ago

I'll do as soon as I programmed all the buttons :)

LennardPlay commented 1 year ago

This switch is super weird. The release values for the left and right button are the same, and the param1 counts how long the switch was pressed in ms. Interesting, but I don't think it's very useful.

Sian-Lee-SA commented 1 year ago

Yh to be honest I didn't like the look of the data when I saw it... If you don't need to know how long it was pressed then just omit that condition and use only param2 and param3. Is it possible the Template condition was working it's just that the arg[0] would always be a different value so it would never pass?

It's also surprising because Ikea switches seem to be fairly popular and well mantained

LennardPlay commented 1 year ago

I think that is the best I can do. The release command triggers the release of the left button, middle button and right button, so it's only semi usable. Still good enough for most automations. Feel free to add it to the official verison if you want. zha-ikea-tradfri-remote.zip

Sian-Lee-SA commented 1 year ago

Just a couple of notes with your blueprint. hold (released) for both left and right will fire with either one because the conditions match.

Also power hold can be referenced by command move_to_level_with_on_off based on the blueprint @ https://github.com/niro1987/homeassistant-config/blob/main/blueprints/automation/niro1987/zha_ikea_tradfri_5button_remote_custom.yaml

Sian-Lee-SA commented 1 year ago

Also latest version now works with templated conditions and now lists in condition keys so thanks for sending your blueprint to use as my guineapig ;)

LennardPlay commented 1 year ago

Just a couple of notes with your blueprint. hold (released) for both left and right will fire with either one because the conditions match.

Also power hold can be referenced by command move_to_level_with_on_off based on the blueprint @ https://github.com/niro1987/homeassistant-config/blob/main/blueprints/automation/niro1987/zha_ikea_tradfri_5button_remote_custom.yaml

You are right about that. As far as I can see there's no way around triggering hold(release) on the left AND right button because they use the same data. Power/Middle button hold is actually 4 seperate events.

First - The basic press command: event_type: zha_event data: device_ieee: 5c:02:72:ff:fe:ab:2e:25 unique_id: 5c:02:72:ff:fe:ab:2e:25:1:0x0006 device_id: 225bb9969c3d7d965a2061fbcbc0eeaf endpoint_id: 1 cluster_id: 6 command: toggle args: [] params: {} origin: LOCAL time_fired: "2023-01-08T14:02:55.637173+00:00" context: id: 01GP8V218N85VAY9GQYCRRZGKJ parent_id: null user_id: null

Second (about 3 seconds after pressing): `event_type: zha_event data: device_ieee: XXX unique_id: XXX device_id: XXX endpoint_id: 1 cluster_id: 5 command: release args:

Third (Shortly after the first one) `event_type: zha_event data: device_ieee: XXX unique_id: XXX device_id: XXX endpoint_id: 1 cluster_id: 8 command: move_to_level_with_on_off args:

Fourth (Shortly after the second one) `event_type: zha_event data: device_ieee: XXX unique_id: XXX device_id: XXX endpoint_id: 1 cluster_id: 5 command: press args:

Using the third or fourth one solves no issues because the release event will still be triggered. I used the second one because it provides the fastest response (around 3 seconds of holding) Importantly the release command of the middle button has "param1=0" so I've used this to differentiate between middle hold(release) and left/right hold (release). This way the left/right release doesn't trigger the middle release. To stop the left/right release from triggering when pressing the middle button would require the condition "param1 is not 0". Sadly I can't seem to get the template conditions to work, so I can't use an "is not" function.

I don't know if this is on ikea or the ZHA team, but someone has really messed up this remote 😵‍💫

Sian-Lee-SA commented 1 year ago

The Zigbee2MQTT and deCONZ blueprints for this switch/remote doesnt have hold or hold (released) for the power button but do for everything else so that part must be more Ikea's design flaw (well Zigbee2MQTT does have hold but not hold (released). I assume those integrations have made effort to preprocess these before sending out as an event or mqtt to be handled. Probably why that link I sent you for the generic blueprint doesn't have hold (released) but on hold for the buttons. I can only advise using that blueprints data as reference as it's designed for ZHA and been used by others. Not much more Switch Manager can do about these cases I'm afraid, it can only work with the data that's passed in