Hue motion sensors + remotes: custom component

nice! thanks you for sharing this, Ill add that immediately!

please help me out, I’d like to add some extra flexibility, by using an input_select for the id, (i have 11 sensors)… and have the service use a variable for id, that can either be taken from that input_select, or maybe an automation sending a few motion sensor id’s to that script

would something like this work:

in secrets file:

hue_command: http://HUE_BRIDGE_IP/api/APIKEY/{{ type }}/{{ id }}/{{ command }}

in a package:

rest_command:
  hue_command:
    url: !secret hue_command
    method: put
    payload: '{{ data }}'

automation:

  - id: set_motion_sensitivity
    trigger:
      [some_trigger]
    action:
      service: rest_command.hue_command
      data:
        command: config
        data: >
          { "sensitivity": "{{states('input_select.set_hue_sensitity')}}" }
        id: >
          {{states('input_select.select_hue_motion_sensor')}}
        type: sensors

input_select:
  set_hue_sensitivity:
  name: set Hue sentitiviy
  options:
    - 0 #low
    - 1 #medium
    - 2 #high

input_select:
  select_hue_motion_sensor:
  name: Select motion sensor
  options:
    - 5
    - 7
    - 12
    - 22
    - 28
    - 33
    - 44
    - 48
    - 52
    - 56
    - 60

another question:

Im trying to read the times for the day and night, as you might have seen. Now you are using a put command, and since my curl command_line sensor doesn’t work without implicating the sensors behavior, i was thinking maybe using a get command could get those schedule times too?

needing to extract value in {“address”:“/config/localtime”,“operator”:“in”,“value”:“T07:30:00/T{{now().strftime(”%H")}}:00:00"}
see: Hue motion sensors + remotes: custom component - #687 by Mariusthvdb

for a (each) motion sensor?

is that what you do in HA:

if so, that is exactly what I am trying to accomplish, next to be able to read it and display it in HA…!

buried in the rules I can find this for all motion sensors:

MotionSensor 5.day-on is {“address”:“/config/localtime”,“operator”:“in”,“value”:“T06:45:00/T22:45:00”}
MotionSensor 5.day-dark-on is {“address”:“/config/localtime”,“operator”:“in”,“value”:“T06:45:00/T22:45:00”}
MotionSensor 5.night-on is {“address”:“/config/localtime”,“operator”:“in”,“value”:“T22:45:00/T06:45:00”}
MotionSensor 5.night-dark-on is {“address”:“/config/localtime”,“operator”:“in”,“value”:“T22:45:00/T06:45:00”},

… so. a lot of the same info… what would be wise to select in order to be able to set the schedule from HA

Kind of… The Hue API does not allow dedicated commands, it’s just GET (=read), PUT (=modify), POST (=create new) or DELETE (=remove). You would also need to read/modify the rules, not the sensors. Each motion sensor has (at least) 4 rules you need to modify if you want to change the times. I can strongly recommend the All4Hue Android app which is a big help for identifying rule IDs etc.

To extract the configured times into a template sensor in HA, I would pick one of those 4 rules of the sensor, use a rest sensor on /api/>API Key>/rules/<ruleID> with something like value_template: "{{ value_json.conditions[1].value }}" and go from there. The template sensor(s) probably need(s) some fancy Jinja logic to extract the exact hours/minutes…

EDIT: I see you already found the 4 rules I mentioned… :slight_smile: It was actually a real PITA to write all that code, especially since the API does not allow to change single values, you always have to PUT the entire {conditions} element…

If I relied 100% on HA (which I don’t), I would probably model the entire logic in HA without messing with the Hue rules, but I want my lights to be fully functional even when HA is offline, that’s why I chose this way.

suppose one would be enough indeed , since they all contain the same info… Im good when using jinja, but first make the sensor up to

http://192.168.1.212/api/api/rules/20 and extracting the value out of {“address”:“/config/localtime”,“operator”:“in”,“value”:“T22:00:00/T07:00:00”} ?

full set for rule 20 is:

{"name":"MotionSensor 48.night-dark-on","owner":"redacted owner","created":"2019-01-20T12:58:29","lasttriggered":"none","timestriggered":0,"status":"enabled","recycle":true,"conditions":[{"address":"/config/localtime","operator":"in","value":"T22:00:00/T07:00:00"},{"address":"/sensors/48/state/presence","operator":"eq","value":"true"},{"address":"/sensors/49/state/dark","operator":"eq","value":"true"},{"address":"/sensors/49/state/dark","operator":"dx"},{"address":"/sensors/50/state/status","operator":"lt","value":"1"}],"actions":[{"address":"/groups/2/action","method":"PUT","body":{"scene":"fHEB6SwiIIlSXMt"}},{"address":"/sensors/50/state","method":"PUT","body":{"status":1}}]}

would this be it:

sensor:
  - platform: rest
    resource: http://192.168.1.212/api/API/rules/20
    method: GET
    value_template: >
      {{ value_json.conditions[1].value }}

not sure bout the [1]

btw this command_line works also:

curl -X GET http://192.168.1.212/api/API/rules/20

any preference between the command_line and rest sensors?

testing these now:

- platform: rest
  name: Schedule Hue sensor rest
  resource: http://192.168.1.212/api/API/rules/14
  method: GET
  value_template: >
    {{ value_json.conditions[1].value }}


- platform: command_line
  name: Schedule Hue sensor cmd
  command: curl -X GET http://192.168.1.212/api/API/rules/25
  value_template: >
    {{ value_json.conditions[1].value }}

UPDATE
should have been {{ value_json.conditions[0].value }} (duh, first item in the list is 0… ;-0

And please, since you do set these times in HA, give me that code too…?

as an update to the previous post, and to keep things tidy, I can report success:

31

command_line and rest sensors both work. Command line seems to react somewhat slower though.

- platform: command_line
  name: Laundry schedule
  command: curl -X GET http://192.168.1.212/api/API/rules/93
  value_template: >
    {{ value_json.conditions[0].value }}

or:

- platform: rest
  name: Corridor schedule rst
  resource: http://192.168.1.212/api/API/rules/74
  method: GET
  value_template: >
    {{ value_json.conditions[0].value }}

template sensor based on the above:

laundry_schedule_night:
  friendly_name: Laundry schedule night
  value_template: >
    {{states('sensor.laundry_schedule')[1:-13]}}
laundry_schedule_day:
  friendly_name: Laundry schedule day
  value_template: >
    {{states('sensor.laundry_schedule')[11:-3]}}

creating the option for an automation like this:

  - alias: 'Weekend Switch Off Masterbedroom motion sensor'
    id: 'Weekend Switch Off Masterbedroom motion sensor'
    initial_state: 'on'
    trigger:
      #platform: time
      #at: '07:00:00' # the time set in the Hue app, which has only day setting, and doesn't recognize weekend
      platform: template
      value_template: >
        {{now().time().strftime(%H:%M) == states('sensor.master_bedroom_schedule_day')}}
    condition:
      - condition: template
        value_template: >
          {{ is_state ('sensor.activity_selection', 'Slapen')}}
      - condition: template
        value_template: >
          {{is_state('binary_sensor.workday_sensor','off') or 
            is_states('input_boolean.home_mode_vacation','on')}}
    action:
      - service: switch.turn_off
        entity_id: switch.master_bedroom_motion_sensor_switch

Sure. Switching nightmode on:

- alias: "Nachtmodus wurde aktiviert"
    trigger:
      - platform: state
        entity_id: input_boolean.b_nightmode
        to: "on"
    action:
      # Bewegungsmelder Toilette 
      - service: rest_command.set_hue_rule
        data_template:
          id: 3
          payload: '{"conditions":[{"address":"/sensors/8/state/status","operator":"lt","value":"1"},{"address":"/config/localtime","operator":"in","value":"T07:30:00/T{{now().strftime("%H")}}:00:00"},{"address":"/sensors/7/state/dark","operator":"eq","value":"true"},{"address":"/sensors/6/state/presence","operator":"eq","value":"true"},{"address":"/sensors/6/state/presence","operator":"dx"}]}'
      - service: rest_command.set_hue_rule
        data_template:
          id: 5
          payload: '{"conditions":[{"address":"/sensors/8/state/status","operator":"lt","value":"1"},{"address":"/config/localtime","operator":"in","value":"T07:30:00/T{{now().strftime("%H")}}:00:00"},{"address":"/sensors/7/state/dark","operator":"eq","value":"true"},{"address":"/sensors/6/state/presence","operator":"eq","value":"true"},{"address":"/sensors/7/state/dark","operator":"dx"}]}'
      - service: rest_command.set_hue_rule
        data_template:
          id: 14
          payload: '{"conditions":[{"address":"/sensors/8/state/status","operator":"lt","value":"1"},{"address":"/config/localtime","operator":"in","value":"T{{now().strftime("%H")}}:00:00/T07:30:00"},{"address":"/sensors/7/state/dark","operator":"eq","value":"true"},{"address":"/sensors/6/state/presence","operator":"eq","value":"true"},{"address":"/sensors/6/state/presence","operator":"dx"}]}'
      - service: rest_command.set_hue_rule
        data_template:
          id: 15
          payload: '{"conditions":[{"address":"/sensors/8/state/status","operator":"lt","value":"1"},{"address":"/config/localtime","operator":"in","value":"T{{now().strftime("%H")}}:00:00/T07:30:00"},{"address":"/sensors/7/state/dark","operator":"eq","value":"true"},{"address":"/sensors/6/state/presence","operator":"eq","value":"true"},{"address":"/sensors/7/state/dark","operator":"dx"}]}'
      - delay: "00:00:01"

And for switching it off:

- alias: "Nachtmodus wurde deaktiviert"
    trigger:
      - platform: state
        entity_id: input_boolean.b_nightmode
        to: "off"
    action:
       # Bewegungsmelder Toilette (MotionSensor 6)
      - service: rest_command.set_hue_rule
        data_template:
          id: 3
          payload: '{"conditions":[{"address":"/sensors/8/state/status","operator":"lt","value":"1"},{"address":"/config/localtime","operator":"in","value":"T{{now().strftime("%H")}}:00:00/T23:00:00"},{"address":"/sensors/7/state/dark","operator":"eq","value":"true"},{"address":"/sensors/6/state/presence","operator":"eq","value":"true"},{"address":"/sensors/6/state/presence","operator":"dx"}]}'
      - service: rest_command.set_hue_rule
        data_template:
          id: 5
          payload: '{"conditions":[{"address":"/sensors/8/state/status","operator":"lt","value":"1"},{"address":"/config/localtime","operator":"in","value":"T{{now().strftime("%H")}}:00:00/T23:00:00"},{"address":"/sensors/7/state/dark","operator":"eq","value":"true"},{"address":"/sensors/6/state/presence","operator":"eq","value":"true"},{"address":"/sensors/7/state/dark","operator":"dx"}]}'
      - service: rest_command.set_hue_rule
        data_template:
          id: 14
          payload: '{"conditions":[{"address":"/sensors/8/state/status","operator":"lt","value":"1"},{"address":"/config/localtime","operator":"in","value":"T23:00:00/T{{now().strftime("%H")}}:00:00"},{"address":"/sensors/7/state/dark","operator":"eq","value":"true"},{"address":"/sensors/6/state/presence","operator":"eq","value":"true"},{"address":"/sensors/6/state/presence","operator":"dx"}]}'
      - service: rest_command.set_hue_rule
        data_template:
          id: 15
          payload: '{"conditions":[{"address":"/sensors/8/state/status","operator":"lt","value":"1"},{"address":"/config/localtime","operator":"in","value":"T23:00:00/T{{now().strftime("%H")}}:00:00"},{"address":"/sensors/7/state/dark","operator":"eq","value":"true"},{"address":"/sensors/6/state/presence","operator":"eq","value":"true"},{"address":"/sensors/7/state/dark","operator":"dx"}]}'
      - delay: "00:00:01"

I had to copy the conditions rule by rule from the API (just call it in a browser window with /sensors/{ruleID}) and paste them into my code :sweat: It seems that my conditions are in a different order than yours, hence the difference between conditions[0] and conditions[1]…

That’s the code for one single motion sensor. Added a short delay after each sensor to prevent the Hue bridge from overload.

1 Like

o dear… thanks…:wink:

may I ask why you don’t only use the changing part in the payload? As far as I can understand this now (need some more studying… you change the time in the schedule to now().strftime(“%H”) but that’s the only thing changing in all sensors?

couldn’t you just ‘put’ that condition then, and not all the others per sensor? Or would that delete everything else?

like this:

- alias: "Nachtmodus wurde aktiviert"
    trigger:
      - platform: state
        entity_id: input_boolean.b_nightmode
        to: "on"
    action:
      # Bewegungsmelder Toilette 
      - service: rest_command.set_hue_rule
        data_template:
          id: 3
          payload: '{"conditions": {"address":"/config/localtime","operator":"in","value":"T07:30:00/T{{now().strftime("%H")}}:00:00"}
      - service: rest_command.set_hue_rule
        data_template:
          id: 5
          payload: {"address":"/config/localtime","operator":"in","value":"T07:30:00/T{{now().strftime("%H")}}:00:00"}
      - service: rest_command.set_hue_rule
        data_template:
          id: 14
          payload: '{"conditions":{"address":"/config/localtime","operator":"in","value":"T{{now().strftime("%H")}}:00:00/T07:30:00"}
      - service: rest_command.set_hue_rule
        data_template:
          id: 15
          payload: '{"conditions": {"address":"/config/localtime","operator":"in","value":"T{{now().strftime("%H")}}:00:00/T07:30:00"}
      - delay: "00:00:01"

Forget about the above_
I see I missed what you said here:

could you please tell me in words what you achieve by setting Nachtmodus aktiviert und deaktiviert. What actually changes in the Hue setup other than the actual time you do it.

(I am correct you dont actually switch on/off anything ?) Like we can set to ‘do nothing’ in the app, or set a scene, but I dont’ see you do that? Those would be my main reasons to try and do this all,

  • set day/night to do nothing , or set a scene
  • set sensitivity

btw @Martso have constructed this mapper, I think you wanted something like the too for the friendly_names? hop over to Map values for an input_select? and we can work this dedicated issue out without further disruption of this thread…

Regarding Remote needs a "standby" state · Issue #85 · robmarkcole/Hue-sensors-HASS · GitHub and trying to solve it.
Any ideas on how to solve this situation:

My situation is that I have a motion sensor that triggers me entering the room and turns on the lights. But when I want to leave and don’t want to wait for the motion timeout I want to press the TAP. This only works once, because the second time I do this the TAP is already in that 1_click state and does not fire again.

In the past I had used a rest trigger that set the state after 1 second to idle again based on the update_time. Now it works confusing.

HI @robmarkcole and @yottatsa

please have a look at https://github.com/robmarkcole/Hue-sensors-HASS/issues/111 where I added the tholddark (renamed to ‘threshold’ for the more-info) to the list of attributes of the sensors:

12

hope you would merge that with the CC (btw sensitivity works perfectly! so much better than adding those manually, thanks!)

Hi all,

I’m receiving the following error multiple times for a number of my HUE devices that are using the custom component:

Wed Jan 30 2019 08:34:21 GMT+0000 (GMT)

Update for sensor.downstairs_motion_sensor fails
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/local/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/local/lib/python3.6/http/client.py", line 986, in send
    self.sock.sendall(data)
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/local/lib/python3.6/site-packages/urllib3/util/retry.py", line 367, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/usr/local/lib/python3.6/site-packages/urllib3/packages/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/local/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/local/lib/python3.6/http/client.py", line 986, in send
    self.sock.sendall(data)
urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError(9, 'Bad file descriptor'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 650, in urlopen
    conn = conn and conn.close()
  File "/usr/local/lib/python3.6/http/client.py", line 949, in close
    sock.close()   # close it manually... there may be other refs
  File "/usr/local/lib/python3.6/socket.py", line 417, in close
    self._real_close()
  File "/usr/local/lib/python3.6/socket.py", line 411, in _real_close
    _ss.close(self)
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/app/homeassistant/helpers/entity.py", line 221, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/app/homeassistant/helpers/entity.py", line 349, in async_device_update
    await self.hass.async_add_executor_job(self.update)
  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/sensor/hue.py", line 236, in update
    self._data.update()
  File "/usr/src/app/homeassistant/util/__init__.py", line 315, in wrapper
    result = method(*args, **kwargs)
  File "/config/custom_components/sensor/hue.py", line 186, in update
    response = requests.get(self.url)
  File "/usr/local/lib/python3.6/site-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: [Errno 9] Bad file descriptor

Can anyone shed any light on what’s causing it? Running latest HA (0.86.4) but it’s been happening for a while.

I’ve not updated this custom component in some time (it’s at 0.8) but I gather that the sensor component hasn’t changed much.

Sensor component has been changed a lot. Please consider upgrading. You will need to modify the configuration.yaml slightly.

I’ll check that but my installed version is 0.8 which is the stable version that’s available. Would it be worth going with a later version even though this is in development?

I’m on 1.0.3 and it works as is should :slight_smile:

new version is just as stable, simply changed to binary_sensor (vs regular sensor) added a device_tracker, and added a few attributes. Which means nothing for stability. If anything, it has bettered!
Not even sure I understand why this should be called the stable version. (believe me I am a heavy user…:slight_smile:

1 Like

Thanks @yottatsa @Mariusthvdb @yan All working now and no hue.py errors in my logs :slight_smile:

1 Like

I’ve been using the Hue Sensors module for quite some time now to support the 4 button dimmer. Some months ago we started noticing phantom button presses coming from some of them. It has taken time, but we’ve figured out that when the battery is less than fresh and the temperature in the house is allowed to drop a bit, the remote sends out jittering battery charge/% updates at a 5 minute interval. …like clock work. Each time this happens, the date attribute doesn’t change, the battery attribute does, and the most recent button press event is re-issued. I tried updating to 0.7 as I noticed it had an update for, “Test to make sure a button has actually been pressed on the dimmer.”. It didn’t look like it would do what I needed, but tried it anyway. …it didn’t. I have enough information to check the date/time attribute and filter these things out, but that can’t be the right solution. I’ve held off on updating further because my core HASS install is also behind and I need time for that upgrade project.

Before I go digging into the code:

  • Is this an issue that has already been reported? (I’ve searched)
  • Has it already been fixed?
  • If it hasn’t been fixed, what is the right layer to fix it in?

Thanks!

Just found this that looks like the connection to battery hasn’t been noticed by others:
https://github.com/robmarkcole/Hue-sensors-HASS/issues/55

I’ve posted an update today, which set state to None if only attributes has been changed. Please try this version

2 Likes

@yottatsa’s update has solved the click event repeated every 5 minutes problem for me. Thanks! :slight_smile:

Will this break automations was that are built around multiple presses of a button? My switches are setup as toggles eg button 1 toggles my lamp, so often I will press button 1 to turn the lamp on and then a bit later press it again to turn it off.

it shouldn’t break your automation since pressing a button should change the last_update, and I hide the state only if both state and last_update hasn’t been changed.