EPMatt / awesome-ha-blueprints

A curated collection of automation blueprints for Home Assistant.
https://epmatt.github.io/awesome-ha-blueprints
GNU General Public License v3.0
823 stars 246 forks source link

Support zigbee2mqtt native button press counter for Phillips Hue dimmer switch #23

Open lwfitzgerald opened 3 years ago

lwfitzgerald commented 3 years ago

Blueprint name: philips_324131092621

zigbee2mqtt natively includes a button press count in the state it sends to Home Assistant for the Phillips Hue dimmer switch (324131092621). This could be used for detecting double presses.

Here is an example Home Assistant event on a double press. Note the counter field.

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.front_bedroom_switch_hue_action",
        "old_state": {
            "entity_id": "sensor.front_bedroom_switch_hue_action",
            "state": "",
            "attributes": {
                "action": "",
                "battery": 100,
                "brightness": 255,
                "counter": 1,
                "linkquality": 81,
                "update": {
                    "state": "available"
                },
                "update_available": true,
                "friendly_name": "front_bedroom_switch_hue_action",
                "icon": "mdi:gesture-double-tap"
            },
            "last_changed": "2021-03-31T19:23:27.559882+00:00",
            "last_updated": "2021-03-31T19:23:27.559882+00:00",
            "context": {
                "id": "4da0b7b4135ae48746b90d6c1854dfe5",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.front_bedroom_switch_hue_action",
            "state": "",
            "attributes": {
                "action": "on-press",
                "battery": 100,
                "brightness": 255,
                "counter": 2,
                "duration": 0,
                "linkquality": 84,
                "update": {
                    "state": "available"
                },
                "update_available": true,
                "friendly_name": "front_bedroom_switch_hue_action",
                "icon": "mdi:gesture-double-tap"
            },
            "last_changed": "2021-03-31T19:23:27.559882+00:00",
            "last_updated": "2021-03-31T19:23:57.111797+00:00",
            "context": {
                "id": "1f5150192cead565b99d846442a213ab",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-03-31T19:23:57.111797+00:00",
    "context": {
        "id": "1f5150192cead565b99d846442a213ab",
        "parent_id": null,
        "user_id": null
    }
}
EPMatt commented 3 years ago

Hi @lwfitzgerald, thank you for the suggestion. :) Definitely a great idea, I'll look into this. Does the counter get updated for triple clicks as well?

lwfitzgerald commented 3 years ago

Yep, and for four clicks.

Three click event:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.front_bedroom_switch_hue_action",
        "old_state": {
            "entity_id": "sensor.front_bedroom_switch_hue_action",
            "state": "",
            "attributes": {
                "action": "",
                "battery": 100,
                "brightness": 255,
                "counter": 1,
                "linkquality": 63,
                "update": {
                    "state": "available"
                },
                "update_available": true,
                "friendly_name": "front_bedroom_switch_hue_action",
                "icon": "mdi:gesture-double-tap"
            },
            "last_changed": "2021-03-31T20:06:11.474045+00:00",
            "last_updated": "2021-03-31T20:06:11.474045+00:00",
            "context": {
                "id": "374cbdcc734c501b7243f71c59283c48",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.front_bedroom_switch_hue_action",
            "state": "",
            "attributes": {
                "action": "on-press",
                "battery": 100,
                "brightness": 255,
                "counter": 3,
                "duration": 0,
                "linkquality": 84,
                "update": {
                    "state": "available"
                },
                "update_available": true,
                "friendly_name": "front_bedroom_switch_hue_action",
                "icon": "mdi:gesture-double-tap"
            },
            "last_changed": "2021-03-31T20:06:11.474045+00:00",
            "last_updated": "2021-03-31T20:06:21.079485+00:00",
            "context": {
                "id": "b7e254717874b0b43f62209c3692d1d2",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-03-31T20:06:21.079485+00:00",
    "context": {
        "id": "b7e254717874b0b43f62209c3692d1d2",
        "parent_id": null,
        "user_id": null
    }
}

Four click event:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.front_bedroom_switch_hue_action",
        "old_state": {
            "entity_id": "sensor.front_bedroom_switch_hue_action",
            "state": "",
            "attributes": {
                "action": "",
                "battery": 100,
                "brightness": 255,
                "counter": 1,
                "linkquality": 70,
                "update": {
                    "state": "available"
                },
                "update_available": true,
                "friendly_name": "front_bedroom_switch_hue_action",
                "icon": "mdi:gesture-double-tap"
            },
            "last_changed": "2021-03-31T20:08:50.879375+00:00",
            "last_updated": "2021-03-31T20:08:50.879375+00:00",
            "context": {
                "id": "12f0cc018444de6d66894f132610092b",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.front_bedroom_switch_hue_action",
            "state": "",
            "attributes": {
                "action": "on-press",
                "battery": 100,
                "brightness": 255,
                "counter": 4,
                "duration": 0,
                "linkquality": 73,
                "update": {
                    "state": "available"
                },
                "update_available": true,
                "friendly_name": "front_bedroom_switch_hue_action",
                "icon": "mdi:gesture-double-tap"
            },
            "last_changed": "2021-03-31T20:08:50.879375+00:00",
            "last_updated": "2021-03-31T20:09:00.466296+00:00",
            "context": {
                "id": "b1c77fd0cc644d2f4a54c64cf912c1e6",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-03-31T20:09:00.466296+00:00",
    "context": {
        "id": "b1c77fd0cc644d2f4a54c64cf912c1e6",
        "parent_id": null,
        "user_id": null
    }
}
EPMatt commented 3 years ago

Hi @lwfitzgerald,

that's incredible. Does it work for all buttons, or only for the "on" one?

lwfitzgerald commented 3 years ago

Does it work for all buttons, or only for the "on" one?

It works for all four buttons on the remote.

Brightness up 2 press event:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.front_bedroom_switch_hue_action",
        "old_state": {
            "entity_id": "sensor.front_bedroom_switch_hue_action",
            "state": "",
            "attributes": {
                "action": "",
                "battery": 100,
                "brightness": 255,
                "counter": 1,
                "linkquality": 65,
                "update": {
                    "state": "available"
                },
                "update_available": true,
                "friendly_name": "front_bedroom_switch_hue_action",
                "icon": "mdi:gesture-double-tap"
            },
            "last_changed": "2021-04-01T11:39:11.641006+00:00",
            "last_updated": "2021-04-01T11:39:11.641006+00:00",
            "context": {
                "id": "e3a1eed2c8cc1893b4120faeb9fe6576",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.front_bedroom_switch_hue_action",
            "state": "",
            "attributes": {
                "action": "up-press",
                "battery": 100,
                "brightness": 255,
                "counter": 2,
                "duration": 0,
                "linkquality": 65,
                "update": {
                    "state": "available"
                },
                "update_available": true,
                "friendly_name": "front_bedroom_switch_hue_action",
                "icon": "mdi:gesture-double-tap"
            },
            "last_changed": "2021-04-01T11:39:11.641006+00:00",
            "last_updated": "2021-04-01T11:39:15.125600+00:00",
            "context": {
                "id": "ae24db4250606da7078a7f64e79192fe",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-04-01T11:39:15.125600+00:00",
    "context": {
        "id": "ae24db4250606da7078a7f64e79192fe",
        "parent_id": null,
        "user_id": null
    }
}

Brightness down 3 press event:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.front_bedroom_switch_hue_action",
        "old_state": {
            "entity_id": "sensor.front_bedroom_switch_hue_action",
            "state": "",
            "attributes": {
                "action": "",
                "battery": 100,
                "brightness": 1,
                "counter": 1,
                "linkquality": 73,
                "update": {
                    "state": "available"
                },
                "update_available": true,
                "friendly_name": "front_bedroom_switch_hue_action",
                "icon": "mdi:gesture-double-tap"
            },
            "last_changed": "2021-04-01T11:41:06.783080+00:00",
            "last_updated": "2021-04-01T11:41:06.783080+00:00",
            "context": {
                "id": "309d5df96ff3cf4ad8300fd1c045c743",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.front_bedroom_switch_hue_action",
            "state": "",
            "attributes": {
                "action": "down-press",
                "battery": 100,
                "brightness": 1,
                "counter": 3,
                "duration": 0,
                "linkquality": 76,
                "update": {
                    "state": "available"
                },
                "update_available": true,
                "friendly_name": "front_bedroom_switch_hue_action",
                "icon": "mdi:gesture-double-tap"
            },
            "last_changed": "2021-04-01T11:41:06.783080+00:00",
            "last_updated": "2021-04-01T11:41:11.801248+00:00",
            "context": {
                "id": "01bb5b23b6f5bc56dc0470c1be659513",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-04-01T11:41:11.801248+00:00",
    "context": {
        "id": "01bb5b23b6f5bc56dc0470c1be659513",
        "parent_id": null,
        "user_id": null
    }
}

Off button 2 press event:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.front_bedroom_switch_hue_action",
        "old_state": {
            "entity_id": "sensor.front_bedroom_switch_hue_action",
            "state": "",
            "attributes": {
                "action": "",
                "battery": 100,
                "brightness": 1,
                "counter": 1,
                "linkquality": 73,
                "update": {
                    "state": "available"
                },
                "update_available": true,
                "friendly_name": "front_bedroom_switch_hue_action",
                "icon": "mdi:gesture-double-tap"
            },
            "last_changed": "2021-04-01T11:42:40.913069+00:00",
            "last_updated": "2021-04-01T11:42:40.913069+00:00",
            "context": {
                "id": "47e6203d07a8f8810643faa1945e30ea",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.front_bedroom_switch_hue_action",
            "state": "",
            "attributes": {
                "action": "off-press",
                "battery": 100,
                "brightness": 1,
                "counter": 2,
                "duration": 0,
                "linkquality": 76,
                "update": {
                    "state": "available"
                },
                "update_available": true,
                "friendly_name": "front_bedroom_switch_hue_action",
                "icon": "mdi:gesture-double-tap"
            },
            "last_changed": "2021-04-01T11:42:40.913069+00:00",
            "last_updated": "2021-04-01T11:42:44.421738+00:00",
            "context": {
                "id": "0c40ca775d2308efdbbc4abfee1e6325",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-04-01T11:42:44.421738+00:00",
    "context": {
        "id": "0c40ca775d2308efdbbc4abfee1e6325",
        "parent_id": null,
        "user_id": null
    }
}
EPMatt commented 3 years ago

Hi @lwfitzgerald,

that's really nice to know. I'll take a look at this useful feature, which will significantly simplify the handling of double press events, at least for Zigbee2MQTT. I'll let you know as soon as it gets implemented.

Thank you for the great suggestion! :)

Mar1usW3 commented 5 months ago

any update here?

janholbrouck commented 3 months ago

Also interested in this. Would be handy functionality to have.