If this is something you know you’re going to need ahead of time, would it be possible to have an automation store the current temperature away so that you have it available for tomorrow (either in a variable, or a database, or even simply just a file)?
That’s the problem, its not a simple query. You’d need to intelligently search though the states and choose one that represents the time frame you are looking for.
Example: You want to get the state at 4pm. The last state changes were at noon and 9pm. You intelligently need to choose the noon state.
That’s why I was suggesting preemptively storing the desired data. The data would obviously be missing if the system was down the previous day, but might be workable.
All of this, of course, assumes there isn’t a website that could be used to extract the data from… similar to what was worked on for the Hours of Daylight discussion.
After a lot of trial and error I taped this SQL query together:
# Get the sensor value at a specified time
- platform: sql
queries:
- name: Temp. piscina ontem
query: "SELECT * FROM states WHERE entity_id = 'sensor.temperatura_piscina' ORDER BY ABS(strftime('%s', created) - strftime('%s', '2019-11-07 12:00:00', 'utc')) LIMIT 1;"
column: 'state'
Although the time (hour) seems shifted in the state window, it seems that it’s getting the right value. Timezones can be a …argh.
It works. However, at least two people on StackOverflow said that this solution is NOT efficient and will slow down massively on very big databases (multiple GBs). Random Google reader, keep that in mind - please.
The SQL query, in my understanding, works by subtracting both times and then taking the absolute (removing the minus sign, basically). Then it sorts the results by smallest difference and displays the first one (which is the smallest difference).
# Get the sensor value yesterday at the same time as today - Only partially tested!
- platform: sql
queries:
- name: Temp. piscina ontem
query: "SELECT * FROM states WHERE entity_id = 'sensor.temperatura_piscina' ORDER BY ABS(strftime('%s', created) - strftime('%s', datetime('now'), '-1 day')) LIMIT 1;"
column: 'state'
#scan_interval: 30 (default)
It’s a sensor, its always 24 hours behind. It’s updated based on the scan interval which is 30 seconds. Make an automation that performs your math and trigger it at the time you want to get the value.
It updates a template sensor value only at 11:59pm from the source sensor and stores that value for 24 hours until the next day when it refreshes again.
Good solution, but people should realize that the difference between sensors is that yours is always at 23:59, where the SQL look up is exactly 24 hours in the past from the current time.
Problem with solutions like this is that it will “reset” during your current day. Where as @Valentino_Stillhardt‘s solution will stay for the full duration of the day. I.e. if you set the time to 22:00 and at 23:00 you look at the sensor, the info is from an hour ago instead of the previous day. So the automation route and @jwoznys solution only really work at midnight.
Just make an automation that triggers at the time you want to keep the data from on that day for the next day, and put the data in a variable. I do something similar here, though I’m triggering on a state change rather than at a certain time, but same concept: