ESPhome and json

I am stuck trying to process a json string in ESPhome. Here the config:

esphome:
  name: esphome-tibber-com
  friendly_name: esphome-tibber-com

esp32:
  board: esp32dev
  framework:
    type: arduino 
    

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "..."

ota:
  - platform: esphome
    password: "..."

# Example configuration entry
wifi:
  ssid: ...
  password: ...

# Example configuration entry
spi:
  clk_pin: GPIO18
  mosi_pin: GPIO23

font:
  - file: "https://github.com/IdreesInc/Monocraft/releases/download/v3.0/Monocraft.ttf"
    id: web_font
    size: 9
      # gfonts://family[@weight]
  - file: "Eight-Bit-Dragon.ttf"
    id: my_font
    size: 8

  - file: "pixelmix.ttf"
    id: pixel_8
    size: 8


# Example configuration entry
sensor:
  - platform: homeassistant
    id: tibber_sensor
    entity_id: sensor.electricity_price_gaddgrand_4
    internal: true
  - platform: homeassistant
    id: display_brightness
    entity_id: sensor.esphome_tibber_brigthness
    internal: true
  - platform: homeassistant
    id: tibber_demand
    entity_id: sensor.develco_zhemi101_instantaneous_demand
    internal: true
  - platform: homeassistant
    id: tesla_usd
    entity_id: sensor.tesla_usd
    internal: true

  
globals:
  - id: page
    type: int
    initial_value: "1"

  - id: next_10_hours_prices
    type: float[10]
    initial_value: '{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}'

  - id: next_10_hours_levels
    type: std::vector<std::string>
    initial_value: '{"", "", "", "", "", "", "", "", "", ""}'

  - id: debug_mode
    type: bool
    initial_value: 'true'


text_sensor:
  - platform: homeassistant
    id: tibber_prices
    entity_id: sensor.tibber_electricity_price
    attribute: current




display:
  - platform: max7219digit
    cs_pin: GPIO5
    num_chips: 4
    intensity: 0
    lambda: |-
      switch (id(page)){
        case 1:
          // Print the current time
          it.printf(0, 0, id(pixel_8), "%.2f", id(tibber_sensor).state);
          it.line(24, 0, 24, 7);
          it.line(25, 4, 25, 7);
          it.line(26, 5, 26, 7);
          it.line(27, 6, 27, 7);
          it.line(28, 2, 28, 7);
          it.line(29, 1, 29, 7);
          it.line(30, 1, 30, 7);
          break;
        case 2: 
          it.printf(0, 0, id(pixel_8), "%.0fW", id(tibber_demand).state);
          break;
        case 3: 
          it.printf(0, 0, id(pixel_8), "%.2f", id(tesla_usd).state);
          break;
      }
      auto json_str = id(tibber_prices).state;
      auto root = json::parse(json_str);


      



interval:
- interval: 5s
  then:
    - lambda: |-
        id(page) = (id(page) + 1);
        if (id(page) > 3) {
          id(page) = 1;
        }

When compiling i get following error:

INFO ESPHome 2024.12.4
INFO Reading configuration /config/esphome/esphome-tibber-com.yaml...
WARNING GPIO5 is a strapping PIN and should only be used for I/O with care.
Attaching external pullup/down resistors to strapping pins can cause unexpected failures.
See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
INFO Generating C++ source...
INFO Compiling app...
Processing esphome-tibber-com (board: esp32dev; framework: arduino; platform: platformio/[email protected])
--------------------------------------------------------------------------------
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch5
Dependency Graph
|-- WiFi @ 2.0.0
|-- ESPmDNS @ 2.0.0
|-- Update @ 2.0.0
|-- noise-c @ 0.1.6
|-- SPI @ 2.0.0
Compiling .pioenvs/esphome-tibber-com/src/main.cpp.o
Compiling .pioenvs/esphome-tibber-com/libbf6/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-ssse3.c.o
Compiling .pioenvs/esphome-tibber-com/libbf6/libsodium/crypto_generichash/blake2b/ref/blake2b-ref.c.o
Compiling .pioenvs/esphome-tibber-com/libbf6/libsodium/crypto_generichash/blake2b/ref/generichash_blake2b.c.o
Compiling .pioenvs/esphome-tibber-com/libbf6/libsodium/crypto_hash/crypto_hash.c.o
Compiling .pioenvs/esphome-tibber-com/libbf6/libsodium/crypto_hash/sha256/cp/hash_sha256_cp.c.o
Compiling .pioenvs/esphome-tibber-com/libbf6/libsodium/crypto_hash/sha256/hash_sha256.c.o
Compiling .pioenvs/esphome-tibber-com/libbf6/libsodium/crypto_hash/sha512/cp/hash_sha512_cp.c.o
Compiling .pioenvs/esphome-tibber-com/libbf6/libsodium/crypto_hash/sha512/hash_sha512.c.o
/config/esphome/esphome-tibber-com.yaml: In lambda function:
/config/esphome/esphome-tibber-com.yaml:120:19: error: 'json' has not been declared
       auto root = json::parse(json_str);
                   ^~~~
Compiling .pioenvs/esphome-tibber-com/libbf6/libsodium/crypto_onetimeauth/crypto_onetimeauth.c.o
Compiling .pioenvs/esphome-tibber-com/libbf6/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna.c.o
Compiling .pioenvs/esphome-tibber-com/libbf6/libsodium/crypto_onetimeauth/poly1305/onetimeauth_poly1305.c.o
Compiling .pioenvs/esphome-tibber-com/libbf6/libsodium/crypto_pwhash/argon2/argon2-core.c.o
*** [.pioenvs/esphome-tibber-com/src/main.cpp.o] Error 1

Seems like json::parse_json can’t be found.
What am I doing wrong?

Frank

I think you need to add the http_request: component to force ESPHome to include the JSON library.

Don’t quote me though, just guessing. The docs for JSON in ESPHome are useless.

If you want to use json, you need to configure it, otherwise it won’t be included, unless some component auto-loads it.

json:

Is this just a guess, or do you have a link? The json component docs don’t have an example and a search for json: just gives results without the colon…. Normally my search skills are pretty good. :blush:

I didn’t say it was documented, just gave you some advice, which of course you are free to ignore, but you could always just try it.

Thanks for the support here, it solved the problem.

json:

in the yaml file triggers to load the json libary. Amamzing that it does not mention it in the documentation. As sensors are potentially imported from HA, there is then no need to use any http request in ESPhome and no json is loaded, unless you specify the directive above.