REST sensor with multiple attributes

Yes that is what the template does.

Binary sensor templates should resolve to true or false. if the state == ‘on’ then the template evaluates to true, turning the binary sensor on. Any other value will result in the template evaluating to false which will turn off the binary sensor.

1 Like

This:

Should be:

unit_of_measurement: "%"

1 Like

Thanks again.

Is this the right way to do it to check for validity of json?

        value_template: >-
        {% if value_json %}
          {{ value_json.rain == 'on' }}
        {% else %}
          None
        {% endif %}

I still get a configuration error from that.

What error?

Error loading /Users/server/.homeassistant/configuration.yaml: while scanning for the next token found character '%' that cannot start any token in "/Users/server/.homeassistant/rest.yaml", line 6, column 10

Which is the second line in the snippet above.

Nevermind it was an intendation issue. :innocent:

Btw. What is the use of this notation: value_template: >- wouldn’t it work without >-?

Also how can I make the sensors being able to move in areas in the GUI?

In simple terms > indicates the template will be on more than one line. - removes the last carriage return and is almost never needed but seems to be included a lot by people here for some unknown reason. It’s not in any of the document examples.

Play with the radio button options here and observe the result on the lower right of the page:

https://yaml-multiline.info/

1 Like

Good explanation.

How can I give the sensors a unique_id so I can manage them in the interface?

You cant. That is not supported yet.

Ok, let’s hope this will come sometime.

Can I at least move it into an “area” using YAML?

Nope.

I’ve just been steered onto using rest vs restful sensor to do exactly the same as above (ie. grabbing multiple values in a single call and creating multiple sensors).

It seems that restful sensor can do something similar via json_attributes, but they are still just attributes of a single sensor.

Given that the restful seems to be able to do everything that restful sensor can do, why even have restful sensor ? Whats it use case ?

Backwards compatibility for people who were using it before the new integration was made. There are a few integrations like this, e.g: template sensor platform vs template integration, legacy groups vs new groups.

One doubt I have regarding the rest platform, and the OP title ‘REST sensor with atributes’ is,

  • Why does Sensor allow for attributes to be defined, but binary_sensor doesn’t?

I want to have a connectivity binary_sensor setup, but I can’t show details of that connectivity in the same entity. Not sure if this comes after and architectural decision regarding binary_Sensors or just noone has had such request before (smells like a ‘WTH’ candidate then)?

Thanks!

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.