Help with esphome and strange yaml error

I’m new to ESPhome and Home Assistant. I am trying to make a weather station by replaceing the electronics of a cheap broken Sencor station. I wanted to add a display to the project to show some of the sensor data on the unit itself.

I’m using a Wemod D1-mini, a BME280 temperature, humidity and pressure sensor, an AS5600 magnetic sensor to get wind direction, and the original reed switches to get wind speed, and rainfall readings via pulse counters.

I also added a small OLED to display the sensor data, wich is wehere I encountered the problem. At first it was working somewhat. I gave id-s to the sensor data, and used

“it.printf(0, 0, id(my_font), “Temperature: %.1f°C, Humidity: %.1f%%”, id(temperature).state, id(humidity).state);”

to print the data to the display. It was working, but the data was overflowing the display, and I could not find sample code to display it in multiple lines. I duplicated the above line and modified the y position to 20. Now I had two lines owerflowing the display. I modified the first line to only display the temperature and the secound to display only the humidity. Then the first error appeared and ever since I can’t compile the yaml file. Even after reverting back to the original code wich was working before.

"INFO ESPHome 2024.8.3
INFO Reading configuration /config/esphome/weather-station.yaml…
ERROR Error while reading config: Invalid YAML syntax:

while scanning for the next token
found character ‘%’ that cannot start any token
in “/config/esphome/weather-station.yaml”, line 81, column 48

My config:

esphome:
  name: weather-station
  friendly_name: Weather Station

esp8266:
  board: d1_mini

i2c:
  sda: D2
  scl: D1
  scan: true
  id: bus_a
  frequency: 400kHz

font:
  - file: "Roboto-Regular.ttf"
    id: my_font
    size: 12

as5600:
  dir_pin: D5
  start_position: 0deg
  range: 180deg
  
# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "9DeZgDupsiTXs3VnY5EpFo6xRgN7GlcEl0I0uCSjx/Y="

ota:
  - platform: esphome
    password: "449cfc6710e8f26524c5d0434d9f8b98"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Weather-Station Fallback Hotspot"
    password: "VUpknRO75Y6X"

captive_portal:

sensor:
  - platform: bme280_i2c
    address: 0x76
    temperature:
      name: "BME280 Temperature"
      id: temperature
    pressure:
      name: "BME280 Pressure"
      id: pressure
    humidity:
      name: "BME280 Humidity"
      id: humidity
    update_interval: 10.0s

  - platform: as5600
    name: Position
    raw_position:
        name: Raw Position
    gain:
        name: Gain
    magnitude:
        name: Magnitude
    status:
        name: Status
    update_interval: 10.0s

display:
  - platform: ssd1306_i2c
    model: "SH1106 128x64"
    reset_pin: D0
    address: 0x3C
    update_interval: 0.25s
    lambda: |-
    it.printf(0, 0, id(my_font), "Temperature: %.1f°C, Humidity: %.1f%%", id(temperature).state, id(humidity).state);

You have invalid YAML code, as it’s stating in the message.

But, without full context (the code), it’s hard to help you.

With that said, you’re not escaping the " in your code. Maybe that you’r issue.

“it.printf(0, 0, id(my_font), → Temperature: %.1f°C, Humidity: %.1f%%<-, id(temperature).state, id(humidity).state);”

Try and mix " and ', or escape your code. Try and give a look at |- too.

You maybe want to take a look at this:

I don’t think there’s anything strange with the YAML error, as it’s simply stating that you have a syntax error :smiley: .

I can see you’re editing the post as we speek.

Format your code like this:

image

Resulting in this:

oh_boy:
  formatted_code:
    is_the_best:
     - hi
     - ok

Updated the code… Still learning the ropes of the forum…

2 Likes

That’s great! I fully understand, the Discourse forum-platform can do some great things, but we all need to learn it first :slight_smile: .

I tried ask chatGPT to confirm my own thoughts in the first answer. As I’m not sure if the HA community likes that kind of answer, or not, here’s just the screenshot:

Fixed YAML code:

display:
  - platform: ssd1306_i2c
    model: "SH1106 128x64"
    reset_pin: D0
    address: 0x3C
    update_interval: 0.25s
    lambda: |-
      it.printf(0, 0, id(my_font), "Temperature: %.1f°C, Humidity: %.1f%%", id(temperature).state, id(humidity).state);

You’ll need to push in the “it.printf” code :slight_smile:

Just for a future recommendation: Once possible, always share your full code, as you’re giving us the actual scope of the code, not just the thrown error. Else, it could easy be the beginning of a xy-problem topic.

Let me know how it goes.

Else, try and check:

I was not considering that… I never thought of indentations as “part of the syntax”. I have to set up a Pi at home, since I left the place where I was running HA on a virtual machine in order to experiment. I’ll get back to you when I was able to try fixing the indentation.

It really doesn’t like it. At least you know now. Thanks for your honesty.

1 Like

Worked! Thank You!

1 Like

You’re welcome :slight_smile:

I fully understand. I’d just tried and help dormantamas out, next time he found himself in a similar situation (by mentioning ChatGPT). I originally answered without a “AI prompt” :slight_smile:.

1 Like