Setting Up Dual RESTFul Sensors

In an attempt to retrieve the “lightllevel” from my 2 Hue Motion sensors I have come up with the following which works:

rest:
  - resource: http://111.222.333.444/api/juCgDpHNoHkHNYqIlU6E3kgjixsdfeOFmMG9Cux2Tlt/sensors
    method: GET
    sensor:
      - name: RAR 112 Lightlevel
        json_attributes_path: "$.112.state"
        value_template: "OK"
        json_attributes:
          - "lightlevel"
      - name: RAR 141 Lightlevel
        json_attributes_path: "$.141.state"
        value_template: "OK"
        json_attributes:
          - "lightlevel"

My question is… is there a way I can have both sensors return the 'lightlevel" value as the “state” of the sensor as opposed to returning 'OK" as the state and the “lightlevel” as an attribute?

value_template: "{{ value.json['112']['state']['lightlevel'] }}"

Should work provided the REST data is under 255 characters long. Alternatively, leave it as it is and set up template sensors whose state pulls the attribute from these sensors.

Something like this

rest:
  - resource: http://111.222.333.444/api/juCgDpHNoHkHNYqIlU6E3kgjixsdfeOFmMG9Cux2Tlt/sensors
    method: GET
    sensor:
      - name: RAR 112 Lightlevel
        value_template: "{{ value_json.112.state.lightlevel }}"
      - name: RAR 141 Lightlevel
        value_template: "{{ value_json.141.state.lightlevel }}"

I already tried that, both return ‘unknown’ for state in the Developers State tool.

Please paste both the sensor you tried that didn’t work, and the response from that URL when accessed with a web browser.

You will have to use my format ['112'] as opposed to @koying’s .112 which will get confused with a list index [112] — unless you are genuinely after the 113rd and 115th elements.

Sensors:

rest:
  - resource: http://aaa.bbb.ccc.ddd/api/juCgDpHNoHkxybffqIlU6E3kgjiAbOFmMG9Cux2Tlt/sensors
    method: GET
    sensor:
      - name: RAR 112 Lightlevel
        value_template: "{{ value.json['112']['state']['lightlevel'] }}"
      - name: RAR 141 Lightlevel
        value_template: "{{ value.json['141']['state']['lightlevel'] }}"

46 of these type entries are returned as the JSON from resource: above

{
	"112": {
		"state": {
			"lightlevel": 837,
			"dark": true,
			"daylight": false,
			"lastupdated": "2021-06-03T23:38:53"
		},
		"swupdate": {
			"state": "noupdates",
			"lastinstall": "2021-04-25T14:37:54"
		},
		"config": {
			"on": true,
			"battery": 100,
			"reachable": true,
			"alert": "none",
			"tholddark": 22000,
			"tholdoffset": 2000,
			"ledindication": false,
			"usertest": false,
			"pending": []
		},
		"name": "Hue ambient light sensor 1",
		"type": "ZLLLightLevel",
		"modelid": "SML001",
		"manufacturername": "Signify Netherlands B.V.",
		"productname": "Hue ambient light sensor",
		"swversion": "6.1.1.27575",
		"uniqueid": "00:17:88:01:09:16:a0:df-02-0400",
		"capabilities": {
			"certified": true,
			"primary": false
		}
	},
}

Everything works fine when I have it defined as this below, but I could not figure out how to get this one to support the 2nd sensor:

sensor:
  - platform: rest
    resource: http://aaa.bbb.ccc.ddd/api/juCgDpHNoHkHNYqIlxhauhdmcU6E3kgjiAbOFmMG9Cux2Tlt/sensors/141
    name: Hue 141 Lightlevel
    method: GET
    value_template: "{{ value_json.state.lightlevel }}"

I can’t see anything wrong with your first rest definition.

It would also seem to me that this would work for your second sensor using platform: rest:

sensor:
  - platform: rest
    resource: http://aaa.bbb.ccc.ddd/api/juCgDpHNoHkHNYqIlxhauhdmcU6E3kgjiAbOFmMG9Cux2Tlt/sensors/141
    name: Hue 141 Lightlevel
    value_template: "{{ value_json.state.lightlevel }}"

  - platform: rest
    resource: http://aaa.bbb.ccc.ddd/api/juCgDpHNoHkHNYqIlxhauhdmcU6E3kgjiAbOFmMG9Cux2Tlt/sensors/112
    name: Hue 112 Lightlevel
    value_template: "{{ value_json.state.lightlevel }}"

My 1st ‘platform’ example was working. I was getting a ‘dupe key’ message when trying to add the 112 side. I believe that was because I was adding it with another sensor: label.

Your way works fine. I am thinking your way is making 2 individual trips to the Hue Hub, no? Is there any way to ‘set’ the polling frequency for these individual sensors? I keep seeing examples of folks setting a “scan_interval:”, but no where in the docs have I seen it as an option.

Another question I couldn’t figure out was what is the difference between setting these up as a ‘platform’ and not? :slight_smile:

Setting up as a rest sensor platform (under a top-level rest) allows setting the scan_interval: see the docs. It also only makes one call to the resource.

The code you posted starting with rest: looks like it should work: what’s the problem?

This returns ‘unknown’ for ‘state’ for both sensors in the Developer ‘State’ Tool:

rest:
  - resource: http://aaa.bbb.ccc.ddd/api/juCgDpHNoHkxybffqIlU6E3kgjiAbOFmMG9Cux2Tlt/sensors
    method: GET
    sensor:
      - name: RAR 112 Lightlevel
        value_template: "{{ value.json['112']['state']['lightlevel'] }}"
      - name: RAR 141 Lightlevel
        value_template: "{{ value.json['141']['state']['lightlevel'] }}"

Your method works fine for both sensors.

So your method only make one call?

don’t you use the HUE integration? because that does not all for you, and as explained in the other thread, pushes all state changes so no need to create those rest sensors at all

you should really try an avoid to poll the HUE hub as much as possible.

No, the sensor: method makes two. It’s not “my” method, I’d prefer the rest: method, but I don’t know why it’s not working for you.

I do not believe the light or temperature are ‘pushed’ by the Hue device. I could be wrong, as I am new to this stuff, but in my testing of the motion / lightlevel / temperature, only the motion is pushed, and that is only with this new release of HA (and the latest Hue update of the last few weeks I assume).

Believe they are and I’ve just tested the correct working.

You could add here though and report your finding Unhandled Hue events · Issue #49 · home-assistant-libs/aiohue · GitHub

Quite confusing.

Are we talking about

This is my Hue Motion sensor (I guess there only one model) bound to the hue bridge.
It’s been working for a long time.

Re push/pull, every sensors are push, afaict from the hue integration code.

Meh. It’s value_json. An underscore, not a dot.

1 Like

exactly, so why create an expensive rest sensor, when you have all you need in core already?

1 Like

Indeed, especially considering a rest sensor is specifically pull :wink:

Dammit, should have spotted that. Well done!

Nothing to be proud of, I just tested it and looked at the error message :wink:
In all honesty, the message is not helpful until you reduce to `{{ value.json }}’