Hue: rest sensor or not (to reveal many attributes) from_json

Hi,

trying to create a sensor for my Hue hub’s capabilities, I am unsure how to proceed. I think know how to set the json_attributes, but the value_template is the issue, since that can only hold the full string of the resource, which is way to long…

the resource would be in this form:

http://192.168.1.212/api/resource_id/capabilities

and then the sensor config could be:

  - platform: rest
    name: Hue capabilities
    resource: !secret hue_capablities_resource
#    authentication: basic
    value_template: >
      {{ value_json }}
    json_attributes:
      - lights
      - sensors
      - clip
      - zll
      - zgp
      - groups
      - scenes
      - lightstates
      - schedules
      - rules
      - conditions
      - actions
      - resourcelinks
      - streaming
    headers:
      Content-Type: application/json
      Authorization: !secret api_bearer_token
      User-Agent: Home Assistant REST sensor

Not sure about the bottom part, since I don’t need to access the HA instance, but this is a copy of another rest sensor in my system for now.

The output of the resource is:

{"lights":{"available":18,"total":63},
 "sensors":{"available":174,"total":250,
 "clip":{"available":174,"total":250},
 "zll":{"available":45,"total":64},
 "zgp":{"available":45,"total":64}},
 "groups":{"available":35,"total":64},
 "scenes":{"available":0,"total":200,
 "lightstates":{"available":0,"total":12600}},
 "schedules":{"available":92,"total":100},
 "rules":{"available":53,"total":250,
 "conditions":{"available":848,"total":1500},
 "actions":{"available":590,"total":1000}},
 "resourcelinks":{"available":44,"total":64},
 "streaming":{"available":1,"total":1,"channels":10},
 "timezones":{"values":["CET","CST6CDT","EET","EST","etcetcetcetc"]}}

the “timezones” part of the output makes me need to exclude that (or as in the above effort, list all included attributes), because it is a full list of all available timezones, which is way to long for the sensor, and not necessary at that.

Or, should I maybe create a template sensor using attribute_templates? In which case the question would remain: how to set the main template, on which to base the attributes belonging to it?

please have a look with me, thanks!

One tool that might be handy is jq. It is for processing json. There are examples here on the forum (but jq is probably too short to search on!)

The other thing to think about: do you want one sensor with multiple attributes, or a whole series of separate sensors?

yes I figured that too, create a set of template sensors based on the full rest sensor, as on: RESTful - Home Assistant

that still would need me to find a way to have the ‘mother’ rest sensor have a state that would fit the HA state machine wouldn’t it?

Thought about that in the template sensor, and then have the main sensor check if the hub is ‘on’ followed by the attribute_templates for the list I posted above, which would then each need the full rest-resource. Could that be possible?

like:

sensor:
  - platform: template
    sensors:
      hue_capabilities:
        value_template: >-
          {{is_state('device_tracker.hue_hub','home') }}
        attribute_templates:
          lights: >
            {{http://192.168.1.212/api/resource_id/capabilities/lights}}
          sensors: >
            {{http://192.168.1.212/api/resource_id/capabilities/sensors}}
          etc:>
            {{...}}

thing is the {{http://192.168.1.212/api/resource_id/capabilities/lights}} doesn’t work like that, I need to parse that differently… though Login - Philips Hue Developer Program suggests otherwise, and might need a from_json template? Templating - Home Assistant

never used the latter, and there are no real informative examples in the docs how to use the resource in this template…

update:

after a lot of experimental sensor configs in the realm of Rest and Command, this works alright:

- platform: rest
  name: Hue capabilities lights
  resource: !secret hue_capablities_resource
  method: GET
  value_template: >
    {{value_json.lights}}
  scan_interval: 86400

- platform: rest
  name: Hue capabilities sensors
  resource: !secret hue_capablities_resource
  method: GET
  value_template: >
    {{value_json.sensors}}
  scan_interval: 86400

#- platform: rest
#  name: Hue capabilities zll
#  resource: !secret hue_capablities_resource
#  method: GET
#  value_template: >
#    {{value_json.zll}}
#  scan_interval: 86400
#
#- platform: rest
#  name: Hue capabilities clip
#  resource: !secret hue_capablities_resource
#  method: GET
#  value_template: >
#    {{value_json.clip}}
#  scan_interval: 86400
#
#- platform: rest
#  name: Hue capabilities zgp
#  resource: !secret hue_capablities_resource
#  method: GET
#  value_template: >
#    {{value_json.zgp}}
#  scan_interval: 86400

- platform: rest
  name: Hue capabilities groups
  resource: !secret hue_capablities_resource
  method: GET
  value_template: >
    {{value_json.groups}}
  scan_interval: 86400

- platform: rest
  name: Hue capabilities scenes
  resource: !secret hue_capablities_resource
  method: GET
  value_template: >
    {{value_json.scenes}}
  scan_interval: 86400

#- platform: rest
#  name: Hue capabilities lightstates
#  resource: !secret hue_capablities_resource
#  method: GET
#  value_template: >
#    {{value_json.lightstates}}

- platform: rest
  name: Hue capabilities schedules
  resource: !secret hue_capablities_resource
  method: GET
  value_template: >
    {{value_json.schedules}}
  scan_interval: 86400

- platform: rest
  name: Hue capabilities rules
  resource: !secret hue_capablities_resource
  method: GET
  value_template: >
    {{value_json.rules}}
  scan_interval: 86400

#- platform: rest
#  name: Hue capabilities conditions
#  resource: !secret hue_capablities_resource
#  method: GET
#  value_template: >
#    {{value_json.conditions}}
#  scan_interval: 86400
#
#- platform: rest
#  name: Hue capabilities actions
#  resource: !secret hue_capablities_resource
#  method: GET
#  value_template: >
#    {{value_json.actions}}
#  scan_interval: 86400

- platform: rest
  name: Hue capabilities resourcelinks
  resource: !secret hue_capablities_resource
  method: GET
  value_template: >
    {{value_json.resourcelinks}}
  scan_interval: 86400

- platform: rest
  name: Hue capabilities streaming
  resource: !secret hue_capablities_resource
  method: GET
  value_template: >
    {{value_json.streaming}}
  scan_interval: 86400

# invalid, attribute_templates not valid for rest sensor
#  attribute_templates:
#    lights: '{{value_json.lights}}'
#    sensors: '{{value_json.sensors}}'
#    clip: '{{value_json.clip}}'
#    zll: '{{value_json.zll}}'
#    zgp: '{{value_json.zgp}}'
#    groups: '{{value_json.groups}}'
#    scenes: '{{value_json.scenes}}'
#    lightstates: '{{value_json.lightstates}}'
#    schedules: '{{value_json.schedules}}'
#    rules: '{{value_json.rules}}'
#    conditions: '{{value_json.conditions}}'
#    actions: '{{value_json.actions}}'

some of these sensors are nested apparently (commented them out above), while the full rest resource shows them unnested… made it rather difficult to iterate.

As you can see, they all use the same resource, but because attribute_templates aren’t supported for rest sensors (see bottom commented sensor) we need to create all of these individual sensors, which is a pity because that takes extra resource. hence my adding the scan_interval to once a day.

Hope to hear from fellow members here if I can do this more efficiently.
thanks!

yes!, just for the sake of it I tried this:

- platform: rest
  name: Hue capabilities
  resource: !secret hue_capablities_resource
  method: GET
  value_template: >
    {{value_json.lights}}
  json_attributes:
    - lights
    - sensors
    - clip
    - zll
    - zgp
    - groups
    - scenes
    - lightstates
    - schedules
    - rules
    - conditions
    - actions
    - resourcelinks
    - streaming
  scan_interval: 86400

which works just perfectly! It mis a bit of a hack, since I don’t need the state to be the lights, but this allows me to use the json_attributes as I want them to be, and easily template these attributes.

in a raw unstyled card:

more-info:


So for now, this is marked solved…unless someone knows of a way to have the state (value_template) be another source than the desired json_attributes…

I’d just have the main state as a count and leave everything else in the attributes.

{{ value_json.items() | count }}

It’ll output the count… so it will always be 15.

EDIT: Nevermind I see how this is laid out now.