Problem with a REST sensor (not technical but knowledge)

Dear all,

I would like to integrate my new charging station for my car into HA as a sensor.
The charger has a REST api and when I curl the status I get the following output (truncated)

{"success":true,"age":132,
"data":{"version":"B","tme":"0311190734","rbc":"8","rbt":"395977232","car":"4",
"amp":"32","err":"0","ast":"0","alw":"1","stp":"0","cbl":"32","pha":"56","tmp":"19",
"dws":"1944425","dwo":"0","adi":"0","uby":"0","eto":"1020","wst":"3","txi":"1", ....}

I’ve got that in my configuration.yaml

sensor:   
  - platform: rest
    resource: https://api.go-e.co/api_status?token=some_token&wait=0
    name: "Ladestation"
    scan_interval:
      minutes: 2
    value_template: '{{ value_json["data"] }}'
    json_attributes:
      - data
  - platform: template
    sensors:
      car_state:
        friendly_name: 'Zustand Ladestation'
        value_template: "{{ state_attr('sensor.Ladestation', 'data').car }}"
      charging_power:
        friendly_name: 'Ladestrom'
        value_template: "{{ state_attr('sensor.Ladestation', 'amp') }}"
      unit_temp:
        friendly_name: 'Temperatur der Ladestation'
        value_template: "{{ state_attr('sensor.Ladestation', 'data').tmp }}"
      error_code:
        friendly_name: 'Fehlermeldung Ladestation'
        value_template: "{{ state_attr('sensor.Ladestation', 'data').err }}"
       total_energy:
         friendly_name: 'Gesamte geladene Energiemenge'
         value_template: "{{ state_attr('sensor.Ladestation', 'data').eto }}"

With that I get no output for car_state: sensor (blank) and “None” for the charging_power: sensor.

The others will go the same way, so I just focus on these two now.

So here is what I really would like to have (but I don’t understand yet fully, if this is possible).

One sensor with multiple readings, one for car_state, one for charging_power, one for …

When I do one sensor with just one data field (e.g. amp) I already got the right value. Unfortunately I did not save that code for reference :frowning:

I’ve read several posts now and I’m trying for 2 days to get it working. I know I just have it close to my fingertips but maybe someone more experienced like me will solve this in seconds and then I will just cry…

Help is highly appreciated.

Thanks upfront
RalfP

Correct code here:

  - platform: rest
    resource: https://api.go-e.co/api_status?token=some_token&wait=0
    name: "ladestation"
    scan_interval:
      minutes: 30
    value_template: '{{ value_json["success"] }}'
    json_attributes:
      - success
      - data
  - platform: template
    sensors:
      request_state:
        value_template: "{{ state_attr('sensor.ladestation', 'success') }}"
        attribute_templates:
          charging_state: >-
            {{ state_attr('sensor.ladestation', 'data').car }}
          unit_temp: >-
            {{ state_attr('sensor.ladestation', 'data').tmp }}
          charging_power: >-
            {{ state_attr('sensor.ladestation', 'data').amp }}
          error_code: >-
            {{ state_attr('sensor.ladestation', 'data').err }}
          total_energy: >-
            {{ state_attr('sensor.ladestation', 'data').eto }}

Are you getting errors in your logs?

Bummer!

homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: sensor.ladestation. State max length is 255 characters.

The received data exceeds the maximum string length supported by the entity’s state (255 characters).

Attributes don’t have a 255-character limit. You are already using json_attributes to receive all of data so just change your value_template to extract success or age.

    value_template: '{{ value_json.success }}'

Thanks Taras,

now I get the values for the individual sensors.

What do I need to change now to get one sensor with multiple values?

Will check out the documentation but help is always appreciated.

I would keep the rest sensor as is now, i.e. keep the data in json_attributes, and also keep the template sensors.
The value template would need to change to something like this:

value_template: "{{ state_attr('sensor.Ladestation', 'data')['car'] }}"

So far I got here:

sensor:
  - platform: rest
    resource: https://api.go-e.co/api_status?token=some_token&wait=0
    name: "Ladestation"
    scan_interval:
      minutes: 30
    value_template: '{{ value_json["success"] }}'
    json_attributes:
      - success
      - data
  - platform: template
    sensors:
      request_state:
      value_template: "{{ state_attr('sensor.Ladestation', 'success') }}"
        attribute_templates:
          charging_state: >-
            {{ state_attr('sensor.Ladestation', 'data').car }}
          unit_temp: >-
            {{ state_attr('sensor.Ladestation', 'data').tmp }}
          charging_power: >-
            {{ state_attr('sensor.Ladestation', 'data').amp }}
          error_code: >-
            {{ state_attr('sensor.Ladestation', 'data').err }}
          total_energy: >-
            {{ state_attr('sensor.Ladestation', 'data').eto }}

But with that I get an error when checking the config:

ERROR:homeassistant.util.yaml.loader:while parsing a block mapping
  in "/config/configuration.yaml", line 84, column 7
expected <block end>, but found '<block mapping start>'
  in "/config/configuration.yaml", line 86, column 9

Line 84 is the one with request state:

Must be indentation but I just don’t get it.

Move value_template to the right by two spaces. It should be aligned vertically with attribute_templates below it.

In addition, change:

sensor.Ladestation

to:

sensor.ladestation

All entity names are lower-case.

That did the trick.

Thanks to Taras!

So for all interested see the full sensor code below.

  - platform: rest
    resource: https://api.go-e.co/api_status?token=some_token&wait=0
    name: "ladestation"
    scan_interval:
      minutes: 30
    value_template: '{{ value_json["success"] }}'
    json_attributes:
      - success
      - data
  - platform: template
    sensors:
      request_state:
        value_template: "{{ state_attr('sensor.ladestation', 'success') }}"
        attribute_templates:
          charging_state: >-
            {{ state_attr('sensor.ladestation', 'data').car }}
          unit_temp: >-
            {{ state_attr('sensor.ladestation', 'data').tmp }}
          charging_power: >-
            {{ state_attr('sensor.ladestation', 'data').amp }}
          error_code: >-
            {{ state_attr('sensor.ladestation', 'data').err }}
          total_energy: >-
            {{ state_attr('sensor.ladestation', 'data').eto }}

Further improvements will follow, e.g. change the output of the attribute car according to the documentation, add the right units, …

1 Like