Phillips Hue Tap switch controlling 4 lights

Thought I’d share my steps for getting a hue tap switch working, as there were a few steps involved and I had to scout info from a few separate places to get it all working.

First up - find the IP address of your hue bridge (and maybe set it to static on your router). Then, press the button on the hue bridge before issuing this command (with the IP address of your bridge in place of the Xs below:

curl -X POST -d '{"devicetype":"my_hue_app"}' http://XXX.XXX.XXX.XXX/api --header "Content-Type:application/json"

This will spit out something like this:
[ { "success": { "username": "SeCrETsECretMuMboJUmBO" } }]

Now, point a web broswer to:
http://XXX.XXX.XXX.XXX/api/SeCrETsECretMuMboJUmBO/sensors

This will give you a bunch of details about all your hue sensors and their possible states. Do a search on that page for “tap” and find out the sensor number that is assigned to the tap device (it will be the integer in quotes followed by :{“state”:{… BEFORE the name “Hue tap switch”)

Now you have the info you need for your configuration.yaml file. Replace the Y at the end of the resource lines below with that integer:

sensor:
  - platform: rest
    resource: http://XXX.XXX.XXX.XXX/api/SeCrETsECretMuMboJUmBO/sensors/Y
    value_template: '{{ value_json.state.buttonevent }}'
    name: 'Hue Tap'
    scan_interval: 1
  - platform: rest
    resource: http://XXX.XXX.XXX.XXX/api/SeCrETsECretMuMboJUmBO/sensors/Y
    value_template: '{{ value_json.state.lastupdated }}'
    name: 'Hue Tap last updated'
    scan_interval: 1

Finally, one automation to rule them all:

- id: tap_switching
  alias: Hue tap switch controls
  trigger:
    - entity_id: sensor.hue_tap_last_updated
      platform: state
  action:
  - delay:
      milliseconds: 20
  - data_template:
      entity_id:
      - light.
        {%- if is_state('sensor.hue_tap', '34') -%}
          your_first_light
        {%- elif is_state('sensor.hue_tap', '16') -%}
          your_second_light
        {%- elif is_state('sensor.hue_tap', '17') -%}
          your_third_light
        {%- elif is_state('sensor.hue_tap', '18') -%}
          your_fourth_light
        {%- else -%}
          this_light_should_never_happen
        {% endif %}
    service: light.toggle

A few notes:

  • The 20 ms delay is in the automation because without it, on my system, the automation would be triggered by a button press detection before the button state sensor had updated, causing the automation to toggle the light corresponding to your previous button press, not the current one;
  • 34 = button 1; 16 = button 2; 17 = button 3; 18 = button 4. Why? Because weird binary thing about how the switch registers presses, maybe;
  • replace your_first_light, etc. with your light entity names, MINUS the “light.” part - because that’s templated in;
  • enjoy!
5 Likes

Awesome - works great for me. However I did have a struggle so will share to help others…

I got the ‘sensors’ information which weirdly had not got a date in lastupdated. Added to which the button actions I had set up in the Hue app ages ago had stopped working. So I went into the Hue app and deleted the accessory and then added it again.
This fixed it completely BUT it changed the sensor id number so you do need to re-acquire the sensors data to check.

Forgot to say that I didn’t use curl, I used the web page that the hue hub gives. Use your browser to go to http://xxx.xxx.xxx.xxx/debug/clip.html and use POST to get the username and GET to get the sensor data. Note also that the tap switch name will be what you set it in the HUE app and not always ‘Hue Tap switch’.

Great! Nice tutorial aswell! Very comprehensive way of implementing most of the tap’s functionality. Easiliy change ‘light’ for ‘switch’ and you are good to go for a battery-free IoT button :slight_smile:

For troubleshooting I used the HUE IOS app to check if buttonpresses are received by the HUE Bridge, also usefull to read the ‘Hue Tap’ sensor output in HA.

Make sure to check that your newly added automation is active within HA, I had to manually turn it on (HA Menu>Configuration>Automation, look for ‘Hue tap switch controls’).

Last but not least, the apostrophe at the end of the automation gave me an error, I deleted it.

Cheers. I don’t know how that apostrophe crept in. I’ve deleted it now for the sake of anyone who may want to copy and paste.

This is nicely set up thanks, but is there any way to avoid having to poll the hub twice a second? Surely hue has some sort of push feature?

Actually, it turns out you can’t poll more than once every five seconds now. And, no, there’s currently no other option as hue need to update the bridge to push messages for third party stuff like this, I believe. If you’re using hue with HA for any other automations you’ll see there’s always a latency of up to five seconds.

Been trying to follow this, but cannot work out what I am supposed to put in to get “username”
How should I be filling out this page?

Sorry if I am just being dumb, but thanks in advance for your help