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?
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
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
Store the list as an attribute of the sensor. You can set the state to whatever you want.
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:
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(',') }}