Hue motion sensors + remotes: custom component

Hey thanks for the info. So takes advantage of the coming home feature I see in the app. Do I have to enable this in the app or do some thing in the clip api debugger? I don’t see listed it as an entity. Is it possible to find out more info about it …how long before it lists you as not home if not sensing motion etc? Sorry I’m looking around but info is vague

Edit: Well it looks like at least some of it is using geolocation (maybe gps, maybe cell towers) in the Hue app then passing that on to the bridge?

So i have setup my Hue Bridge using ‘discovery’ and it all just worked.

but now, I don’t seem to have a phue.conf? file at all. any ideas?

@yottatsa and @robmarkcole

been wanting to ask for a longer period, but never did…
Hue motion sensors are uncalibrated and are Off/low 1.5 to 2 degrees in general. There are many posts about this on the developers community forum, but all Philips devs are saying is one might have a look in the future.

Which made me wonder if you CC couldn’t build in a variable to be able add to the value the sensors read now.

It is rather obvious to do yourself. Have a motion sensor next to your room thermostat, and note the difference. This difference could than be set in the config settings of the CC, or even in the CC itself, just as the scan_interval now is set. It would make it very easy and prevent the necessity of further correcting all temp sensors in HA.

Add

temp_calibration = 1.5 (or whatever your difference would need of course)

and then have this:

elif response["type"] == "ZLLTemperature":
    if response["state"]["temperature"] is not None:
        data = {"temperature": response["state"]["temperature"] / 100.0 + temp_calibration}
    else:
        data = {"temperature": "No temperature data"}

use that temp_calibration ?
Could you please give it a thought?

Well you could achieve this using a template sensor, and I think thats preferable to adding more complexity to the CC, but feel free to make an issue on the CC Github including links to these reports

Ok thanks!
I will have a look, but it wasn’t very complex adding a +1.7 to the CC :wink:
I hard coded it for now, and a config setting shouldn’t be much harder. I’d think you’d do many people a great service/pleasure to add this, preventing the need for yet again a full set of templates…

Fyi, here are 2 threads on the developers community, where I also bumped the discussions:

https://developers.meethue.com/forum/t/how-to-calibrate-temperature-in-philips-hue-motion-sensor/5564/12

https://developers.meethue.com/forum/t/motion-sensor-temperature-calibration/5512

Like I said, if you create an issue, and its an issue for many others, then we can look at it.

TBH I wouldn’t expect high accuracy from these sensors. But if there sensors were systematically reporting high or low, thats an issue for Hue production team

I have had Sucess with the remote buttons, which is awesome, and I can see the motion sensor, and it works great, but the temp and lx sensors are not getting a reading. I have only had it running for about 10 mins…is there a set refrest time or somthing i am missing?

quite right! That why users are posting, asking or complaining on the developers forum.
Ive got 11 of them, and can say it is rather a systematical issue. they’re al off, at about the same amount …

I created an issue on the Github linking this thread post, thanks for considering!

As it stands Ive added the 1.7 and suddenly all of my Hue sensors and thermostats and indoor climate sensors (that are calibrated) are in nicely in sink :+1:

do have the odd rounding issue on occasion:

06

Well this does raise the question of whether the sensor platform should have a calibration_factor attribute.

https://github.com/home-assistant/architecture/issues/135

1 Like

Hey mate, could you post up your template sensor from the config yaml. I have posted mine below, and i’m not seeing a value come through. Motion sensor works as does the remote controls.

  - platform: template
sensors:
  living_room_temperature:
    friendly_name: "Living room temperature"
    value_template: "{{states.sensor.hue_motion_sensor_motion_1.attributes.temperature}}"
  living_room_light:
    friendly_name: "Living room light"
    value_template: "{{states.sensor.hue_motion_sensor_motion_1.attributes.lx}}"

on further investigating I have found there is the below in the error log:

Log Details (WARNING)
Fri Jan 18 2019 21:38:27 GMT+1100 (Australian Eastern Daylight Time)

Could not render template Living room temperature, the state is unknown.

sure see below, but please note, I use the latest version of the CC, which creates binary_sensors for the motion sensor, and not the previous (regular) sensor:

corridor_motion_sensor_temperature:
  friendly_name: 'Corridor temperature'
  value_template: >
    {{state_attr('binary_sensor.corridor_motion_sensor','temperature')|round(2)}}
  unit_of_measurement: '°C'
  device_class: temperature


corridor_motion_sensor_battery:
  friendly_name: 'Corridor battery'
  unit_of_measurement: '%'
  value_template: >
    {{state_attr('binary_sensor.corridor_motion_sensor','battery')}}
  device_class: battery

corridor_motion_sensor_lux:
  friendly_name_template: >
    Corridor: 
    {% set lux = state_attr('binary_sensor.corridor_motion_sensor','lx') %}
    {%- if lux <=1 %}
      moonlight
    {% elif lux <=2 %}
      night light
    {% elif lux <= 10 %}
      dimmed light
    {% elif lux <= 50 %}
      'cosy' living room
    {% elif lux <= 150 %}
      'normal' non-task light
    {% elif lux <= 350 %}
      working / reading
    {% elif lux <= 700 %}
      inside daylight
    {% elif lux <= 2000 %}
      maximum to avoid glare
    {% elif lux <= 10000 %}
      clear daylight
    {% elif lux <= 120000 %}
      direct sunlight
    {% else %}
      too bright!
    {% endif %}
  value_template: >
    {{state_attr('binary_sensor.corridor_motion_sensor','lx')}}

corridor_motion_sensor_light_level:
  friendly_name_template: >
    Corridor: 
    {% set light_level = state_attr('binary_sensor.corridor_motion_sensor','light_level') %}
    {% if light_level <=1 %}
      bright moonlight
    {% elif light_level <=3000 %}
      night light
    {% elif light_level <= 10000 %}
      dimmed light
    {% elif light_level <= 17000 %}
      'cosy' living room
    {% elif light_level <= 22000 %}
      'normal' non-task light
    {% elif light_level <= 25500 %}
      working / reading
    {% elif light_level <= 28500 %}
      inside daylight
    {% elif light_level <= 33000 %}
      maximum to avoid glare
    {% elif lux <= 40000 %}
      clear daylight
    {% elif light_level <= 51000 %}
      direct sunlight
    {% else %}
      too bright!
    {% endif %}
  value_template: >
    {{state_attr('binary_sensor.corridor_motion_sensor','light_level')}}
  device_class: illuminance

in light of the above discussion about temp_calibration, Ive also done this, which works fine:

corridor_motion_sensor_temperature:
  friendly_name: 'Corridor temperature'
  value_template: >
    {{ (state_attr('binary_sensor.corridor_motion_sensor','temperature') +
        states('input_number.temp_calibration')|float)|round(2) }}
  unit_of_measurement: '°C'
  device_class: temperature

and

input_number:
  temp_calibration:
    name: Temp calibration
    initial: 1.5
    min: -2
    max: 2
    step: 0.1
    icon: mdi:thermostat
    unit_of_measurement: '°C'

thanks for posting that up. did you have to do anything else to get the extra data flowing though, I have coppied and pasted your code, and update the name of the sensor, but the sensor reads ‘none’

below is my code, so i think that looks all good, but was there anything else you needed to do to bring the data through?

      corridor_motion_sensor_temperature:
    friendly_name: "Living room temperature New way"
    value_template: "{{state_attr('states.sensor.hue_motion_sensor_motion_1','temperature')|round(2)}}"
    unit_of_measurement: "°C"
    device_class: temperature

first check if your sensor is correct so let me know what happens when you enter this in the dev-template: {{state_attr(‘sensor.hue_motion_sensor_motion_1’,‘temperature’)|round(2)}}

If you use the states() or state_attr() method you shouldn’t use 'states.sensor.etc etc" in your (), but only the (‘sensor.etc etc’)

I’m rebooting @ present and have removed the the extra states from in…i’ll check now.

what service do i need to call to test it?

i’m clearly pretty noob at this…lol

in HA menu check the bottom line for the developer tools:

30

and click the third from right, the dev-template page

then delete all text on the left, below Template editor, and past your code, to see it show the result on the right

2 Likes

thanks heaps mate. i got that now. thanks for the awesome help dude.

1 Like

Sorry for the late reply, been away for a few days. In the hue labs I see that the status does get updated and this is what the logs say:
2019-01-19 12:17:53 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=device_tracker.hue_samsung_sm_g960f, old_state=None, new_state=<state device_tracker.hue_samsung_sm_g960f=home; source_type=gps, latitude=xxxxxxx, longitude=xxxxxx, gps_accuracy=0, last_updated=2019-01-19T11:30:45+01:00, unique_id=L_02_Qp4iU, friendly_name=Samsung SM-G960F, hidden=True @ 2019-01-19T12:17:53.080576+01:00>>

2019-01-20 10:44:32 DEBUG (MainThread) [custom_components.device_tracker.hue] Hue Geofence Samsung SM-G960F: home ({‘last_updated’: datetime.datetime(2019, 1, 20, 4, 46, 49, tzinfo=<DstTzInfo ‘Europe/Amsterdam’ CET+1:00:00 STD>), ‘unique_id’: ‘xxxxxxxxxxx’})

So it does seem to get updated, but I don’t see the component on the main dashboard.

there’s another thing I have been looking for since forever, trying to sync the Hue setup and App with HA. the settings for the accessories, more specifically the motion sensors scheduling rules.
It would really be of great added value, if we could template these in our HA automations. AS it stands now, we have to hard code these Day and night times (and the action) manually in the automations. Would be really really cool if we could use a template for that.

Trying to read the API, one can browse down to /rules, and for one a sensor find this:

http://192.168.1.212/api/api/rules/20

{"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}}]}

I am especially interested in the value":"T22:00:00/T07:00:00 and would like to use these times.

of course my top question would be if you could have a look and see if these could be added to the properties of the motion sensors, and as a consequence have them available for templating of off the binary_sensors now created.

If not, please would you help me create a command line sensor like this which I use for several lights testing for reachability:

- platform: command_line
  name: Driveway reachable
  command: !secret driveway_command
  value_template: >
    {{value_json.state.reachable}}

driveway_command: curl -X GET http://192.168.1.212/api/ID/lights/13/

I can seem to get to the value with the times from http://192.168.1.212/api/API/rules/20

please let me invite @pnbruckner for this too, since you have helped me before in going down in these nested listed templates…

Sorry, this topic is too long to read to try to figure out exactly what you’re asking my help with. Is it simply to extract “T22:00:00/T07:00:00” from the JSON you show if that JSON was obtained via a command_line sensor using curl?

If so, I’d suggest using jq in the command to extract the JSON element. E.g.:

- platform: command_line
  name: blah
  command: curl http://192.168.1.21/api/API/rules/20 | jq '.conditions[]|select(.address == "/config/localtime")|.value'

yess! that’s what I am after. Thanks for helping!.preferably even in 2 separate sensors showing the morning and night time

wouldn’t this need the

  value_template: >
    {{value_json.state.value}}  ?

Getting there:

using these templates to derive from your json sensor:

34

I can create sensor.master_bedroom_schedule_morning and sensor.master_bedroom_schedule_evening

Would this allow for using these in my automations and substitute the current hard coded time:

  - 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
    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

to

  - alias: 'Weekend Switch Off Masterbedroom motion sensor'
    id: 'Weekend Switch Off Masterbedroom motion sensor'
#    initial_state: 'on'
    trigger:
      platform: time
      at: {{states('sensor('master_bedroom_schedule_morning')}}
    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

couldn’t these evening and morning sensors be created directly from the json? It would prevent creating extra intermediary sensors.