tap_ha

Use Hue Tap with Home Assistant

If your using Home Assistant you’ve probably heard about Hue. It’s the smart lighting product from Philips, which in my opinion set the standard for everyone else. Nowadays they have a whole range of products, ranging from bulbs to complete lamps to hang under ceiling. Integrating lights into Home-Assistant isn’t rocket science, but what about their remote controls? Read further to find out!


Important to know!

The way of implementing a Hue Tap via the way below results in a delay of 250-1250ms. This in my opinion is mandatory, since when I tried to get rid of the delay I found sometimes the keypress on the Tap wasn’t detected by HA. Which is more annoying the a small delay. I kinda work around this delay by switching my Hue bulbs via the Hue bridge and not via HA. This way, the delay only appears for my Sonoff-switched devices.


What do you need

In this tutorial I assume you have the following:
– Running installation of Home Assistant (Blogpost)
– Set up the time sensor with at least display_option ‘time_utc’
– Philips Hue Bridge


Find Hue bridge username

It goes without saying that to be able to do this, your Hue bridge should be connected to Home Assistant. As soon as you connect the bridge to HA, you’ll see a file created in the /config directory of HA, phue.conf. In this file you can find the username for HA to authenticate to the bridge.

{"192.168.0.187": {"username": "uWi-HmQEct8sMnQiB2mQKjMFoHeQtYVjTk-gLp4p"}}

Find Hue Tap on bridge

Next you have to find out which sensor number your Hue Tap has in the bridge. Open your webbrowser and with knowledge of the HA username, go to this url

http://192.168.0.187/api/uWi-HmQEct8sMnQiB2mQKjMFoHeQtYVjTk-gLp4p/sensors

One sensor will have the name corresponding to whichever name you gave you Hue tap within the Hue app when you got it out of the box. In the example below, this is sensor 3

3:
  state:
	buttonevent:	16
	lastupdated:	"2018-07-10T20:28:20"
  swupdate:
	state:			"notupdatable"
	lastinstall:	null
  config:
	on:				true
  name:				"Hue tap"
  type:				"ZGPSwitch"
  modelid:			"ZGPSWITCH"
  manufacturername:	"Philips"
  productname:		"Hue tap switch"
  uniqueid:			"00:00:00:00:00:43:97:48-f2"
  capabilities:
	certified:		true

Add Tap to HA

Now that you know which sensor we want to know the status from, you can create the sensors in Home Assistant. In your YAML-file for the sensors, add the following.

- platform: rest
  resource: http://192.168.0.187/api/uWi-HmQEct8sMnQiB2mQKjMFoHeQtYVjTk-gLp4p/sensors/3
  value_template: '{{ value_json.state.buttonevent }}'
  name: 'Hue Tap Button'
  scan_interval: 1
  verify_ssl: false
  
- platform: rest
  resource: http://192.168.0.187/api/uWi-HmQEct8sMnQiB2mQKjMFoHeQtYVjTk-gLp4p/sensors/3
  value_template: '{{ value_json.state.lastupdated }}'
  name: 'Hue Tap Updated'
  scan_interval: 1
  verify_ssl: false

Also, create an input_select. This is used to be able to read which button was pressed, and reset the value in HA. If you don’t after every restart of HA it will think the last button was pressed again, resulting in lights going on or off.

hue_tap:
  name: Hue Tap Button
  options:
    - '-'
    - 16
    - 17
    - 18
    - 34
  initial: '-'

Finally, fun stuff

Now, to make this work we need at least two automations. One for reading the status of the sensors and setting the corresponding buttonpress to the input_select. The other automation is for the actual action which you want for that specific button. First, reading the sensors and setting the input_select.

- alias: Hue Tap buttonevent Sync
  id: "152374987"
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: sensor.hue_tap_updated
  condition:
    - condition: template
      value_template: >
         {%- set tmstmp = states.sensor.hue_tap_updated.state.split('T')[1] %}
         {% set hours = tmstmp.split(':')[0] %}
         {% set minutes = tmstmp.split(':')[1] %}
         {% set time = hours ~ ':' ~ minutes %}
         {% if (states.sensor.time_utc.state | string) == (time | string)  %} True
         {% else %} False
         {% endif %}
  action:
    - delay:
        # supports milliseconds, seconds, minutes, hours
        milliseconds: 250
    - service: input_select.select_option
      entity_id: input_select.hue_tap
      data_template:
        option: >
          {% if is_state('sensor.hue_tap_button', '16') %}
            16
          {% elif is_state('sensor.hue_tap_button', '17') %}
            17
          {% elif is_state('sensor.hue_tap_button', '18') %}
            18
          {% elif is_state('sensor.hue_tap_button', '34') %}
            34
          {% endif %}

What this does:
– It reads for changes in the lastupdated sensor, which has the UTC time for the last buttonpress on the Tap
– The condition compares the buttonpress with the actual utc_time (for which you need the time sensor!)
– Delay by 250ms. If not, sometimes the hue_tap_sensor isn’t updated yet. Might be able to set this to 150ms maybe, haven’t tested it
– Read the tap_button sensor and set the corresponding number to the input_select

I found the condition to be necessary after having multiple morning with the lights being on when getting downstairs. After troubleshooting this was caused by the automation above. Somehow, the Hue bridge updated the lastupdated value to other values without buttonpresses. With the condition being in place, false switching of lights hasn’t appeared since.

Automation two:

- alias: Hue Tap Button 1
  id: "156196787"
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: input_select.hue_tap
      to: '34'
      from: '-'
  action:
    - service: light.turn_on
      entity_id: light.erker
    - service: light.turn_on
      entity_id: light.keukenspots
    - service: input_select.select_option
      data:
        entity_id: input_select.hue_tap
        option: "-"

As you can see, the Tap buttonpress has different values than the actual buttons on the device itself. You’ll find these easy enough by just pressing them and refreshing the website of the bridge.

This automation gets triggered by the changing value of the input_select, which is a result of the first automation. After execution it resets the input_select back to ‘-‘. If this wouldn’t be done, pressing the same button again (for whatever reason) on the Tap would result in nothing being executed, since that value would already be active in the input_select.

About: lolgast


Leave a Reply

Your email address will not be published.