REST sensor with multiple attributes

You can create a Feature Request (search the category for an existing one first - duplicates will be linked to earlier requests and closed) but be aware that the devs are discouraging the use of attributes where a seperate sensor will do.

1 Like

I suspect you could make a case for attributes for the template binary sensor as they don’t have an associated device the extra sensor could be attached to.

1 Like

Funny enough, the template one does seem to support attributes, is the REST one which doesnt

Yes it does. Look at the example:

Edit: oh, restful binary sensor. Ignore that ^

1 Like

Further to the comments above, I’ve been experimenting with rest sensor but I am unable to get the value_template with multiple attributes to work - I am unable to set the “value_template” to anything other than “OK”, and this is all I can find in the documentation.

Here is my config.yaml entry:

rest:  
  - resource: http://my_HP:3000/planets # a VSOP87 planet positions server
    method: GET
    headers:
      User-Agent: Home Assistant
      Accept: application/json
    scan_interval: 300
    sensor:
      - name: "My HP Sun"
        unique_id: phil121
        value_template: "OK"
        json_attributes_path: "$.Sun"
        json_attributes:
            - "RA"
            - "Dec"
            - "Alt"
            - "Az"
            - "Rise"
            - "Set"
      - name: "My HP Mercury"
        unique_id: phil122
        value_template: "OK"
        json_attributes_path: "$.Mercury"
        json_attributes:
            - "RA"
            - "Dec"
            - "Alt"
            - "Az"
            - "Rise"
            - "Set"

I had limited success with:

value_template: >
    {{ state_attr('sensor.my_hp_sun', 'alt')|float(0) > 0.0 }}

But it seems I cannot reference an attribute before it is defined, and it always evaluates to false.

Any idea how I can template a useful value_template for a rest sensor?

I found the answer in another post. In case anyone wonders, you must reference the json rather than state_attr:

rest:  
  - resource: http://my_hp:3000/planets # a VSOP87 planet positions server
    method: GET
    headers:
      User-Agent: Home Assistant
      Accept: application/json
    scan_interval: 300
    sensor:
      - name: "Phil-HP Sun"
        unique_id: phil121
        value_template: "{{ value_json['Sun']['Alt']|float(0) > 0.0 }}"
        json_attributes_path: "$.Sun"
        json_attributes:
            - "RA"
            - "Dec"
            - "Alt"
            - "Az"
            - "Rise"
            - "Set"

… much more useful than “OK” for the state of the sensor.