Multiple sensors in one REST call

Hello,

I’m a little bit lost in the templating mechanism, apologies if it seems obvious for you…
Despite the documentation I found, I can’t understand how to avoid doing X calls for my X sensors by using template for doing only one. If I got it well, I’m supposed to retrieve the whole JSON value in one time and then use templates for mapping portion of it?

Here is the original data:

{
    "bedroom1": {
        "temperature": 14.98,
        "humidity": 53.68,
        "battery": 5.26,
        "timestamp": "2019-02-25T21:27:52Z"
    },
    "bedroom2": {
        "temperature": 18.49,
        "humidity": 47.65,
        "battery": 5.08,
        "timestamp": "2019-02-25T21:27:26Z"
    },
    "bedroom3": {
        "temperature": 18.14,
        "humidity": 48.16,
        "battery": 5.15,
        "timestamp": "2019-02-25T21:31:16Z"
    }
}

Here is what I have in my configuration.yaml:

sensor:
- platform: rest
    name: csensor
    resource: http://10.161.0.130:2001/sensors
- platform: template
    sensors:
    temperature:
        value_template: "{{state.sensor.csensor.bedroom1.temperature}}"

Do you have any advises? Should I avoid dictionary? I can update the shape of the received data if needed.

Thank you! :slight_smile:

1 Like

AppDaemon can do this, but you will require a bit of effort getting it all up and running and figuring out the python.

Have a look at the rest sensor’s json_attributes. That will allow you to make one rest call to fetch all data, store the details in attributes and then define one template sensor per attribute.

Thanks for the answer! I already looked at json_attributes and I just tried again, but I’m not sure it can do what I want, still trying, but without any success. How to point to the right ro om? Am I using one extra level of dictionary that I shouldn’t?

I tried multiple variant, but always getting ‘unknown’ as results…

sensor:
- platform: rest
    name: csensor
    resource: http://10.161.0.130:2001/sensors
    json_attributes:
    - temperature
    - humidity
    - battery
    - timestamp
- platform: template
    sensors:
    temperature:
        value_template: '{{state.sensor.csensor.bedroom1.attributes["temperature"]}}'

Have you tried using “bedroom1”, etc. as json_attributes?

And in your template sensor the indentation of value_template is wrong, and the sensor’s entity id looks wrong - superfluous “csensor” and I assume that there is currently no sensor that has “bedroom1” in its entity id?

1 Like

Thanks for the new ideas @exxamalte !

Sadly, I tried again and again this evening, with the dictionary key in json_attributes, with/without csensor (which is the name of the rest sensor we refer to, I think I need it from what I saw in other conf), etc

Here was my best bet, but still not working :frowning:

sensor:
  - platform: rest
    name: csensor
    resource: http://10.161.0.130:2001/sensors
    json_attributes:
      - bedroom1
      - bedroom2
      - bedroom3
  - platform: template
    sensors:
      temperature:
        value_template: '{{ state.sensor.csensor.attributes["bedroom1"] }}'
1 Like

Alright, first, what you forgot to mention is that in your log you should get an error message saying something like: homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: sensor.csensor. State max length is 255 characters.
The reason for this is that the rest sensor by default just tries to store the whole JSON response as its state which in your case is larger than the allowed 255 characters.
Normally in this type of scenario I pick a simple value from the JSON like for example a timestamp, or the most significant value. To fix your configuration up, I’d suggest you just set a static value for now.
Next, to access the state or attributes of a sensor you start with '{{ states. (missing s in your configuration).
And lastly, you have to add the individual keys to the value template.

Try this:

sensor:
  - platform: rest
    name: csensor
    resource: http://10.161.0.130:2001/sensors
    value_template: 'OK'
    json_attributes:
      - bedroom1
      - bedroom2
      - bedroom3
  - platform: template
    sensors:
      bedroom1_temperature:
        value_template: '{{ states.sensor.csensor.attributes["bedroom1"]["temperature"] }}'
        device_class: temperature
        unit_of_measurement: '°C'
4 Likes

Wooooo thank you soooo much!

I was also confused since the beginning about this value_template that I didn’t understood the need!
This 255 char limit + this stupid s (that I noticed after posting my message :man_facepalming:) + this double dictionary were too many errors simultaneously…

I really thank you for your effort on my config!

Honestly, I was surprised that it was so hard to find nice documentation about something I thought was common, do you know how I can improve that? propose an additional exemple in the official documentation or something like that?

1 Like

Contributions to the documentation are always welcome. Have a look here as a starting point:
Github repository: GitHub - home-assistant/home-assistant.io: 📘 Home Assistant User documentation
Developer info: https://developers.home-assistant.io/docs/en/documentation_index.html

Great :+1:
I’m proposing an additional exemple to this page: RESTful Sensor

It’s where I always landed during my researches.

Thanks again :man_mechanic:

Hello,
May be my question is stupid, but I need some help. I have a temperature and humidity sensor , working as a webserver in my home network.
With rest call I can read temperature or humidity, but I don’t know how can I read both.
Currently, I’m using the following code into config.yaml and I have the temperature.
sensor:

My question is how to also have the humidity. My sensor can be found at http://ovidiuraluca.asuscomm.com

Many thanks