REST sensor with multiple attributes

This sensor works perfectly, but I have to call the website multiple time, which is not ideal.

How can I make multiple sensors with just one call - or at least add multiple attributes to that sensor?
What would be the better approach?

  - platform: rest
    resource: url
    name: Information
    scan_interval: 60
    value_template: >-
      {% if value_json %}
        {{ value_json.information}}
      {% else %}
        None
      {% endif %}
    unit_of_measurement: ""

You can read in multiple attributes from one read. 1 state and then have attributes.

Or use the rest integration rather than the rest sensor platform. You can have as many sensors as you want from the one resource call. The examples shown here aren’t great, each value_template is just set to “OK” but each one can be a full template like your example above. This negates the need for template sensors to retrieve values from attributes.

That looks promising.

My “json” looks like that:
{ "rain": "off" , "raindensity": 63 }

rest:
  - authentication: basic
    username: "???"
    password: "???"
    scan_interval: 60
    resource: http://192.168.1.12/status.xml
    sensor:
      - name: "Multiple Sensors"
        json_attributes_path: "$.response.?????"
        value_template: "??"
        json_attributes:
          - "rain"
          - "raindensity"

I don’t have authentication, can I just leave the password blank?
How do I get the response.??? right for my example?

What is the value template? I have a binary sensor and a numeric sensor in %. I guess I have to make a new sensor for each type?

Authentication is optional https://www.home-assistant.io/integrations/rest/#authentication

So like this:

rest:
  - resource: http://192.168.1.12/status.xml
    scan_interval: 60 # the default is 30 seconds if you leave this line out, or change it to what you need.
    sensor:
      - name: "Rain"
        value_template: "{{ value_json.rain }}"
      - name: "Rain Density"
        value_template: "{{ value_json.raindensity }}"

You should add a unit_of_measurement to that last sensor.

Is the rain sensor state only on or off?

If so it should be a binary sensor. Like this:

rest:
  - resource: http://192.168.1.12/status.xml
    scan_interval: 60 # the default is 30 seconds if you leave this line out, or change it to what you need.
    binary_sensor:
      - name: "Rain"
        value_template: "{{ value_json.rain == 'on' }}"
    sensor:
      - name: "Rain Density"
        value_template: "{{ value_json.raindensity }}"
1 Like

Thanks that helped a lot.

    binary_sensor:
      - name: "Rain"
        value_template: "{{ value_json.rain == 'on' }}"
    sensor:
      - name: "Rain Density"
        value_template: "{{ value_json.raindensity }}"
        unit: "%"

Is value_json.rain == 'on' just to make sure that if the data is anything other than on it is off?

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!