So @krash link query is not putting a draw on resources like mentioned above?
Just for fun-sake. I was wondering why the template wouldn’t work. Indeed you’re right. If I enter it in the config.yaml under sensors it doesn’t work…
But if I enter it in the developer templating-test-tool. It does work… why??? Is it because the reboot should occur at exactly that that timeframe? But would the template be useful if I put it in an automation or script? I like the simplicity of it…
It won’t work in the template editor either because your times won’t match. If it’s working for you, you’re doing something wrong. That template will only work once it passes midnight once. Then it will stay that value until the next midnight if you use the updated template I posted awhile ago. The template you posted in this post will only work from 23:59 to 00:00. All other times it will be unavailable.
FYI, an updated version of the template using the new template sensor integration would be:
template:
- trigger:
- platform: time
at: "00:00:00"
sensor:
- name: Temperatuur vandaag
unique_id: verwachte_temperatuur
state: "{{ states('sensor.buienradar_temperature_1d') }}"
unit_of_measurement: °C
Just keep in mind that it will be unavailable until the trigger fires. You can add a home assistant startup trigger in there too if you want it to refresh at startup. However, it will not be the midnight time.
The related part to this post is the automation, where I maintain last 25 values of a sensor (every hour since yesterday at this hour) with an first-in first-out buffer made with an input_text.
In the template sensor I extract values of the buffer and process them to get a simple estimation of next hours linear-weighted average temperature.
I made it using the sql integration. The suggested queries didn’t worked for me, so I wrote my own: Here it is:
SELECT state FROM states WHERE entity_id = 'sensor.YOURSENSOR' AND CAST(strftime('%s', last_updated) AS integer) > (CAST(strftime('%s', 'now') AS integer) - 86400) ORDER BY last_updated LIMIT 1
And I don’t think this one will have troubles with TZ or DST. The compare is done on unix time stamps (%s).
What I still don’t know is how to control the refresh rate of this sensor. If you have any tip, let me know
SELECT state FROM states WHERE entity_id = 'sensor.total_cost_today' AND HOUR(TIMEDIFF(UTC_TIMESTAMP(), last_updated))>=24 ORDER BY last_updated DESC LIMIT 1
The querying of the DB indeed takes a lot of CPU power; my Pi4 increased from 30% CPU usage to 50%, which really slows the overall experience of HA.
Solution: disable the updates for the SQL sensor and update it via an automation at the frequency you want.
see: SQL - Home Assistant scan interval
In summary: disable the updates in the SQL integration (via the 3 dots and system options).
Next: make an automation to update the sensor, eg. every 10 minutes.
Example:
- alias: update van zonneproductie gisteren op dit uur
id: "6f18655a-e923-46b5-9a83-8d3340458e83"
description: ""
trigger:
- platform: time_pattern
minutes: /10
condition: []
action:
- service: homeassistant.update_entity
data: {}
target:
entity_id: sensor.zonneproductie_gisteren_op_dit_uur
mode: single
Note: I defined the SQL sensor in the configuration.yaml file and saw that the sensor was mentioned 2 times in the SQL integration. I have the impression that you have to define the SQL sensor via the GUI only instead of the configuration.yaml because you cannot disable the updates via the config file.
The timestamp spec in the database has changed in recent schema, so old SQL queries may no longer work. If you can wait a day to start using your data, I’d suggest using an automation to record the temperature in advance. Another option would be a trigger-based template sensor, but I’m not convinced they would do what you want if the system restarted. Something like this, with two input_number helpers already set up:
alias: Temperature recorder
description: >
Records the temperature at 23:59 into input_number.temp_yday, dropping
the previous value into input_number.temp_day_before.
trigger:
- platform: time
at: "23:59:00"
action:
- service: input_number.set_value
data:
entity_id: input_number.temp_day_before
value: "{{ states('input_number.temp_yday')|float(0.0) }}"
- service: input_number.set_value
data:
entity_id: input_number.temp_yday
value: "{{ states('sensor.current_temperature')|float(0.0) }}"
You could manually trigger it a couple of times, which should populate both helpers with the current temperature. At least you’ll know that the automation works…
sorry to bother with the original post from 2021.
My template sensor is working, but it becomes unknown after HA restart (i understand why) but trying to research your hint, HA Startup trigger for refreshing the value.
I’ve found