Selected forecast items to ESP32 & ePaper display?

Apologies for a newbie question, just trying to learn new things…

So, I’m trying to figure out how to get five first weather forecast datetime items (and time, condition & temperature from each of them) - and then passed them to esphome node’s configuration to be presented in a ePaper display. I have managed to show couple of simple sensor values on the epaper already, but this weather data stuff feels a bit more complicated.

From developer tools I can see that forecast part looks like this (beginning of it).

  - datetime: '2021-05-14T15:00:00+03:00'
    condition: rainy
    temperature: 17.2
    precipitation: 0.1
    wind_speed: 1.98
    wind_bearing: 130
    pressure: 1007.94
    humidity: 62.05
  - datetime: '2021-05-14T18:00:00+03:00'
    condition: sunny
    temperature: 19.6
    precipitation: 0
    wind_speed: 2.82
    wind_bearing: 100
    pressure: 1007.23
    humidity: 49.02
  - datetime: '2021-05-14T21:00:00+03:00'
    condition: sunny
    temperature: 16.1
    precipitation: 0
    wind_speed: 2.26
    wind_bearing: 137
    pressure: 1007.06
    humidity: 56.42
  - datetime: '2021-05-15T00:00:00+03:00'
    condition: sunny
    temperature: 9.5
    precipitation: 0
    wind_speed: 1.38
    wind_bearing: 84
    pressure: 1007.72
    humidity: 90.49
  - datetime: '2021-05-15T03:00:00+03:00'
    condition: partlycloudy
    temperature: 8.8
    precipitation: 0
    wind_speed: 1.98
    wind_bearing: 86
    pressure: 1006.95
    humidity: 92.02
  - datetime: '2021-05-15T06:00:00+03:00'
    condition: partlycloudy
    temperature: 8.8
    precipitation: 0
    wind_speed: 2.22
    wind_bearing: 85
    pressure: 1006.52
    humidity: 95.3
  - datetime: '2021-05-15T09:00:00+03:00'
    condition: cloudy
    temperature: 15.4
    precipitation: 0
    wind_speed: 2.74
    wind_bearing: 88
    pressure: 1005.46
    humidity: 72.49

I guess I might need to use a thing called templates, but feel a bit confused about where to start and what to put into ESPHome node’s yaml. Any help appreciated.

To get info from home assistant to eshome use one of the documented home assistant sensors in esphome.

So, are you saying that I should try to create (in Home Assistant) a template where I gather needed forecast related values into a string, assign it somehow to a state (?) and then get that state to ESPHome using text sensor, split it and present values on e-Paper?

For each item you want to present in esphome create a template sensor in HA. Then use a home assistant sensor (or HA text sensor or HA binary sensor) in esphome. They do not need to be combined then split.

1 Like

Take a look at these projects:

1 Like

Thanks, @nickrout. It might be a good, straightforward idea. Where I’m really stumbling right now is getting out individual forecast values. I can’t figure out how to refer to Nth temperature, condition, etc…
Tried something like:
{{ state_attr('weather.hakametsa.forecast[1]’, 'temperature')}}
and getting ’None’ out. Most likely an easy nut to crack?

Thank you @nanobra1n for thel links! Those are indeed pretty inspirational implementations! Have to bookmark them for reference.
However, the first seems to use something called MQTT (instead of HA API) to get values from HA to ESP - and that might be a lesson in the future :smiley: And the latter does not seem to present any forecast data, where I currently need some help…

Got a hint that this piece of code will give me the first temperature from FMI weather forecast.

{{ state_attr('weather.hakametsa', 'forecast')[0].temperature}}

Very good. Now I’m able to move on picking values I want to show now…

I have something similar (or exactly) what you want.

First I created a sensor in HA to get the forecast values in to one long string.
Values are separated with ; and days are separated with #.

  - platform: template
        friendly_name: "Five day weather"
        value_template: >-
             # this creates an icon for each weather that can be displayed in ESPhome with the mdi font
             {% set weather = {
                "sunny": "",
                "clear-day": "", 
                "clear-night": "", 
                "cloudy": "", 
                "rainy": "", 
                "sleet": "", 
                "snow": "s", 
                "wind": "", 
                "fog": "", 
                "partlycloudy": "", 
              } %}
             # this is a translation table to get day names in local language
             {% set days = {'Mon':'Mån','Tue':'Tis','Wed':'Ons','Thu':'Tors','Fri':'Fre','Sat':'Lör','Sun':'Sön'} %}

                {% for state in[1:6] -%}
                {{ days[as_timestamp(state.datetime)| timestamp_custom("%a")] }};{{state.templow}}-{{ state.temperature }}°C;{{ state.precipitation | replace('.', ',') }}mm;{{ weather[state.condition] }}#
                {%- endfor %}

This sensor creates this:


In ESP-Home you add a text sensor:

  - platform: homeassistant
    id: w_fivedays
    entity_id: sensor.weather_fivedays

The font:

  - file: 'materialdesignicons-webfont.ttf'
    id: weather_font
    size: 49
    glyphs: [
      # Weather
      "", # mdi-weather-sunny
      "", # mdi-weather-night
      "", # mdi-weather-cloudy
      "", # mdi-weather-pouring
      "", # mdi-weather-snowy-rainy
      "s", # mdi-weather-snowy-heavy
      "", # mdi-weather-windy-variant
      "", # mdi-weather-fog
      "n", # mdi-weather-night-partly-cloudy
      "", # mdi-weather-partly-cloudy

And the display lambda:

  - platform: waveshare_epaper
    cs_pin: 5
    dc_pin: 19
    busy_pin: 4
    reset_pin: 12
    model: 2.90in
    full_update_every: 15
    update_interval: 1s
    lambda: |-
          fivedays = id(w_fivedays).state;
          //ESP_LOGD("%s", fivedays.c_str());
          int count = 0;
          int wx = 0; // start positio x
          int wy = 0; // start position y
          token = strtok (&fivedays[0],"#");
          // this while splits the string (I believe, I "found" the code)
          while (token != NULL)
            token = strtok (NULL, "#");
          // here we loop the days
          for ( std::string fiv : five ) {
            it.rectangle(0, wy, 128, 59);  // adds a border around the "day"
            it.rectangle(1, wy+1, 126, 57);
            str = "";
            str = fiv;
            //ESP_LOGD("test: ", "String to Vector: %s", str.c_str());
            token = strtok (&str[0],";");
            while (token != NULL)
              token = strtok (NULL, ";");

            // this is the loop for each value in the "day"
            for ( std::string s : v ) {

              if(count == 0){
                // Day (Mon/Tue...)
                it.printf(wx +7, wy, id(my_font_small), "%s", s.c_str());
              }else if(count == 1){
                // Temperature 
                it.printf(wx +7, wy+18, id(my_font_small), "%s", s.c_str());
              }else if(count == 2){
                // Precipitation 
                it.printf(wx +7, wy+35, id(my_font_small), "%s", s.c_str());
              }else if(count == 3){
                // weather icon
                it.printf(123, wy+5, id(weather_font), TextAlign::TOP_RIGHT, "%s", s.c_str());
              //ESP_LOGD("test: ", "String to Vector: %s", s.c_str());
              count += 1;
            count = 0;
            wy += 59; // move down 59 pixels and output next day

The code is not good in anyways and has lots of room for improvement. But it works.

Wow, thank you @Hellis81!

Need to read and get to know your solution a bit better to understand what it does, localise it to FMI Weather service & my 4.2 inch display - but this certainly helps a lot!

Yes it will need some adjustments. But I believe if you just use your weather sensor in HA and change the display then it should output a small version of the weather on your screen.
Then you just need to “up-scale” it.

it seems to not work, i dont’know if something is changed in lambda backend of esphome but now it stuck at the variable declaration:
lambda: |-
fivedays = id(w_fivedays).state;
This produce error.