Sensor value at specific time

I want to read out a sensor value at a specific time , i used this code to get the value.
I want to use this as base number at the end of the day,to start the next day to display the amound of kwh is produced , so i can deduct the current poduced power minus the value from the sensor at a set time.
At first i thought it worked, but it reads the value only that minit.
I tried doing something with ‘var’ but i failed.
Doe anyone know what i need and on how to accomplisch this.

sensors:
        start_kwh:
          friendly_name: "start_kWh"
          entity_id: []
          value_template: >-
            {% if now().hour == 23 and now().minute == 58 -%}
              {{ states('sensor.p1_meter_3c39e727cc84_total_power_export') }}
            {%- endif %}
          unit_of_measurement: "kWh"

thanks in advance.

Have you considered using the Utility Meter integration?

I just want a value from the sensor at a specific time,

can i use this:

alias: power recorder
description: >
  Records the power 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.input_number_temp_yday
      value: "{{ states('sensor.p1_meter_3c39e727cc84_total_power_import_t1')|float(0) }}" 
  - service: input_number.set_value
    data:
      entity_id: input_number.input_number_temp_day_before
      value: "{{ states('input_number.input_number_temp_yday')|float(0) }}"

i do not know how this works, i made the input numbers helpers but also this does not work.

Is the “day_before” helper meant to represent the day before yesterday? If so, you need to set it’s value before you set the “yesterday” helper, or they will end up being the same value.

alias: power recorder
description: >
  Records the power 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:
      value: "{{ states('input_number.input_number_temp_yday') | float(0) }}"
    target:
      entity_id: input_number.input_number_temp_day_before
  - service: input_number.set_value
    data:
      value: "{{ states('sensor.p1_meter_3c39e727cc84_total_power_import_t1') | float(0) }}"
    target:
      entity_id: input_number.input_number_temp_yday

that could be treu, both values are the same, but none of my input helpers get the data from the sensor.
The only thing i want is the value at 23:59 each day.

In the end i got it working, i can now read the value at 23:59 and use it in the energy calcultation.

Hi, could you share the code working for you? Trying to do something similar, to have daily consumption from YTD sensor data. Thanks a lot

I’m struggling with the same problem. Can you please tell us what your solution is?

You can have a template sensor that updates when a specific trigger fires, without the need for automations.
With these, you can avoid the limits of input_* helper sensors; you can assign state_class, device_class, etc.
Instead of updating whenever the entities involved update, you force the sensor to update only when the trigger(s) fires.

Here is an example

template:
  - trigger:
      - platform: time
        at: "00:01:00"
    sensor:
      - name: "Grid Import Energy - End of Day"
        unique_id: grid_import_energy_end_of_day
        state_class: total
        unit_of_measurement: kWh
        device_class: energy
        icon: mdi:transmission-tower-import
        state: "{{ states('sensor.grid_import_energy') }}"
        availability: "{{ has_value('sensor.grid_import_energy') }}"

This will create a regular template sensor, except the state logic will only execute once every night, at the defined time; giving you a sensor that keeps its value, until the next day’s night.

1 Like

Is there a way to get an array of all the values at end of each hour?

I have a utility meter that I need to get the X latest hours of values to predict future consumption, but the sensor only gives me the current accumulated consumption in real time.