How to "clone" a weather entity into a template sensor, with less datafields?

Hey everyone,
I’m using the openweathermap integration to get the percipitation probability for the next 48 hours, to then push the attributes of that to an esphome node via a home assistant text sensor and deserialize the state of that sensor from JSON.

Problem: the attributes of the weather entity within HA are way bigger than the 255 characters of the esphome text sensor state on the receiving end.

Is there a way the clone/copy a weather entity within HA into e.g. a template sensor, but to restrict the number of copied datafields?

My current workaround would be to create one template sensor each in HA per datapoint, which sounds horrible. sensor.chanceOfRain1hlater, sensor.chanceOfRain2hlater, … :face_vomiting:
Let alone I would have to setup individual HA text sensors on the esphome side…

I checked the forum and the documentation, but it didn’t “click”.

Thanks a lot in advance!

Hi
You can create a template sensor with attributes, see : Template - Home Assistant

Means you can create a template sensor with it’s state based on the “real” weather sensor, and only few attributes on that templat one.

2 Likes

If you create a copy then you would copy the 255 attributes too and thus you need to limit it during the copy…if you limit it during the copy then can’t you just limit it when writing to esp?

No, you won’t copy all attributes. To define the template sensor you need to get a state.
This state can be anything. The state from another sensor, an attribute from another sensor…what you want !
And on this very same template sensor, you can also add the attributes you want. These attributes are named by you (see doc) and the value is anything you want (an attribute from any other sensor, the state on any other sensor…)

In that way he will be able to create some kind of copy of his weather sensor, but with less attributes because it was his problem (too much attributes in the original sensor).

Looks a fine solution to me, maybe there are better ones (probably ! IDK)

My comment was wrongly formulated, sorry…it should be 255 characters (not attributes) and since these need to be chopped/truncated during template creation then I thought to chop when sendin to ESP
The othe sensor would of course work fine and make it transparetn, just another step I would personally avoid

Okay, things are getting weird here. I was working on the template sensor solution, and wanted to verify the max size/length of the esphome text sensor before. I couldnt find an “official” documentation entry for the “255 max” anymore. What did strike me though: when looking at the log ouput of the esphome node, the text sensor content was indeed cut off, but way past the supposed 255 characters:


(I’m now wondering if the cutoff in the logs is not because of the sensor size but because of the max length of a log entry…)

Based on the original code, this is the output I could extract from the sensor so far, 10 items from the list:

Just for kicks I increased the document size within arduinoJson from 2048 to 4048, and after compiling and flashing I now get twice the number of bars.

So it looks as if just increasing the document size works for my usecase, and I can just use the existing text sensor to pull data in. But I still want to find the ACTUAL size/length limit of the HA text sensor for future usecases…

For the sake of completeness:

The sensor to pull in the forecast data in esphome (the bottom one):

text_sensor:
  - platform: homeassistant
    id: outside_weather
    entity_id: weather.home
    internal: true
  - platform: homeassistant
    id: owm_forecast
    entity_id: weather.openweathermap
    internal: true
    attribute: forecast

The code to extract and deserialise the JSON coming from the text sensor (where I now switched 2048 to 4048):

      - id: page2
        lambda: |-
          it.printf(0, 0, id(fontsmall), "chance of rain 24h");
          it.line(5, 25, 291, 25);
          if (id(owm_forecast).has_state())
          {
          DynamicJsonDocument doc(2048);
          deserializeJson(doc, (id(owm_forecast).state.c_str()));
          JsonArray root = doc.as<JsonArray>();
          int linestart = 5;
          for (int i=0; i <= 23; ++i)
          {
          JsonObject root_0 = root[i];
          float root_0_precipitation_probability = root_0["clouds"];
          int heighthelper = 1 + (int)root_0_precipitation_probability;
          int offsethelper = 125 - heighthelper;
          it.filled_rectangle(linestart, offsethelper, 10, heighthelper);
          linestart += 12;
          }
          
          }

(fyi: I’m using the “clouds” datafield at the moment as there are always values, while chance of rain is mostly zero these days)

Here are 2 examples where I found the 255char limit mentioned:

1 Like

Thanks for your thoughts Laikini.

Default buffer for the esphome logger component is indeed “only” 512 long. You can increase it in yaml

logger:
  tx_buffer_size: 1024

:man_facepalming: