Esphome help with nested attribute in homeassistant

Hi, I’m trying to create a weather forecast esp32 lcd device that takes advantage of the home assistant weather service.
The weather forecast for the following days is in a strange format to be an attribute: for example ‘[{’ condition ‘:’ cloudy ‘,’ precipitation ‘: 0.7,’ temperature ‘: 20.2,’ templow ‘: 14.0,’ datetime ‘:’ 2021-09-27T10: 00: 00 + 00: 00 ‘,’ wind_bearing ‘: 196.8,’ wind_speed ‘: 7.9}, {’ condition ‘:’ partlycloudy ‘,’ precipitation ‘: 0.0,’ temperature ': 23.2

Days don’t seem to have a unique id.

In esphome i do many attempts:

In this way i obtain only day1 condition:

text_sensor:
  - platform: homeassistant
    id: weather_test
    entity_id: weather.casa
    attribute: forecast

in this way the text sensor simply doesnt work:

text_sensor:
  - platform: homeassistant
    id: weather_test
    entity_id: weather.casa
    attribute: forecast.1 or forecast[1] or forecast.1.condition

I guess I don’t understand how to take condition values ​​when they are nested.
If in homeassistant i add this template sensor and using it in esphome work fine

      forecast1_hass:
        value_template: 
          '{{ states.weather.casa.attributes.forecast.1.condition }}'

But I would still like to understand if there is a way to take the nested values ​​of homeassistant without creating template sensors every time.

Thank you in advice :slight_smile:

This should bring in all the forecast information:

text_sensor:
  - platform: homeassistant
    id: weather_test
    entity_id: weather.casa
    attribute: forecast

You can then split it up in ESPHome.

Thank you tom in this way teorically i import all forecast 6 day state array in this sensor, but after how i can grab for example condition of day 1 or day2? You think there is a way to print condition in lambda?
(just an example)
it.printf(0, (it.get_height()/3-15), id(font8), TextAlign::BASELINE_LEFT, “%s”, id(forecast_test)1.condition.state.c_str());
I have do some attempts but I still think I don’t understand.

Can you write a code example for split the various strings please?

@meconiotech, did you solve this in the end? Exactly same situation here, working on figuring this out. I’m looking intro extracting the chance of rain to then create a visual representation out of it

My way of doing this kind of things is to create a sensor in HA that parse the weather to what I need and make it a string.
Example:

Date;wind;temp;etc.#Date2;wind2;temp2...

This then means I have one simple string I can split ans output on the screen.
And since I create the string in HA I can convert units to what I want and change date of today to “today”/“weekday” easier than in ESP-Home.

Hey, thanks for the direction :slight_smile: . Would you be able to provide a snippet of example code both for the HA and the esphome side of this approach? Thanks a lot in advance!

Hi, yes in the end i have done this, i learned some basic of c++ and writed this progam for esphome:
Basically when you import attribute forecast in esphome the sensor contain a nested json that contain 5 days of forecast and condition and need to be deserialized,

1 Like

@meconiotech, thanks a lot, that helped me imensely to get the whole arduinojson thing going! How does your forecast_5 sensor/entitiy look like on the HA side? I’m using the weather entity of openweathermap, but it’s too big to just “import” the attributes into an esphome text sensor (it gets cut off after the first 5 or 6 forecast objects). I’m now thinking about creating a template sensor within HA that just contains the datafields that I need, in the hope that I stay under the 255 characters for a text sensor state on esphome…

Hello, sorry for delay. There is an option in the openweathermap wizard that fit this configuration, I don’t remember what it was, but there is an option to make openweathermap identical to met.no format and you don’t necessarily have to change the esphome code (excluding the weather entity). Make some attempts with onecall, onecall daily etc…

In the end it turned out that I just had made the arduinojson object too small, after increasing its size I got all the entries in one go without creating a smaller version :slight_smile:

Hi @Hellis81 . Can you give details about how you do this? It is exactly what I want to do, but I have been unsuccessful at it so far.