Creating an INPUT ARRAY

An input_array type integration for numeric or string values ​​is similar to the imput_number integration.

Input_select?

1 Like

Hey,
It would take an input_array integration that works like a pyhton array Python Arrays (w3schools.com)

Entity attributes can hold array types.

What exactly are you trying to do?

What problem are you trying to solve?

Hey,
In an automation, I need to memorize an atmospheric pressure value every full hour. For this I use 24 input_number in the following way

alias: Memo Pressions
description: “”
trigger:

  • platform: time_pattern
    hours: “*”
    condition: []
    action:
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress23”) }}”
    target:
    entity_id: input_number.histopress24
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress22”) }}”
    target:
    entity_id: input_number.histopress23
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress21”) }}”
    target:
    entity_id: input_number.histopress22
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress20”) }}”
    target:
    entity_id: input_number.histopress21
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress19”) }}”
    target:
    entity_id: input_number.histopress20
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress18”) }}”
    target:
    entity_id: input_number.histopress19
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress17”) }}”
    target:
    entity_id: input_number.histopress18
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress16”) }}”
    target:
    entity_id: input_number.histopress17
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress15”) }}”
    target:
    entity_id: input_number.histopress16
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress14”) }}”
    target:
    entity_id: input_number.histopress15
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress13”) }}”
    target:
    entity_id: input_number.histopress14
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress12”) }}”
    target:
    entity_id: input_number.histopress13
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress11”) }}”
    target:
    entity_id: input_number.histopress12
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress10”) }}”
    target:
    entity_id: input_number.histopress11
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress9”) }}”
    target:
    entity_id: input_number.histopress10
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress8”) }}”
    target:
    entity_id: input_number.histopress9
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress7”) }}”
    target:
    entity_id: input_number.histopress8
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress6”) }}”
    target:
    entity_id: input_number.histopress7
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress5”) }}”
    target:
    entity_id: input_number.histopress6
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress4”) }}”
    target:
    entity_id: input_number.histopress5
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress3”) }}”
    target:
    entity_id: input_number.histopress4
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress2”) }}”
    target:
    entity_id: input_number.histopress3
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress1”) }}”
    target:
    entity_id: input_number.histopress2
  • service: input_number.set_value
    data:
    value: “{{ states(“input_number.histopress0”) }}”
    target:
    entity_id: input_number.histopress1
  • service: input_number.set_value
    data:
    value: “{{ states(“sensor.relative_pressure”) }}”
    target:
    entity_id: input_number.histopress0
    mode: single

If I had the option of having an input_array, I would just create a single input entity of size 24 and use a For Loop to store the values ​​into it.

You could write a JSON string into an input_text helper.

{% set input_text = "[4,6,1,9,8,3]" %}
Input text: "{{ input_text }}"
{% set array = input_text|from_json %}
Array: {{ array }}
{% set new_pressure = 7 %}
{% set array = array[1:] + [new_pressure] %}
Updated array: {{ array }}
{% set input_text = array|to_json %}
Updated input text: "{{ input_text }}"

You can do this by creating a single sensor with 24 attributes,

If I understood you correctly, you have 24 Input Numbers representing a record of the last 24 values of sensor.relative_pressure recorded at one-hour intervals.

I know how to do this with a single Trigger-based Template Sensor (and its value and attributes aren’t lost after a restart). However, what do you do with these 24 values?


Example

The following Trigger-based Template Sensor records the value of sensor.relative_pressure every hour and stores it, and the time when it was recorded, in a list within an attribute named hourly_pressure. The attribute maintains a record of the last 24 pressure readings (older values are discarded).

template:
  - trigger:
      - platform: time_pattern
        hours: '*'
    sensor:
      - name: Atmospheric Pressure 
        state: "{{ now().timestamp() | timestamp_custom() }}"
        attributes:
          hourly_pressure: >
            {% set current = this.attributes.get('hourly_pressure', []) %}
            {% set new = [{
              "pressure": states('sensor.relative_pressure') | default (0),
              "time": now().isoformat() }] %}
            {{ (new + current)[:24] }}
3 Likes

Hey 123 Taras,

I use them in a weather prediction algorithm on my HASS. I also transfer them in an MQTT push for another device.

Thank for you help.

In that case, the Trigger-based Template Sensor I posted above should do everything you require.

To publish the sensor’s 24 pressure values to MQTT, you can do it like this:

 - service: mqtt.publish
   data:
     topic: your/topic 
     payload_template: >
       {{ state_attr('sensor.atmospheric_pressure', 'hourly_pressure') 
         | map(attribute='pressure') | list }}

The 24 pressure values are in order of newest to oldest. If you want them in the opposite order, just use the reverse filter

     payload_template: >
       {{ state_attr('sensor.atmospheric_pressure', 'hourly_pressure') 
         | map(attribute='pressure') | reverse | list }}
1 Like

Hey 123 Taras,

Great, thanks, it works great. Would it be possible to complete “pressure”: with an index incremented from 1 to 24 to give “pressure1”:, “pressure2”:, …, “pressure24”:.

There’s no need to do that. The value of the hourly_pressure attribute is a list. Lists are indexed starting with the number zero.

For example, this reports the first, second, and last pressure values in the list.

{% set x = state_attr('sensor.atmospheric_pressure', 'hourly_pressure') %}
{{ x[0] }}
{{ x[1] }}
{{ x[-1] }}

Hey 123 Taras,

Everything works perfectly.
Thank you for your help.

1 Like

Sorry to revive an old thread, but does this survive a HA restart?

Because it’s Trigger-based Template Sensor.