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

Yeah I think it’s because the Updated change is seen a bit earlier than the actual state change and thus the automation condition fails within that millisecond.

I changed my automation to this though didn’t test it yet:

- 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

- alias: 'Hue Dimmer Off 2'
  trigger:
    platform: state
    entity_id: sensor.hue_dimmer1_state
    to: "4002"
  action:
    - service: media_player.turn_off
      data:
        entity_id:
        - media_player.sony_bravia_tv

BTW regarding your hackster submission, it’s not a wifi remote ;). Hue is zigbee.

Thanks for the tip!
Surely hue_dimmer1_state and hue_dimmer1_updated are updated in the same second, so I can’t see why the old value of hue_dimmer1_state would be picked up by HASS?

Why do we need a virtual sensor?
Only thing I did was to add this to my HA-conf:

  • platform: rest
    resource: http://[Hue bridge IP]/api/[API-KEY]/sensors/[Sensor ID]
    value_template: ‘{{ value_json.state.presence }}’
    scan_interval: 2
    name: ‘…’

Did the same thing to temperatur but I’m fetching that every 60 second. Does anybody experience any problem bu polling the motion every 2s?

This is what @ThinkPad wrote about it: “It could be indeed, that the presence is already exposed. But the benefit of having a virtual sensor is that you don’t have to create the logic on the HASS side and just have to poll one sensor (the virtual one).”

I still don’t see the major benefit. If I want to create the rules in hass, then it’s great that the logic is in hass?
And by polling the real sensor, I’m also just polling one sensor?

Am I missing something?

I prefer too not use the virtual sensor. That way I can create whatever kind of logic I want in HASS.

Regarding polling problems, I ran into a problem when trying to poll 3 or more Hue Motion Sensors. The hub wouldn’t respond to all the requests which resulted in HASS not creating all sensors properly on startup (worked sometimes, most of the times not).

The solution would be to only use one REST call but since HASS doesnt support creating multiple sensors from one REST call I created another solution.

Im running a shell command every 30 seconds that retrieves all the sensors and puts it in a .txt file:

shell_command:
  hue_populate_values: 'python3 /config/hue_get_data.py'

hue_get_data.py:

#!/usr/bin/python3
import json
from requests import get
response = get('http://IP_ADDRESS/api/API_KEY/sensors')
with open('/config/hue_data.txt', 'w') as outfile:
    json.dump(response.json(), outfile)

I then poll the .txt file for information by running a command line sensor:

sensor:
  - platform: command_line
    command: python3 -c "import json; f = open('/config/hue_data.txt', 'r'); d = json.load(f); print (d['ID_OF_SENSOR']['state']['temperature'])"
    name: 'hue_motion_sensor_1_temperature'
    value_template: '{{ value | float / 100 }}'
    unit_of_measurement: °C
    scan_interval: 60

I only do this for the temperature and light sensors though, the motion is still added as a REST sensor. However I tried polling the whole sensor api with a 5 second interval and it didn’t seem to care so my guess is that it would work. I have the old Hue Hub though and don’t know if it’s slower that the newer one.

It bugged me that the motion sensor was a little bit slow even when setting the “interval_scan: 1”. I tried out an idea that turned out to work pretty well.

  • In a hue-app create a generic scene for the motion sensor or the dimmer switch.

  • Using automations/sensors/lux I then modify this generic scene according to the hue api, in HA.
    For example if it’s night and I want to trigger a night scene, I’ll make an automation that just modifies the generic scene.

  • I let the motion sensor trigger the scene the regular way, the generic scene is pulled but is modifed by HA.

This makes the motion sensor really fast and I dont have to use the REST-component as much. (I had 3 motion sensors set to 1s interval_scan, was still not happy with the reaction time…).

Maybe this was obious to some users, but it made the experience a lot better for me. I get the flexibility and freedom of HA, but the speed of using the sensor/dimmer the regular way.

I’m interested in what you’re saying but don’t quite follow.
With how you set it up can you still manage different kind of automations in HASS?
Like:
during the day and below a certain amount of Lux: turn on the light
beginning of the evening and below a certain amount of Lux: turn on the light
later in the evening and below a certain amount of Lux: turn on the light but with less brightness.
during all times when light turned on automatically, go to a dim status after 1 minute for 6 seconds, but turn back to normal brightness when motion is again detected within those 6 seconds.

Currently I have this setup in HASS but am wondering if with you’re setup that’s still possible.

I second that it sounds interesting and that I would be really curious in understanding on exactly how you’ve set this up…

What you describe seems to be based on state of the lightlevel-sensor, hence you have to do it in HA to have it flexible.

What I was describing where how to trigger different scenes by the motion sensor. And how to turn on light quickly by modifying the scene in HA rather than turning it on.

I’m at work, but I’ll quickly write some psuedo-code

automation:
   trigger:
     platform: state
     entity_id: sensor.lightlevel_from_hue
     state: '4000' 
     #Shoud use above and under for better use
  condition:
  ...
  action:
´   service: homeassistant.turn_on
    entity_id: switch.modify_hue_scene


switch:
   modify_hue_scene
     platform: terminal
     command_on: 'CURL http://IP-to-hue/sceneID/lightstates/light-id (Complete this according to HUE API, on how to modify a scene.)

Thus when triggering the motion sensor, the scene is already modified by HA to match your rules.
For example, I don’t have a scene in my hue-bridge called “kitchen-night”, “kitchen-day”, it’s called “kitchen” and it’s modified by HA.

Since the scene is turned on by the hue-bridge and not HA, it turns on instantly when a motion is detected.
You can complete this with a script that dims the light after 1 minute and shuts it down after e.g 5 minutes, this is done in HA.

Would you please share how you did this? The automation you pasted was triggered by light level not scene. Thanks!

What do you mean by triggered by a scene? Can you give an example?

Ok I just re-read what you posted earlier and I completely misunderstood you. But I got it now, thanks!

I added the dimmer switches based on the status and the last_updated events.
What is bugging me is that any automation is really slow. I have a latency of about 5seconds from pushing a button to turning on a light.
The automation triggers on the last_updated status and just switches on a light, no conditions or anything.
I have set the update interval to 1 second.

Any ideas how to speed things up?

Yes, do like I described a couple of posts ago. Essentially you let HA adjust a scene but HA doesn’t turn on the lights. The hue bridge turns on the lights.

If there is interest enough, I can create a new thread with better examples on how to do this. The information above should be enugh though.

Yes please

Is it possible to disable a HUE Motion sensor ? I would like to disable it when an activity is running with the Harmony hub or within a scene.

This would be possible if sensors were supported

Natively, indeed… but following the tutorial described above, motion sensors can be added to HA, is it correct ?

Then, using CLIP API Debugger, a sensor can be disabled using:

{"address":"/sensors/ID/config","method":"PUT","body":{"on":false}

The question is: how do you “translate” that into HA config ?