Tutorial adding HUE Motion sensor (Lux,Temp and Motion)

This is awesome, thank you!

Thought I would also share using hue sensor to alert if my garage door is still open after 9 pm using value_template and comparing garage temperature with current dark sky appearance temperature (similar to @RuudV post above). I already have the sensor there for the lights…just another bonus. Good stuff!

1 Like

Where do you find the physical sensor ID?

Open the CLIP API debugger in a browser: http://YOUR25 HUE BRIDGE CONTROLLER/debug/clip.html
and do a GET on /api//sensors.

Then look for the “ZLLPresence” and you see the ID. Like in this example the ID is 10:

“10”: {
“state”: {
“presence”: false,
“lastupdated”: “2017-03-29T20:39:34”
},
“config”: {
“on”: true,
“battery”: 100,
“reachable”: true,
“alert”: “none”,
“ledindication”: false,
“usertest”: false,
“sensitivity”: 2,
“sensitivitymax”: 2,
“pending”: []
},
“name”: “Woonkamer sensor”,
“type”: “ZLLPresence”,

Thanks for a nice guide.

I was trying to solve some other aspects of integrating hue motion sensors into HA.

First thing i did i added a panel to home assistant that gives me convenient access to my Hue Bridge API, so i don’t have to type in it’s ip address and API key every time i want to mess with it. To install it, place huebridge.html (https://pastebin.com/LRyuw8X2) under config/panels (create the panels directory if it doesn’t exist yet). Then add something like this to your configuration.yaml:
panel_custom:

- name: huebridge
  sidebar_title: Hue Bridge
  sidebar_icon: mdi:checkbox-marked-outline
  config:
     hue_address: 192.168.xxx.xxx
     hue_apikey: !secret hue_api_key

If it works, you should be able to leave the input field at the top blank and click ‘get’ to get all the bridge config, or enter something like ‘sensors/1’ and get a particular item.

The more complicated project was integrating motion sensors with the Day/Night mode in my HA. I’ve noticed many projects here have a manual Day/Night switch, meaning people here often stay late up at night :wink: I liked the idea, but i they was making it work with Hue motion sensors. They have day/night schedule, so you can make the lights come up dim when you are getting up at night to get a glass of water or something :wink: All that would take to integrate this behaviour with a day/night setting from HA was to create a new virtual day/night sensor on the bridge, update it every time your mode changes, and slightly alter a few rules on the bridge.

Here’s my virtual sensor:

{
	"state": {
		"flag": true,
		"lastupdated": "2017-04-02T12:00:01"
	},
	"config": {
		"on": true,
		"reachable": true
	},
	"name": "House Mode Day",
	"type": "CLIPGenericFlag",
	"modelid": "Home Assistant",
	"manufacturername": "Home Assistant",
	"swversion": "1.0",
	"uniqueid": "hue_house_mode_day",
	"recycle": false
}

When you POST this to the bridge, don’t forget to write down the returned sensor id. In my case it was 41.

And here’s how HA sees it:

switch:
  platform: command_line
  switches:
    hue_house_mode_day:
      command_on: '/usr/bin/curl -s -X PUT http://192.168.1.144/api/<redacted>/sensors/41/state -d {\"flag\":true}'
      command_off: '/usr/bin/curl -s -X PUT http://192.168.1.144/api/<redacted>/sensors/41/state -d {\"flag\":false}'
      command_state: "python3 -c 'import requests; print(requests.get(\"http://192.168.1.144/api/<redacted>/sensors/41\").json()[\"state\"][\"flag\"])'"
      value_template: '{{ value == "True" }}'
      friendly_name: Hue House Mode Day

There must be a better way to do this. Unfortunately i couldn’t use the RESTful binary sensor, because it doesn’t support HTTP PUT.

And here’s the automation that updates it when my input_select changes state (paste this into automation.yaml):

  - alias: Update House Mode Sensor
    hide_entity: True
    trigger:
      - platform: state
        entity_id: input_select.house_mode
    action:
      - service_template: "{% if is_state('input_select.house_mode', 'Day') %}switch.turn_on{% else %}switch.turn_off{% endif %}"
        entity_id: switch.hue_house_mode_day

The most fun part was modifying the bridge rules. Here’s a script i ended up with: https://pastebin.com/WuVD1iUk

This prints out a sequence of curl command lines. You can paste those into a shell after you’ve verified them.

Setting BACKUP=“true” produces a sequence that returns your sensors to their initial state in case you decide to undo the result. I recommend running with BACKUP=“true” first and saving the result into a file somewhere. Then run with BACKUP=“false” and execute the result.

One side effect is that the “owner” of the rules changes. I was considering setting HUE_USER to the same value that was used by the Hue App that created those rules, i don’t know how this would affect the cleanup or what happens if you try to reconfigure those sensors using Hue App again (which is not recommended - at the very least it would probably revert all the changes, maybe create new rules and leave old ones taking up space).

jq was fun to play with. I wish HA had jq support instead of almost undocumented and much less useful JSONPath. Would be fun to integrate this script into the Hue Bridge panel.

1 Like

After many months of use, you recommend it?

HI all, got a method to add the Hue dimmer switch (http://www2.meethue.com/en-us/productdetail/philips-hue-dimmer-switch) to HASS, with the idea being to use it to trigger scripts. Identical method to the motion sensors:

## Hue remote
- platform: rest
  resource: http://192.168.0.0/api/Your_Key/sensors/sensor_number
  name: Hue-dimmer-switch-1
  value_template: '{{ value_json.state.buttonevent }}'
  scan_interval: 1

Automations:

#### Hue dimmer switch
- alias: Hue dimmer 1 automations
  trigger:
    platform: state
    entity_id: sensor.huedimmerswitch1
    to: '4002'
  action:
    service: notify.ios_robins_iphone
    data_template:
      title: "Hue notification"
      message: Button 4 pressed

The only prob with this approach is that if the button is pressed a second time, this will not be registered. Need to add logic to detect if there is another press by interrogating the last updated time:

1 Like

I have 2 dimmer. Can you please tell me the steps to add them in HASS?

Hi @anon35356645 the top post by @PuckStar describes the steps, then modify the rest sensor as described in my post. Since you have 2 dimmers you will need to determine the correct sensor_number.
I might summarise the process as a Hackster post in a bit

It’s indeed possible to trigger other events based on the dimmer buttons however keep in mind that when you for instance click the off button to turn off the lights, and the next day the lights turned on again via an automation (so not using the On button), the status will not change. It was and stays 4002.
So when you then press again the Off button, HASS will not notice a change and won’t trigger anything!

I didn’t find a way to work around that yet.

I think the solution is to have a sensor for the ‘lastupdated’, then if that changes query the button state.

I do something similar for detecting changes of my external IP (Detect if IP changes?), however currently have hard coded the current IP. Is there a nice way to keep local variables in HASS? I suppose I could create a template sensor and write the current value to it, then over-write when new value is detected.

UPDATE: use Event state_changed

OK so the remote is mostly working… :slight_smile: Sometimes values come in as 4002, other times as 4000, so will need to just parse the first int in the string

- platform: rest
  resource: http://192.168.0.2/api/key/sensors/2
  name: dimmer1_state
  value_template: '{{ value_json.state.buttonevent }}'
  scan_interval: 1

- platform: rest
  resource: http://192.168.0.2/api/key/sensors/2
  name: dimmer1_updated
  value_template: '{{ value_json.state.lastupdated }}'
  scan_interval: 1

#### Hue dimmer switch
- alias: Hue dimmer 1 automations
  trigger:
    platform: state
    entity_id: sensor.dimmer1_updated
  action:
    service: notify.ios_robins_iphone
    data_template:
      title: "Hue notification"
      message: >
        {% if is_state("sensor.dimmer1_state", "1002") %}
        'Button 1 pressed'
        {% elif is_state("sensor.dimmer1_state", "2002") %}
        'Button 2 pressed'
        {% elif is_state("sensor.dimmer1_state", "3002") %}
        'Button 3 pressed'
        {% elif is_state("sensor.dimmer1_state", "4002") %}
        'Button 4 pressed'
        {% else %}
        none
        {% endif %}
1 Like

wow great find with the “lastupdated”! That solves the issue I had! :slight_smile:

I got a user, can get the list of devices … but not sure how to get the api-key???

instead of /api/sensor which command for the dimmer?

OK so its not an API key, its a ‘randomly generated username’ which you generate by following https://developers.meethue.com/documentation/getting-started

I just load the full page given by the URL in the rest sensor and browse until I find the name of the dimmer switch

Trying to have automation when Pressed On and an automation when pressed Off.
But it seems like it only triggers 1 of the 2 automations that should both trigger on the last status being updated.
This is what I have:

- alias: 'Hue Dimmer On'
  trigger:
    platform: state
    entity_id: sensor.hue_dimmer1_updated
  condition:
    condition: state
    entity_id: sensor.hue_dimmer1_state
    state: "1002"
  action:
    - service: notify.TelegramBot
      data:
        message: 'Dimmer Aan gezet'

- alias: 'Hue Dimmer Off'
  trigger:
    platform: state
    entity_id: sensor.hue_dimmer1_updated
  condition: 
    condition: and
    conditions: 
    - condition: state
      entity_id: sensor.hue_dimmer1_state
      state: "4002"
    - condition: time
      after: '17:00:00'
    - condition: time
      before: '05:00:00'
  action:
    - service: media_player.turn_off
      data:
        entity_id:
        - media_player.sony_bravia_tv

Dimmer On works, but Dimmer Off doesn’t.

Any ideas?
Can I have 2 automations that are checked for the same changed of status?

Try this:

#### Hue dimmer switch
- alias: Hue dimmer 1 automations
  trigger:
    platform: state
    entity_id: sensor.dimmer1_updated
  action:
    service: notify.ios_robins_iphone
    data_template:
      title: "Hue notification"
      message: >
        {% if states.sensor.dimmer1_state.state[0] == "1" %}
        'Button 1 pressed'
        {% elif states.sensor.dimmer1_state.state[0] == "2" %}
        'Button 2 pressed'
        {% elif states.sensor.dimmer1_state.state[0] == "3" %}
        'Button 3 pressed'
        {% elif states.sensor.dimmer1_state.state[0] == "4" %}
        'Button 4 pressed'
        {% else %}
        none
        {% endif %}

I notice that in my automation it is sometimes picking up the last sensor.hue_dimmer1_state state, and I have to press the button twice to get the true value of sensor.hue_dimmer1_state Have you noticed this?

Btw I made a post on Hackster covering the remote work, to be updated https://www.hackster.io/robin-cole/hijack-a-hue-remote-to-control-anything-with-home-assistant-5239a4