What is the best option for saving a rolling 24 hour temperature?

I tried last night to create a dynamic attribute name with the temperature as the value but it didn’t work.
Then I found a post here saying it can’t be done without python :confused:

So how can I save 24 hours of weather temperature?
I need to have this data easily accessible since I want to output it on a MAX7219 8 bit display, something like temperature now and temperature say 5 hours ago.
As far as I have managed to google it’s not easily accessible in the history database, so that is why I thought I could just save it in an entity.
Since this doesn’t work:

customizer.set_attribute
  entity_id: input_text.text1
  attribute: now().hour
  value: 5

I assume I need some if, elseif to set the attribute name (?), but is that possible?
Can I break it up something like this?

customizer.set_attribute
  entity_id: input_text.text1
  if(now().hour == 0) attribute: "0";
  if(now().hour == 1) attribute: "1";
  ....
  ....
  value: 5

My hope is that it will work in an automation that runs every hour.

use statistics sensor.

sensor:
  - platform: statistics
    entity_id: sensor.weather
    sample_size: 3600
    max_age:
      hours: 24

Add a really large sample size if you don’t know how many times the sensor updates in a day. It will cut off at max_age.

The state of that sensor is the ‘mean’, but the sensor itself has more attributes where you can get other stats out.

I’m using statistics (for electricity use tracking) and despite much fiddling, and reading documents, somehow I’ve managed to miss how I can get other stats out. A pointer would be appreciated.

they are attributes of the main sensor. Just take a look at the created sensor in the developer tools / states page. You’ll see that it has 10-15 attributes.

2nd sentence of the official docs

I copy pasted this and only change the sample/sampling typo.
But the entity does not show up after a reboot.

Wait rebooting again, noticed the recorder: was missing

After reboot with recorder and sensor, nothing.
No sensor.weather

@petro @AhmadK Thanks guys. The magic (missed by me) word is attribute. Given that pointer, I took my existing entity_id and used value_template on it to find the mean. Not sure my solution is elegant but it works.

I’m guessing you made a binary_sensor? If you made it a sesnor, the mean is the main state.

No it’s not binary.
It’s listed below my pi_hole sensor and is:

- platform: statistics
    entity_id: sensor.weather
    sampling_size: 3600
    max_age:
      hours: 24

That is a copy paste of what my config says.

Wait… I think I understand now.
The entity_id is not the name of the sensor that is created. That is the sensor that I want to track?

In that case it should be something like:

sensor:
  - platform: statistics
    entity_id: {{ states.weather.smhi_home.attributes.temperature | float }} # weather.smhi_home.temperature
    sampling_size: 3600
    max_age:
      hours: 24

But it’s not a valid config.
I get:

Error loading /config/configuration.yaml: invalid key: “OrderedDict([(‘states.weather.smhi_home.attributes.temperature | float’, None)])”
in “/config/configuration.yaml”, line 250, column 0

I was talking to @Chiny with my last response.

What is the statre of sensor.smhi_home? If you are trying to use an attribute as the source, you’ll have to make a template sensor for the attribute before feeding it to the statistics sensor.

1 Like

Apologies to @Hellis81 for piggy-backing but we have similar issues.

@petro I made a sensor, especially for the mean, wrongly as it turns out, based on your comment that the mean is the main state, which I now see in Developer Tools/States. Still, my value_template workings will still be useful for adding max/min. Then I need to do some arithmetic, to get costs. Then sort max_age, sampling, icon… :grin:

This is the Home Assistant version of some Python scripts that I have had running for a few years. I’m a newbie to HA, so this is very much experimental, and enjoyable.


So stat is the weather and the attribute holds the temperature (what I want).

So your saying I need:

sensor:
  -platform: template
   sensors: 
       dummy:
           value_template: {{ states.weather.smhi_home.attributes.temperature | float }}

then get this sensor in my statistic sensor?


Alright!!
So how can I now get the value that is the 10th (0.5 hours between the updates and I want five hours old temperature)

how can it work if a) your source entity is weather.hem (not weather.smhi_home that you use in the template) and b) your template is single-line so in must be enclosed in quotes?
did you check your config or reboot HA after making the changes?

also, I’m almost certain you don’t need that | float as the result will be converted to a string anyway, you just need to make sure the result is always a string that represents a float, i.e nothing like unknown.

a) I don’t think it matters.
See this config:

sensor:
  - platform: template
    sensors:
      dummy:
        friendly_name: "DummyTemperature"
        unit_of_measurement: '°C'
        value_template: "{{ states.weather.smhi_home.attributes.temperature | float }}" 
      #value_template: "{{ states.weather.hem.attributes.temperature | float }}"  probably work too!
        
  - platform: statistics
    entity_id: sensor.dummy
    sampling_size: 3600
    max_age:
      hours: 24

Yes I rebooted after I added the above and it worked.

well, now you have quotes around your template but still, is your entity weather.smhi_home? if yes, what’s this?

That is a incorrect screenshot. Sorry about that…

:man_shrugging:

Yes, that’s what I meant. You can add some safety so that you don’t get boot up errors by doing this:

sensor:
  - platform: template
    sensors:
      dummy:
        friendly_name: "DummyTemperature"
        unit_of_measurement: '°C'
        value_template: "{{ state_attr('weather.smhi_home','temperature') }}"
        
  - platform: statistics
    entity_id: sensor.dummy
    sampling_size: 3600
    max_age:
      hours: 24

You don’t need the float because i’ts already a float and you’re sending it out at that point. The unit of measurement determines that it’s a ‘number’ for your graphs.

Ok!

But is there any way you get the values from this graph? By code I mean?
I want to pass this value(s) to a 7 segment display.