Using Home Assistant Sensor Entity Containing Arrays

I’m building some e-ink displays that are using hundreds of sensor data points to build a few screens. Currently, I’ve tried two solutions since I built my own data collector in Home Assistant (from a JSON template.)

The first one is a single attribute per data point (works mostly okay - just need hundreds of them)

  - platform: homeassistant
    entity_id: sensor.weatherflow_hourly_forecast_0
    id: wf_h_fc_0_air_temperature
    attribute: air_temperature

(Looks like this in HA)

air_temperature: 82
am_pm: AM
conditions: Clear
icon: /
local_hour: 10
pixel_height: 90
precip_probability: 0
friendly_name: weatherflow_hourly_forecast_0

The second I’m asking about is how to use a Home Assistant attribute that contains a JSON array. (Would be great to load in just a few sensors with many arrays)

  - platform: homeassistant
    entity_id: sensor.weatherflow_hourly_forecast
    id: wf_h_fc_0_air_temperature
    attribute: air_temperature[0]

(Looks like this in HA)

air_temperature: 84, 84, 82, 84, 84, 84, 84, 82, 81, 77, 75, 73, 72, 70, 68, 68, 66, 66, 64, 64, 64, 66, 70, 72
am_pm: AM, PM, PM, PM, PM, PM, PM, PM, PM, PM, PM, PM, PM, AM, AM, AM, AM, AM, AM, AM, AM, AM, AM, AM
conditions: Clear, Clear, Partly Cloudy, Partly Cloudy, Partly Cloudy, Partly Cloudy, Cloudy, Cloudy, Cloudy, Cloudy, Partly Cloudy, Rain Possible, Partly Cloudy, Rain Possible, Rain Likely, Rain Likely, Rain Likely, Rain Likely, Thunderstorms Likely, Rain Likely, Rain Likely, Rain Possible, Thunderstorms Possible, Cloudy
icon: /, /, R, R, R, R, a, a, a, a, A, D, A, D, b, b, b, b, w, b, b, E, y, a
local_hour: 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
pixel_height: 100, 100, 90, 100, 100, 100, 100, 90, 85, 65, 55, 45, 40, 30, 20, 20, 10, 10, 0, 0, 0, 10, 30, 40
precip_probability: 0, 0, 0, 0, 0, 0, 0, 0, 5, 15, 15, 25, 15, 20, 50, 80, 85, 80, 85, 90, 50, 40, 40, 10
friendly_name: weatherflow_hourly_forecast

It doesn’t look like there is any ability to use JSON arrays stored in a Home Assistant attribute that I can see. I do see that an array comes across as a text string when debugging but it’s not usable as a number. I’m also not sure how to use something like this in a LAMBDA state:

it.printf(hourly_hour_x, hourly_temperature_y - id(wf_h_fc_0_pixel_height).state, id(font_hourly), TextAlign::BOTTOM_RIGHT, "%.0f", id(wf_h_fc_0_air_temperature).state);

Any thoughts on how others may have accomplished this? Being able to pull out an array for a sensor would greatly simplify how I load and manage the data in Home Assistant. Instead of running lots of scripts, I could use a single script with arrays of data. It also allows me to use other sources of data in Home Assistant that use JSON arrays in attributes. And lastly - instead of pulling hundreds of sensors into esphome, I would be able to just pull a few of the sensors and break out the data. Asking the home assistant API in esphome to pull a large number of sensors takes a very long time and sometimes hiccups where data doesn’t get responded to.

Thanks!

I have tried something similar using the weather forecast data from HA and also stucked. As I wanted particular data in ESP home to show it on a display I’ve prepared the data in HA template:

- sensor:
      - name: "Wetter condition naechster Tag"                    
        state: >                    
            {{ state_attr('weather.firefly', 'forecast')[0]['condition'] }}

Thanks, @JackDrake - getting the data out of an HA sensor array into a HA template I understand - but then you still have the hundreds of sensors to pull into esphome. I’m looking to do the JSON parsing on the esp32 after it pulls the data from HA. As a single sensor (or a handful) that’s doable. I need all of the data in my examples to be available in esphome. I’ll share an example of timing:

Startup:

[15:24:49][I][app:102]: ESPHome version 2022.5.0 compiled on May 24 2022, 15:23:42

First sensor definition (90 total defined in this example):

[15:24:49][C][homeassistant.sensor:030]: Homeassistant Sensor 'bedroom_dave_bathroom_humidity_humidity'
[15:24:49][C][homeassistant.sensor:030]:   State Class: ''
[15:24:49][C][homeassistant.sensor:030]:   Unit of Measurement: ''
[15:24:49][C][homeassistant.sensor:030]:   Accuracy Decimals: 1
[15:24:49][C][homeassistant.sensor:031]:   Entity ID: 'sensor.bedroom_dave_bathroom_humidity_humidity'

Last sensor definition:

[15:24:55][C][homeassistant.text_sensor:024]:   Entity ID: 'sensor.weatherflow_current_conditions'
[15:24:55][C][homeassistant.text_sensor:026]:   Attribute: 'conditions'
[15:24:55][C][homeassistant.text_sensor:023]: Homeassistant Text Sensor 'wf_current_conditions_icon'
[15:24:55][C][homeassistant.text_sensor:024]:   Entity ID: 'sensor.weatherflow_current_conditions'
[15:24:55][C][homeassistant.text_sensor:026]:   Attribute: 'icon'

First sensor result:

[15:24:58][D][homeassistant.binary_sensor:026]: 'input_boolean.esp17_prevent_deep_sleep': Got state ON
[15:24:58][D][binary_sensor:034]: 'esp17 Prevent Deep Sleep': Sending initial state ON

Last sensor result and screen refresh:

[15:25:04][D][homeassistant.text_sensor:017]: 'input_select.esp17_page': Got state 'A'
[15:25:04][D][text_sensor:067]: 'esp17_page': Sending state 'A'
[15:25:04][I][WiFi:308]: Exellent
[15:25:04][D][lilygo_t5_47.display:051]: Periodic full update countdown 0.
[15:25:04][D][lilygo_t5_47.display:055]: Full update!

This makes for about 15 seconds of data being sent back and forth to HA from esphome for each deep sleep/wake event. (Ha!! - seems longer when I’m watching it…)

Whereas - if I could just send a hand full of arrays it would be just a few seconds… I’m also having to define configurations like this:

esphome:
  name: ${devicename}
  platformio_options:
    build_flags: 
      - -DCONFIG_ARDUINO_LOOP_STACK_SIZE=49152
esp32:
  board: m5stack-core2
  variant: esp32
  framework:
    type: arduino
    version: 2.0.2
    source: https://github.com/espressif/arduino-esp32.git#2.0.2
    platform_version: https://github.com/platformio/platform-espressif32.git#feature/arduino-upstream

To get around the number of objects I’m using.

I had the same issue, I wanted to show electricity prices for the next 12 hours on a display connected to ESP32. These were available sensor.nordpool attributes today and tomorrow. First I solved this by parsing this in Home Assistant into 12 separate helpers and passing all to ESPHOME.

Now I tried again doing this on ESPHome and finally got it working. A crucial thing was to assign the values first to local variables h0 and ´h1`. When I tried to output the values directly in ESP_LOGD, the value was always 0.

The lambda:

        StaticJsonDocument<600> doc;
        double h0, h1;
        
        ESP_LOGD("ha", "Today: %s", id(nordpool_today).state.c_str());

        DeserializationError error = deserializeJson(doc, id(nordpool_today).state.c_str());
        if (error)
          ESP_LOGE("json", "Deserialize error %s", error.f_str());    
        else {
          h0 = doc[0];
          h1 = doc[1];
          
          ESP_LOGD("ha", "Array.size: %d\nHour 0: %f\nHour 1: %f", doc.size(), h0, h1);
        }

The output:

[07:28:12][D][ha:118]: Today: [0.055, 0.053, 0.053, 0.055, 0.057, 0.06, 0.068, 0.073, 0.113, 0.119, 0.089, 0.08, 0.071, 0.068, 0.068, 0.07, 0.072, 0.077, 0.085, 0.082, 0.073, 0.056, 0.061, 0.057]
[07:28:12][D][ha:127]: Array.size: 24
Hour 0: 0.055000
Hour 1: 0.053000
3 Likes