Track Date of Last Low Temperature

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?

Cheers!

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.

Thanks for the start.
Cheers

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.

image

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.

1 Like

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 :thinking: 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?

Thanks

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
Yes, I have changed the purge interval. Reading about Packages usage now.
Thanks again very much.
Cheers

@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.

homeassistant:       
  packages: !include_dir_merge_named packages/ 

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.

Any thoughts?
Thanks

I assume that after you installed hass-variables that you restarted HA?

and that you also restarted HA after adding the packages include code?

also please post the code that you used to configure the variable in the weather_01.yaml file.

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:

variable:
  countdown_timer:
    value: 30
    attributes:
      friendly_name: 'Countdown'
      icon: mdi:alarm
  countdown_trigger:
    name: Countdown
    value: False
  light_scene:
    value: 'normal'
    attributes:
      previous: ''
    restore: true
  current_power_usage:
    force_update: true

The errors are:

Package variable setup failed. Component countdown_timer Integration 'countdown_timer' not found.
Package variable setup failed. Component countdown_trigger Integration 'countdown_trigger' not found.
Package variable setup failed. Component light_scene Integration 'light_scene' not found.
Package variable setup failed. Component current_power_usage Integration 'current_power_usage' not found.

Part of my confusion is in the haas-variable docs where they say:
Add the component variable to your configuration and declare the variables you want.

Wondering if that needs to be outside of the packages in the configuration.yaml?
Thanks for your efforts here.
Cheers

That seems to look ok at first glance.

Try removing that code (or just comment it out) and add a buipt-in integration entity such as an inout_boolean and see if that gives errors as well.

From the error it seems that there is an indentation error somewhere.

Testing a built-in integration will at least possibly get you a similar error and might point you toward the real problem.

You might be on to something with the indenting. I moved everything over 2 spaces and I get a different error.

expected dict for dictionary value @ data['packages']['variable']['value']

Not sure I follow on how to add a built-in integration entity?? I am trying to research some of the HA examples for Packages to get a better example.

Adding an input_boolean…

input_boolean:
  mytest:

Results in:

Package input_boolean setup failed. Component mytest Integration ‘mytest’ not found.

@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.

weather_01:
  variable:
    countdown_timer:
      value: 30
      attributes:
        friendly_name: 'Countdown'
        icon: mdi:alarm

This results in a Valid configuration. Again, because packages are new to me not sure what the best/correct way to structure this is.

This

homeassistant:       
  packages: !include_dir_merge_named packages/

Should be this.

homeassistant:
  packages: !include_dir_named packages

And this.

weather_01:
  variable:
    countdown_timer:
      value: 30
      attributes:
        friendly_name: 'Countdown'
        icon: mdi:alarm

Should be this.

variable:
  countdown_timer:
    value: 30
    attributes:
      friendly_name: 'Countdown'
      icon: mdi:alarm

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).

I use packages in my config also. Have a look at my configuration.yaml file and my folder structure.

@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.

As you show in your configs you linked, I can still maintain subfolders in my workflow.

Learned a lot for you and @finity on the subject of packages and variables. Now to put these learnings to work.

Cheers!

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”.

  - platform: statistics
    name: "Outdoor Temperature Daily High"
    unique_id: outdoor_temperature_daily_high
    entity_id: sensor.outdoor_temperature
    state_characteristic: value_max
    sampling_size: 500
    max_age:
      hours: 24
    precision: 1

  - platform: statistics
    name: "Outdoor Temperature Daily High Time"
    unique_id: outdoor_temperature_daily_high_time
    entity_id: sensor.outdoor_temperature
    state_characteristic: datetime_value_max
    sampling_size: 500
    max_age:
      hours: 24

image

1 Like