How to combine multiple RESTful Sensors in one?

Hello,

In order to decrease the amount of http request from HomeAssistant to my device, I’m looking for a solution in order to combine my RESTful Sensors declared in my configuration.yaml in one.

As you can see below, I have 2 independant request from the same ressource.
How to combine them in one ?

Thanks a lot !

  • First ressource :
  - platform: rest
    name: "ecocompteur_data_conso_hp"
    resource: http://192.168.1.92/data.json
    value_template: '{{ (((value|regex_replace("([ :])0+(\\d+),", "\g<1>\g<2>,")|from_json).conso_hp) | float * 1 / 1000) | round(0) }}'
    device_class: energy
    scan_interval: 300 #5min
    unit_of_measurement: "kWh"
    
  - platform: rest
    name: "ecocompteur_data_conso_hc"
    resource: http://192.168.1.92/data.json
    value_template: '{{ (((value|regex_replace("([ :])0+(\\d+),", "\g<1>\g<2>,")|from_json).conso_hc) | float * 1 / 1000) | round(0) }}'
    device_class: energy
    scan_interval: 300 #5min
    unit_of_measurement: "kWh"
  • Second ressource
  - platform: rest
    name: "ecocompteur_inst_data1"
    resource: http://192.168.1.92/inst.json
    value_template: '{{ value_json.data1 | round(0) }}'
    device_class: power
    scan_interval: 5
    unit_of_measurement: "W"
    
  - platform: rest
    name: "ecocompteur_inst_data2"
    resource: http://192.168.1.92/inst.json
    value_template: '{{ value_json.data2 | round(0) }}'
    device_class: power
    scan_interval: 5
    unit_of_measurement: "W"
    
  - platform: rest
    name: "ecocompteur_inst_data3"
    resource: http://192.168.1.92/inst.json
    value_template: '{{ value_json.data3 | round(0) }}'
    device_class: power
    scan_interval: 5
    unit_of_measurement: "W"
    
  - platform: rest
    name: "ecocompteur_inst_data4"
    resource: http://192.168.1.92/inst.json
    value_template: '{{ value_json.data4 | round(0) }}'
    device_class: power
    scan_interval: 5
    unit_of_measurement: "W"
    
  - platform: rest
    name: "ecocompteur_inst_data5"
    resource: http://192.168.1.92/inst.json
    value_template: '{{ value_json.data5 | round(0) }}'
    device_class: power
    scan_interval: 5
    unit_of_measurement: "W"
    
  - platform: rest
    name: "ecocompteur_inst_data6m3"
    resource: http://192.168.1.92/inst.json
    value_template: '{{ ((value_json.data6m3 ) | float * 1 + 283.993 ) | round(3) }}'
    scan_interval: 5
    unit_of_measurement: "m3"
    
  - platform: rest
    name: "ecocompteur_inst_data7m3"
    resource: http://192.168.1.92/inst.json
    value_template: '{{ ((value_json.data7m3 ) | float * 1 + 134.391) | round(3) }}'
    scan_interval: 5
    unit_of_measurement: "m3"

That is literally the first example in the documentation.

Yes I already read it but not sure to do the good thing.
Does that works ?

rest:
  - resource: http://192.168.1.92/data.json
    scan_interval: 300 #5min
    sensor:
      - name: "ecocompteur_data_conso_hp"
        value_template: '{{ (((value|regex_replace("([ :])0+(\\d+),", "\g<1>\g<2>,")|from_json).conso_hp) | float * 1 / 1000) | round(0) }}'
        device_class: energy
        unit_of_measurement: "kWh"
      - name: "ecocompteur_data_conso_hc"
        value_template: '{{ (((value|regex_replace("([ :])0+(\\d+),", "\g<1>\g<2>,")|from_json).conso_hc) | float * 1 / 1000) | round(0) }}'
        device_class: energy
        unit_of_measurement: "kWh"

Looks good.

1 Like

Thanks to @tom_l , I solved it !

Below my final code :

rest:
  # LEGRAND EcoCompteur
  - resource: http://192.168.1.92/data.json
    scan_interval: 300 #5min
    sensor:
      - name: "ecocompteur_data_conso_hp"
        value_template: '{{ (((value|regex_replace("([ :])0+(\\d+),", "\g<1>\g<2>,")|from_json).conso_hp) | float * 1 / 1000) | round(0) }}'
        device_class: energy
        unit_of_measurement: "kWh"
      - name: "ecocompteur_data_conso_hc"
        value_template: '{{ (((value|regex_replace("([ :])0+(\\d+),", "\g<1>\g<2>,")|from_json).conso_hc) | float * 1 / 1000) | round(0) }}'
        device_class: energy
        unit_of_measurement: "kWh"

  # LEGRAND EcoCompteur
  - resource: http://192.168.1.92/inst.json
    scan_interval: 5 #5s
    sensor:
      - name: "ecocompteur_inst_data1"
        value_template: '{{ value_json.data1 | round(0) }}'
        device_class: power
        unit_of_measurement: "W"
        
      - name: "ecocompteur_inst_data2"
        value_template: '{{ value_json.data2 | round(0) }}'
        device_class: power
        unit_of_measurement: "W"
        
      - name: "ecocompteur_inst_data3"
        value_template: '{{ value_json.data3 | round(0) }}'
        device_class: power
        unit_of_measurement: "W"
        
      - name: "ecocompteur_inst_data4"
        value_template: '{{ value_json.data4 | round(0) }}'
        device_class: power
        unit_of_measurement: "W"
        
      - name: "ecocompteur_inst_data5"
        value_template: '{{ value_json.data5 | round(0) }}'
        device_class: power
        unit_of_measurement: "W"
        
      - name: "ecocompteur_inst_data6m3"
        value_template: '{{ ((value_json.data6m3 ) | float * 1 + 283.993 ) | round(3) }}'
        device_class: power
        unit_of_measurement: "m3"
        
      - name: "ecocompteur_inst_data7m3"
        value_template: '{{ ((value_json.data7m3 ) | float * 1 + 134.391) | round(3) }}'
        device_class: power
        unit_of_measurement: "m3"
1 Like