Pull sensor history data from exact 24h ago

Hi,
I am wondering if there is possibility to read sensor history value using dynamic query like ‘now-24h’. I have PV and my inverter exposes entity for daily total production up to this moment: sensor.inverter_daily_yield. What I want to achieve is to see underneath it daily yield from same time yesterday. Is something like this even possible?

Doesn’t have to be real time, it can be done by some automation for example every 1 minute.

Yes you can use the SQL sensor to query the database to return that. There’s even an example query for exactly that in the SQL sensor documentation.

1 Like

Or, using purely HA:

I use this automation and input _number helpers to store the data I want, the automation triggers every evening at midnight and ‘cascades’ the day’s sensor value to the next inline which allows me to display the previous n days of data as separate cards.

i.e.

Today -4 days is stored in today -5
Today -3 days is stored in today -4
Today -2 days is stored in today -3
Today -1 days is stored in today -2
Today is stored in today -1

id: '1699398125439'
  alias: "Outside temp history daily update"
  description: Update temp  entities
  trigger:
  - platform: time
    at: '23:59:59'
  condition: []
  action:
  - service: input_number.set_value
    data_template:
      entity_id: input_number.outside_temp_min_24_hr_5
      value: '{{ states("input_number.outside_temp_min_24_hr_4")| round(2) }}'
  
  - service: input_number.set_value
    data_template:
      entity_id: input_number.outside_temp_min_24_hr_4
      value: '{{ states("input_number.outside_temp_min_24_hr_3")| round(2)}}'
  
  - service: input_number.set_value
    data_template:
      entity_id: input_number.outside_temp_min_24_hr_3
      value: '{{ states("input_number.outside_temp_min_24_hr_2")| round(2)}}'
  
  - service: input_number.set_value
    data_template:
      entity_id: input_number.outside_temp_min_24_hr_2
      value: '{{ states("input_number.outside_temp_min_24_hr_1")| round(2)}}'
  
  - service: input_number.set_value
    data_template:
      entity_id: input_number.outside_temp_min_24_hr_1
      value: '{{ states("sensor.outside_temp_min_24_hours") | round(2) }}'

You can achieve what you have done with a single trigger-based template sensor: see here.

The OP’s question is for fine-grained tracking though — unless you’re going to create 1440 helpers / data elements in an attribute structure, the SQL solution will be better.