UndefinedError when parsing sensor json

Hi,

I am new to HASS and I need to parse some JSON from an endpoint. I spend the last 5 hours trying multiple things but without luck… :frowning:

Problem is that I always obtain: UndefinedError: ‘tesla_estado’ is undefined

Remote JSON it’s pretty simple:

{  
   "state":"online",
   "usable_battery_level":"67"
}

And in configuration.yaml I have this:

sensor:

  - platform: rest
    json_attributes:
      - state
      - usable_battery_level
    resource: https://www.teslafi.com/feed.php?token=SOME-TOKEN
    scan_interval: 60

  - platform: template
    sensors:
      tesla_estado:
        friendly_name: "Estado"
        entity_id: sensor.tesla_estado
        value_template: "{{tesla_estado.state}}"

      tesla_bateria:
        friendly_name: "Nivel de batería"
        entity_id: sensor.tesla_bateria
        value_template: "{{tesla_bateria.usable_battery_level}}"
        unit_of_measurement: "%"

Any help would be much appreciated! Thank you very much

If you look at the states table in dev-tools what do you see under rest_sensor?

Hi,

I finally setup and parse JSON in different way, but I need to do one external query for each sensor, and couldn’t find the right way to process the json_attributes.

My config:

External JSON:

{  
   "state":"online",
   "battery_level":"66"
   "inside_temp":"18.2",
   "outside_temp":"6.0",
   "longitude":"-3.624717",
   "latitude":"40.448048",
}

configuration.yaml:

  - platform: rest
    name: Estado Model X
    resource: https://www.teslafi.com/feed.php?token=SOME-TOKEN
    value_template: '{{ value_json.state }}'
    scan_interval: 60
  - platform: template
    sensors:
      longitude:
        value_template: '{{ states.sensor.state.attributes.longitude }}'
        friendly_name: 'Longitud'
      latitude:
        value_template: '{{ states.sensor.state.attributes.latitude }}'
        friendly_name: 'Latitud'

  - platform: rest
    name: Nivel de batería Model X
    resource: https://www.teslafi.com/feed.php?token=SOME-TOKEN
    value_template: '{{ value_json.battery_level }}'
    unit_of_measurement: "%"
    scan_interval: 60

  - platform: rest
    name: Temperatura interior Model X
    resource: https://www.teslafi.com/feed.php?token=SOME-TOKEN
    value_template: '{{ value_json.inside_temp }}'
    unit_of_measurement: "ºC"
    scan_interval: 60

  - platform: rest
    name: Temperatura exterior Model X
    resource: https://www.teslafi.com/feed.php?token=SOME-TOKEN
    value_template: '{{ value_json.outside_temp }}'
    unit_of_measurement: "ºC"
    scan_interval: 60

I can see all the data in dev-tools except for the json-attributes: latitude and longitude. I see them as ‘unknow’ and in the log I see this:

2017-12-18 11:13:34 WARNING (MainThread) [homeassistant.components.sensor.template] Could not render template Latitud, the state is unknown.
2017-12-18 11:13:35 WARNING (MainThread) [homeassistant.components.sensor.template] Could not render template Longitud, the state is unknown.

And in my dev-tools I see this:

My plans are to use longitude / latitude in a map, not as a sensor, so my questions are:

  • Is it possible to process the incoming JSON feed with 1 external call to the API instead of 1 call for each sensor?
  • Could you please point me in the right direction to ingest and process those Lat/Long data to reuse in a map?

Thank you so much!

I can’t help with the last 2 questions :frowning: but I can where you are going wrong on the lat/long, they have to be decoded from the json the same way the rest of the values (for battery, temp, state etc) were retrieved value_template: ‘{{ value_json.longitude }}’, they are not attributes they are just part of the json message.

To reduse calls to teslafi. I do the following

crontab:
*/1 * * * * curl -s https://www.teslafi.com/feed.php?token=SOME-TOKEN > /home/thomas/.homeassistant/teslafi.json

sensors:

  • platform: file
    name: Tesla inne temperatur
    file_path: /home/thomas/.homeassistant/teslafi.json
    value_template: ‘{{ value_json.inside_temp }}’
    unit_of_measurement: ‘°C’