Trying to get a complex RESTful sensor and binary_sensor working

This is what i get:

{
  "TFmini-S": {
    "name": "TFmini-S",
    "detector": "LiDAR",
    "dist": 5,
    "flux": 412,
    "temp": 52,
    "sig": 253,
    "unit": {
      "temp": "°C",
      "dist": "cm",
      "flux": "",
      "sig": ""
    }
  },
  "HC-SR04": {
    "name": "HC-SR04",
    "detector": "ultrasonic",
    "dist": 9.010000229,
    "unit": {
      "dist": "cm"
    }
  },
  "timestamp": "18.05.2022 18:40:38",
  "dload": 9.010000229,
  "lload": 311.1871338,
  "pload": 94.2991333,
  "R1": "off",
  "R2": "off",
  "R3": "off",
  "R4": "off",
  "R6": "off",
  "unit": {
    "dload": "cm",
    "lload": "Liter",
    "pload": "%"
  }
}

Here my sensor configuration:

- platform: rest
  name: cisterne
  resource: http://192.168.133.90/cisterne
  json_attributes:
    - TFmini-S
    - HC-SR04
    - timestamp
    - dload
    - lload
    - pload
    - timestamp
  value_template: 'OK'
- platform: template
  sensors:
    tfminis_dist:
      value_template: "{{ state_attr('sensor.cisterne', 'TFmini-S')['dist'] | round(1) }}"
      unit_of_measurement: "cm"
    tfminis_sig:
      value_template: "{{ state_attr('sensor.cisterne', 'TFmini-S')['sig'] }}"
    tfminis_flux:
      value_template: "{{ state_attr('sensor.cisterne', 'TFmini-S')['flux'] }}"
    tfminis_temp:
      value_template: "{{ state_attr('sensor.cisterne', 'TFmini-S')['temp'] }}"
      device_class: temperature
      unit_of_measurement: "°C"
    tfminis_detector:
      value_template: "{{ state_attr('sensor.cisterne', 'TFmini-S')['detector'] }}"
    hcsr04_detector:
      value_template: "{{ state_attr('sensor.cisterne', 'HC-SR04')['detector'] }}"
    hcsr04_dist:
      value_template: "{{ state_attr('sensor.cisterne', 'HC-SR04')['dist'] | round(1) }}"
      unit_of_measurement: "cm"
    cisterne_pload:
      value_template: "{{ state_attr('sensor.cisterne', 'pload') | round(1) }}"
      unit_of_measurement: "%"
    cisterne_dload:
      value_template: "{{ state_attr('sensor.cisterne', 'dload') | round(1) }}"
      unit_of_measurement: "cm"
    cisterne_lload:
      value_template: "{{ state_attr('sensor.cisterne', 'lload') | round(1) }}"
      unit_of_measurement: "Liter"

Here my binary_sensor configuration:

- platform: rest
  name: cisterne_r1
  resource: http://192.168.133.90/cisterne
  value_template: '{{ value_json.R1 }}'
- platform: rest
  name: cisterne_r2
  resource: http://192.168.133.90/cisterne
  value_template: '{{ value_json.R2 }}'
- platform: rest
  name: cisterne_r3
  resource: http://192.168.133.90/cisterne
  value_template: '{{ value_json.R3 }}'
- platform: rest
  name: cisterne_r4
  resource: http://192.168.133.90/cisterne
  value_template: '{{ value_json.R4 }}'
- platform: rest
  name: cisterne_r6
  resource: http://192.168.133.90/cisterne
  value_template: '{{ value_json.R6 }}'

All sensors and binary_sensors are working fine, but to read this sensors and binary_sensors homeassistant needs 6 requests though all information could be read with one request.
Is it possible to get all sensors and binary_sensors with one request?

use the REST integration instead of the REST sensor platform:

1 Like

Would you mind elaborating as your link is for the sensor which you actually state to not (!) use. Question as no expert mysef, willing to learn and maybe missing the point between sensor and integration?

No the link is for the rest integration. Which will ultimately give you sensors based on the integration configuration.

this is different than the rest sensor platform:

EDIT: edited my original post for clarity.

Thank you very much.
The Rest Integration is doing the job. Only one request and all sensors and binary_sensors are updated in one pass.
Very happy :+1:

1 Like

Thanks I was thrown off by the first sentences which speak of sensor. What I see as a real benefit (for me) is indeed shorten the steps and get data in attributes. However, if one needs the data in the state-value then there is not much difference or can one generate state-value sensors directly in one go too?

I believe you can get whatever data you want in the state by using the appropriate code in the “value_template:” section of the sensors you create.

the docs are a little confusing since they don’t really show using an actual template in the “value_template:” in any examples.

But the docs do say this about the “sensor:” section:

sensor list (optional)

A list of RESTful Sensor to create from the shared data. All configuration settings that the supported by RESTful Sensor not listed above can be used here.

and in the linked rest sensor docs the “value_template” is explained better and with examples of using a real template.

Hello All!

My rest sensor is working fine now, but i have problems activating utility meter…

Have a problem with my gasmeter:
image

Energy Dashboard is complaining about wrong device_class:

My configuration:
rest:

- resource: http://192.168.133.91/counter
  scan_interval: 60
  sensor:
    - name: gas counter data
      value_template: "OK"
      json_attributes:
        - gas meter
        - company
        - s/n
        - install date
        - timestamp
        - counter
        - volume
        - millis
  binary_sensor:
    - name: gas delivery
      value_template: '{{ value_json.delivery }}'

template:

- sensor:
    - name: gas meter name 
      state: "{{ state_attr('sensor.gas_counter_data', 'gas meter') }}"
    - name: gas meter company 
      state: "{{ state_attr('sensor.gas_counter_data', 'company') }}"
    - name: gas meter serial number  
      state: "{{ state_attr('sensor.gas_counter_data', 's/n') }}"
    - name: gas meter install date 
      state: "{{ state_attr('sensor.gas_counter_data', 'install date') }}"
    - name: gas counter timestamp 
      state: "{{ state_attr('sensor.gas_counter_data', 'timestamp') }}"
    - name: gas counter 
      state: "{{ state_attr('sensor.gas_counter_data', 'counter') }}"
      state_class: measurement
    - name: gas volume 
      state: "{{ state_attr('sensor.gas_counter_data', 'volume') }}"
      device_class: gas
      unit_of_measurement: "m³"
    - name: gas counter millis 
      state: "{{ state_attr('sensor.gas_counter_data', 'millis') }}"
      state_class: measurement

utility_meter:

gasmeter_yearly:
  source: sensor.gas_volume
  cycle: yearly
gasmeter_monthly:
  source: sensor.gas_volume
  cycle: monthly
gasmeter_weekly:
  source: sensor.gas_volume
  cycle: weekly
gasmeter_daily:
  source: sensor.gas_volume
  cycle: daily

All entyties are there, no other problems but my energy dashboard reports wrong device_class.
Tryed device_class: “gas” and “energy”.
unit_of_measurement: “m³”

Any hints
Marc

With the utility meter I assume you mean to create the statistics (totalizing)
I cannot see what ‘platform’ you are using but with me they go under ‘integration’…gas and m3 should be fine imho