Check an entity’s historical states (weight measurements...)

Hi
i want to have an entity which dies present the history data of an entify.
Example: i want to use the openai-conversation agent and tell ChatGPT something like:

The current weight is {{states('sensor.withings_gewicht_2') }} kg - 
the defined target {{states('sensor.withings_zielgewicht_2') }} kg.
The last 7 days of weight mesurements are ???What to do here???

Is there a way to provide those past status of an entiy in an array?
Or do i have to prepare & maintain such an array separatley?

Thank you in advance!

Best
Thorsten

Templates can only access the current state of any entity. So you would need to create a sensor that maintained the past 7 values. You can do this with a trigger-based template sensor. Since the state is always a string, you can either

  1. convert a list to a string to store it as the state. The convert from a string when you need to access it or modify it
  2. Store the list as an attribute of the sensor. You can set the state to whatever you want.

Thanks mekaneck,
easy said… but i would love if you provide more guidance.
I´ve tried to create a sensor:

      - name: "withings-weight-thorsten"
        unique_id: "withings_weighth-thorsten"
        unit_of_measurement: "kg"
        state: >
          {% set array_in = states('input_text.withings_weight-thorsten').split(',') %}
          {% set new_value = states('sensor.withings_gewicht') %}
          {% set array_out =[new_value] + array_in[0:10] %} 
          {{ array_out | join(',') }}
        state_class: measurement

EDIT: got it - i should store the values as attribute to the value…
so … i´m still lost without some examples/guidance. Can you help?

Thank you in advance.

If you wanted to use the state of the template sensor, this is how I would do it:

template:
  - trigger:
      - platform: state
        entity_id: sensor.withings_gewicht
        not_to:
          - unavailable
          - unknown
    sensor:
      - name: withings-weight-thorsten-state
        unique_id: 0783ee15-9ecc-4d0b-9eb0-6f179439beca
        state: >
          {% set list_in = this.state.split(',') | map('float', None) | list %}
          {% set new_value = trigger.to_state.state | float(None) %}
          {% set list_out = [new_value] + list_in[:10] %} 
          {{ list_out | reject('eq', None) | map('round',1) | join(',') }}

Note that since the state is a string and not a number, you can’t apply units or set the state_class to measurement. So those lines were removed.

To reference the state in a template (e.g. for ChatGPT) you simply use
{{ states('sensor.withings-weight-thorsten-state') }}

Note that you could use a very similar trigger and template in an automation to set the state of a UI-created input_text helper entity, if you don’t like working in YAML and you are ok with two separate entities (an automation and an input helper).

If you wanted to use the attribute of a sensor, which can hold a list without going back and forth between a string, it’s the same trigger but a different sensor config:

  - trigger:
      - platform: state
        entity_id: sensor.withings_gewicht
        not_to:
          - unavailable
          - unknown
    sensor:
      - name: withings-weight-thorsten-attribute
        unique_id: 0021a722-7650-408b-b3a5-a6031f1a2972
        unit_of_measurement: "kg"
        state_class: measurement
        state: "{{ trigger.to_state.state }}"
        attributes:
          history: >
            {{ ([trigger.to_state.state | float | round(1)] + this.attributes.history | default([]))[:10] }}

Since this sensor’s state is a number (I chose to use the most recent weight as the state), we can set the units and state_class. To use this attribute in a template formatted as a string, you’d use this template:
{{ state_attr('sensor.withings-weight-thorsten-attribute', 'history') | join(',') }}

to keep it short: you are my hero of the day!
Thank you - this works like a charm!