Home Assistant REST Sensor Only Shows One Attribute from JSON Response

Hello everyone,

my very first post - please be gentle. I had a little support from ChatGPT in gathering the relevant information :slight_smile:

I’m having an issue with my Home Assistant setup where a REST sensor only shows one attribute (Attribute1) from the JSON response, even though the response contains multiple attributes. Here are the details. For those of you interested, I want to integrate emlog (power meter reader) into Home Assistant.

Issue Description The REST sensor (sensor.api_daily_data) is supposed to fetch data from the API and provide multiple attributes (Attribute1, Attribute2, Attribute3, Attribute4, Attribute5, Attribute6). However, in the Home Assistant UI, only Attribute1 is being displayed. All other attributes show as 0.

One of the problems is that the URL needs to change daily, as the server requires the date to provide the correct current set of data. Here is an example of the URL:
http://XXX.XXX.XXX.XXX/pages/getinformation.php?heute=20240802&meterindex=1

After a lot of back and forth, I concluded that my configuration.yaml did not support dynamic URLs. Therefore, I used a static dummy URL and periodically overwrote it via an automation. Unfortunately, I am still left with only the first value in the response.

Setup Details

  • Home Assistant Version: 2024.7.4
  • Installation Type: Non-container (directly on the server)
  • Architecture: amd64
  • Machine: qemux86-64
  • IP Address: XXX.XXX.XXX.XXX
  • Port: 8123
  • Image: Package qemux86-64-homeassistant · GitHub
  • Boot: true
  • Watchdog: true
  • SS1: false
  • Audio Input: null
  • Audio Output: null
  • Backups Exclude Database: false
  • Update Available: false
  • Latest Version: 2024.7.4
  • Configuration Files
  • configuration.yaml

configuration.yaml:

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Include automation, script, and scene files
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

# Example charger configuration
charger:
  chargers:
    - name: charger1
      host: xxx.xxx.xxx.xxx

# Logger configuration for debugging
logger:
  default: info
  logs:
    homeassistant.components.rest: debug

# Template sensors for extracting attributes from the REST sensor
template:
  - sensor:
      # Sensor to generate the API URL
      - name: "Generated API URL"
        state: >
          {% set date = now().strftime('%Y%m%d') %}
          http://xxx.xxx.xxx.xxx/path/to/api?date={{ date }}&param=1
      # Sensor for the current date
      - name: "Current Date"
        state: "{{ now().strftime('%Y%m%d') }}"
        unit_of_measurement: ""
      # Sensor for Attribute1
      - name: "API Attribute1"
        state: "{{ state_attr('sensor.api_daily_data', 'Attribute1') }}"
        unit_of_measurement: "unit"
      # Sensor for Attribute2
      - name: "API Attribute2"
        state: "{{ state_attr('sensor.api_daily_data', 'Attribute2') }}"
        unit_of_measurement: "unit"
      # Sensor for Attribute3
      - name: "API Attribute3"
        state: "{{ state_attr('sensor.api_daily_data', 'Attribute3') }}"
        unit_of_measurement: "unit"
      # Sensor for Attribute4
      - name: "API Attribute4"
        state: "{{ state_attr('sensor.api_daily_data', 'Attribute4') }}"
        unit_of_measurement: "unit"
      # Sensor for Attribute5
      - name: "API Attribute5"
        state: "{{ state_attr('sensor.api_daily_data', 'Attribute5') }}"
        unit_of_measurement: "unit"
      # Sensor for Attribute6
      - name: "API Attribute6"
        state: "{{ state_attr('sensor.api_daily_data', 'Attribute6') }}"
        unit_of_measurement: "unit"

# REST sensor with a static URL and dynamic parameters
sensor:
  - platform: rest
    name: "API Daily Data"
    resource: "http://xxx.xxx.xxx.xxx/path/to/api"
    method: GET
    scan_interval: 15
    headers:
      Content-Type: application/json
    params:
      date: "{{ states('sensor.current_date') }}"
      param: 1
    value_template: "{{ value_json }}"
    json_attributes:
      - Attribute1
      - Attribute2
      - Attribute3
      - Attribute4
      - Attribute5
      - Attribute6

automation.yaml

# automations.yaml

# Automation to update the API URL every 15 minutes
- alias: Update API URL
  trigger:
    - platform: time_pattern
      minutes: "/15"
  action:
    - service: homeassistant.update_entity
      entity_id: sensor.api_daily_data
  mode: single

JSON Response Example
Here is a typical JSON response from the API:

{
  "Attribute1": -1788,
  "Attribute2": 5.9,
  "Attribute3": 0.3,
  "Attribute4": 1250.9183
  "Attribute5": 1350.2757,
  "Attribute6": 5.07229999999981
}

Troubleshooting Steps Taken

  1. Verified the REST sensor configuration in configuration.yaml.
  2. Manually tested the URL in a browser, and the JSON response is correct.
  3. Checked Home Assistant logs for any errors related to the REST sensor.
  4. Verified the automation to update the sensor every 15 minutes.
  5. Ensured the json_attributes list includes all required attributes.

Has anyone faced a similar issue where only one attribute from the JSON response is being recognized by Home Assistant? Any suggestions on what might be causing this and how to resolve it would be greatly appreciated.

Thank you!

The problem with chatgpt is that is gives you made up content very confidently.

Check your syntax with what the document shows. HA has great documentation. I just built something using a json sensor. Compare your syntax with mine.

Here is my post:

You can also check logs and also the state of information pulled. To go developer tools then select the states tab, then look up your sensor and see what is being pulled.

Edit: make sure to post your working config when you figure it out!

thanks for hinting your JSON weather project. I did update my JSON based on your expample. It did “compile” but still exactly the same issue with just the first value being pulled.