How to save current entity value every 24hours?

Hi,

i have an entity that update themself every second.
named smartmeter_consumption

I want to save the value that it gives every 24hours…
So if the value is now 9000, than it must save now 9000.

But in 24 hours its higher and i want to save than again… so i have a course every 24 hours of the value.

Is that possible to save an entity value every 24 hours?

1 Like

Create a template sensor with a time trigger (eg “00:00”) and use ‘sensor.smartmeter_consumption’ in the state.

Can you show me an example? :frowning: I tried but dotn understand that codes

@bertreb it would be great if you can show me the code that i need for my configuration.yaml

I searched but doesnt get an result :frowning:

The code to add in configuration.yaml

template:
  - trigger:
    - platform: time
      at: "00:00"
    sensor:
    - name: "smartmeter consumption daily"
      unit_of_measurement: "kWh"
      state: >
        {{ states('sensor.smartmeter_consumption') }}

Be aware that with this sensor you get a state ‘unknown’ after a restart of HA. Thats the state until the next time trigger.
You can avoid the ‘unkown’ state, by adding a ‘homeassistant start’ trigger. It will be the value of that moment.

If thats not good enough, you could use the HACS ‘var’ component and use an automation to set the var value every day at 00:00. This solution is HA restart proof.

1 Like

Another route possibly. A little more complicated, however with the work you get some good flexibility:

I am using code similar to this to update a sensor value. It works great the first trigger time but noticed it didn’t update the following days. When I looed in the list of entities I can see duplicates of the entity have been created. It looks like each time this is trigger it tries to create a new entity, adds a _3 or_4 or whatever the next number up is to the entity name and then stores the desired value in that new entity. Is there something I am missing in my code that is making it do this?
Sample yaml:

template:        
  - trigger:
      - platform: time
        at: "00:30:00"
    sensor:
      - name: "Estimate Solar Eneregy at 0030"
        state: >
          {{ (states('sensor.energy_production_today')) }}
        unit_of_measurement: "kWh"

Looks ok. I have the same type of template sensor with as an extra the device_class defined (device_class: energy).
You could look in the log to see what happens around 00:30:00

Can I bump this thread?

I made a 24h temperature average sensor with

- platform: statistics
  name: "24h Temp Aussen BlauesHaus"
  entity_id: sensor.t_h_sensor_2_temperature_2
  state_characteristic: mean
  max_age:
    hours: 24

in my sensors.yaml, and want that to be one distinct value at 4am every day.