Difference between 2 configs

hi,

I have recently started with Home Assistant, and look for some advice on my config.

I have an (old) Rpi with serial connection to a P1 interface, and I have made the data accessible via Nginx to use the ‘Rest’ interface:

$ curl -s http://192.168.1.9/ha/p1_file.json | jq
{
  "p1": {
    "gas": 5463.987,
    "low": 6451478,
    "high": 6170272,
    "ret_low": 842.126,
    "ret_high": 2234.51
  }
}

I would like to only query the API Server once, but I am unable to get the gathered data selectable as energy sensors in the Energy dashboard. The sensors do show up, and gather historical data (all good), but they are not presented in the Energy dashboard selection menu.

See below my 2 configs, whereby the 1st one works OK in HA (can select in Energy dashboard), but it requires more API call:


sensor:
  - platform: rest
    resource: http://192.168.1.9/ha/p1_file.json
    name:  low
    value_template: '{{ value_json.p1.low }}'
    device_class: energy
    state_class: total_increasing
    unit_of_measurement: "Wh"
    scan_interval: 60

  - platform: rest
    resource: http://192.168.1.9/ha/p1_file.json
    name: high
    value_template: '{{ value_json.p1.high }}'
    device_class: energy
    state_class: total_increasing
    unit_of_measurement: "Wh"
    scan_interval: 60

  - platform: rest
    resource: http://192.168.1.9/ha/p1_file.json
    name: ret_low
    value_template: '{{ value_json.p1.ret_low }}'
    device_class: energy
    state_class: total_increasing
    unit_of_measurement: "Wh"
    scan_interval: 60

  - platform: rest
    resource: http://192.168.1.9/ha/p1_file.json
    name: ret_high
    value_template: '{{ value_json.p1.ret_high }}'
    device_class: energy
    state_class: total_increasing
    unit_of_measurement: "Wh"
    scan_interval: 60

  - platform: rest
    resource: http://192.168.1.9/ha/p1_file.json
    name: gas
    value_template: '{{ value_json.p1.gas }}'
    device_class: gas
    state_class: total_increasing
    unit_of_measurement: "m³"
    scan_interval: 60

This is my more ‘smart’ config, but with this in place, I cannot select the sensors:

sensor:
  - platform: rest
    name: p1
    resource: http://192.168.1.9/ha/p1_file.json
    scan_interval: 60
    force_update: true
    json_attributes:
      - p1
    value_template: "OK"
    state_class: total_increasing
  - platform: template
    sensors:
      p1_high:
        value_template: "{{ state_attr('sensor.p1', 'p1')['high'] }}"
        device_class: energy
        unit_of_measurement: "KWh"
      p1_low:
        value_template: "{{ state_attr('sensor.p1', 'p1')['low'] }}"
        device_class: energy
        unit_of_measurement: "KWh"
      p1_gas:
        value_template: "{{ state_attr('sensor.p1', 'p1')['gas'] }}"
        device_class: gas
        unit_of_measurement: "m³"
      p1_ret_high:
        value_template: "{{ state_attr('sensor.p1', 'p1')['ret_high'] }}"
        device_class: energy
        unit_of_measurement: "KWh"
      p1_ret_low:
        value_template: "{{ state_attr('sensor.p1', 'p1')['ret_low'] }}"
        device_class: energy
        unit_of_measurement: "KWh"

I must be overlooking something silly?

Help is greatly appreciated!

Not sure why people keep using the old legacy rest sensor platform. The new rest integration is much better. It will grab all your sensors with one resource call. Try this:

rest:
  - resource: http://192.168.1.9/ha/p1_file.json
    scan_interval: 60
    sensor:
      - name: low
        value_template: '{{ value_json.p1.low }}'
        device_class: energy
        state_class: total_increasing
        unit_of_measurement: "Wh"
      - name: high
        value_template: '{{ value_json.p1.high }}'
        device_class: energy
        state_class: total_increasing
        unit_of_measurement: "Wh"
      - name: ret_low
        value_template: '{{ value_json.p1.ret_low }}'
        device_class: energy
        state_class: total_increasing
        unit_of_measurement: "Wh"
        scan_interval: 60

Hi Tom,

Many thanks for the response.
I think it is working now with your example. ( the last line scan_interval is not allowed, and is most probably a copy/past error )

Why I used the old/legacy platform is a good question. I think searching the internet for examples, and misinterpreting the documentation is the culprit here.

1 Like

It was indeed. Sorry.

Home assistant moves pretty rapidly and there is a lot of outdated information not only in blogs and on youtube but here in the forum as well. Once you find what you think is the answer always check the official documentation.

Excellent. Once you confirm it, please mark the post as the solution.

Hi @tom_l ,

Do you know if something changed with respect to this method?

I am recently getting ‘UndefinedError: ‘value_json’ is undefined’ with my config which worked great for the last months (since the above issue was resolved).

I did cleanup the sensors a bit in the past.

The API delivers the same content:

$ curl http://192.168.1.9/ha/dht11.json -s|jq
{
  "temperature": 22,
  "humidity": 45
}

And the config in a separate ‘rest’ config file:

- resource: http://192.168.1.9/ha/dht11.json
  scan_interval: 60
  sensor:
    - name: meterkast_temperature
      value_template:  '{{ value_json.temperature }}'
      device_class: temperature
      unit_of_measurement: "°C"
    - name: meterkast_humidity
      value_template:  '{{ value_json.humidity }}'
      device_class: humidity
      unit_of_measurement: "%"

The error in the docker logs:

2022-06-30 21:54:38 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'value_json' is undefined when rendering '{{ value_json.temperature }}'
2022-06-30 21:54:38 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'value_json' is undefined when rendering '{{ value_json.humidity }}'

I have the feeling my config is still correct? Is this a bug?
I’m running ‘image: "ghcr.io/home-assistant/home-assistant:2022.6.1

Try it like this:

  sensor:
    - name: meterkast_temperature
      value_template:  >
        {% set temperature = value_json.temperature %}
        {% if temperature|is_number %}
          {{ temperature }}
        {% else %}
           unknown
        {% endif %}
      device_class: temperature
      unit_of_measurement: "°C"
    - name: meterkast_humidity
      value_template: >
        {% set humidity = value_json.humidity %}
        {% if humidity|is_number %}
          {{ humidity }}
        {% else %}
           unknown
        {% endif %}
      device_class: humidity
      unit_of_measurement: "%"

hi @tom_l , that does work again indeed. Looking at this config it is at least improving the import values.