Hello,
New to HA and just getting my feet wet on setting everything up. Looking for a nudge on where best to start for solving this task. Automation? Sensors? Template? Combo?
I am trying to track the last date where the low temperature for the day was below 60ºF. This should update everyday at midnight. Any thoughts?
How important is it that it always updates at midnight? If today’s temperature goes below 60 would you not want it to show today right away?
Off the top of my head I would say create an automation with a numeric state trigger to run any time the temperature goes below 60. That trigger will only happen when the temp goes from above to below 60.
So you’ll also need another time trigger for midnight or whenever to make sure it triggers for the current day if the temperature has stayed below 60 for more than a day. This means you’ll also need a numeric state condition for temperature below 60 to prevent the automation from running with the time trigger if the temp is above 60.
In that automation, store the current timestamp in an input_text helper.
You’ll also probably want to create a template sensor that converts that string stored in the input text helper to a timestamp for the UI or use in automations/scripts or whatever you need it for.
I see your point on the update at midnight. Probably not needed in my case. Really more of a weather almanac function that I am trying to achieve. If todays temperature drops below 60º that could reflect immediately and become the “Last Date” I am looking for.
I’ll be starting with the automation and adding the template sensor as I will want to display on a dashboard.
here are the automations I use to set the actual highest and lowest temperature of the day so far:
- alias: Env Set Lowest Temp So Far
trigger:
- platform: state
entity_id: sensor.dark_sky_temperature
- platform: time
at: '00:00:00'
action:
- choose:
- conditions:
condition: template
value_template: "{{ trigger.platform == 'time' }}"
sequence:
service: variable.set_variable
data:
variable: lowest_temp_so_far
value: "{{ states('sensor.dark_sky_temperature') | float }}"
attributes:
time: "{{ states('sensor.time') }}"
default:
service: variable.set_variable
data:
variable: lowest_temp_so_far
value: >
{% if states('sensor.dark_sky_temperature') not in ['unavailable', 'unknown'] and (states('sensor.dark_sky_temperature') | float < states('variable.lowest_temp_so_far') | float) %}
{{ states('sensor.dark_sky_temperature') | float }}
{% else %}
{{ states('variable.lowest_temp_so_far') }}
{% endif %}
attributes:
time: >
{% if states('sensor.dark_sky_temperature') not in ['unavailable', 'unknown'] and (states('sensor.dark_sky_temperature') | float < states('variable.lowest_temp_so_far') | float) %}
{{ states('sensor.time') }}
{% else %}
{{ state_attr('variable.lowest_temp_so_far', 'time') }}
{% endif %}
- alias: Env Set Highest Temp So Far
trigger:
- platform: state
entity_id: sensor.dark_sky_temperature
- platform: time
at: '00:00:00'
action:
- choose:
- conditions:
condition: template
value_template: "{{ trigger.platform == 'time' }}"
sequence:
service: variable.set_variable
data:
variable: highest_temp_so_far
value: "{{ states('sensor.dark_sky_temperature') | float }}"
attributes:
time: "{{ states('sensor.time') }}"
default:
service: variable.set_variable
data:
variable: highest_temp_so_far
value: >
{% if states('sensor.dark_sky_temperature') not in ['unavailable', 'unknown'] and (states('sensor.dark_sky_temperature') | float > states('variable.highest_temp_so_far') | float) %}
{{ states('sensor.dark_sky_temperature') | float }}
{% else %}
{{ states('variable.highest_temp_so_far') }}
{% endif %}
attributes:
time: >
{% if states('sensor.dark_sky_temperature') not in ['unavailable', 'unknown'] and (states('sensor.dark_sky_temperature') | float > states('variable.highest_temp_so_far') | float) %}
{{ states('sensor.time') }}
{% else %}
{{ state_attr('variable.highest_temp_so_far', 'time') }}
{% endif %}
then I use these automations to set a variable to those values:
- alias: Env Set Actual Daily Lowest Temp
trigger:
- platform: time
at: '23:59:00'
action:
- service: variable.set_variable
data:
variable: lowest_temp_history
value: "{{ states('variable.lowest_temp_so_far') }}"
attributes:
date: "{{ states('sensor.date') }}"
- alias: Env Set Actual Daily Highest Temp
trigger:
- platform: time
at: '23:59:00'
action:
- service: variable.set_variable
data:
variable: highest_temp_history
value: "{{ states('variable.highest_temp_so_far') }}"
attributes:
date: "{{ states('sensor.date') }}"
Here are the variable configs. It uses a custom integration called “hass-variables” and can be installed thru HACS:
variable:
lowest_temp_so_far:
value: '100.0'
restore: true
attributes:
icon: 'mdi:thermometer'
unit_of_measurement: '°F'
time: '00:00:00'
friendly_name: 'Todays Lowest Temperature So Far'
highest_temp_so_far:
value: '0.0'
restore: true
attributes:
icon: 'mdi:thermometer'
unit_of_measurement: '°F'
time: '00:00:00'
friendly_name: 'Todays Highest Temperature So Far'
I use the data o display in a card. The picture doesn’t show it but if I hover over the graph lines it shows the actual value of the bar.
the only limitation is it will only maintain a history for as long as your recorder purge value is. Mine is 7 days.
if you want a longer term history you could set the variable attributes to keep a longer set of value but it will never be a long period of time. I would think the longest time you could reasonably expect to keep would be 30 days just due to having to set the code to keep those values in the variable.
Thank you. Great stuff to study/learn/implement here. In my case I am using mariaDb for LT storage of history so I should have more than 30 days. I think anyway. At my location I may not see another day below 60º for a couple months or more now. Hence the goal of capturing the LAST date <60º until sometime in the fall when we might dip into the 60’s again.
Cheers
@finity
Found and loaded hass-variables in HACS. Question:
Are you loading your variables config in configuration.yaml or in a separate variable.yaml referenced in configuration.yaml?
Unless you tell HA to change the purge interval the default is only 10 days (I think). It makes no difference if you are using the built-in DB or MariaDB.
Actually, neither. I’m using packages for this.
So everything for my environmental stuff all goes in one package and I reference everything there and include packages in configuration.yaml.
@finity Well starting to get a feel for Packages and the HACS Integration for variables. However I am getting errors “Package variable setup failed. Component xxxxxx Integration ‘xxxxxx’ not found.” xxxxxx is just me replacing the variable name here for example.
Likely I either don’t have the hess-variables loaded and running correctly OR my thinking on using Packages is incorrect. I am using an !include incorrectly.
Then under packages I have another folder weather. Then weather_01.yaml in that weather folder
Maybe I am overcomplicating the structure for a first attempt. Trying to set up a sustainable structure from the start I guess.
Yes restarted after both. My code is the example code from the hass-variables documentation as a test.
I do not restart after entering the code as the Check Configuration fails with errors.
The test code:
@finity So more reading and testing and I find this result by adding another header - weather_01:
Tried this after reading the Packages examples on HA again. Wondering if it has to do with how I wanted to nest folders under the Packages folder?
My folder structure is:
config/packages/weather/weather_01.yaml
And in weather_01.yaml is this code:
If I don’t have the weather_01 line, the configuration is Invalid with errors.
Subfolders in the packages directory don’t matter. It is recursive, they will all be included. Even the file names don’t matter (they just must end in .yaml).
@jazzyisj Thanks for the added advice. I made your changes and it also worked. However your way seems cleaner to me than what I got from the HA docs on Packages.
Just cleaning out some old bookmarks and realized I track the same data but with statistics sensors. Although the values are for last 24 hours, not “today”.