Help with Once a day sensor

I know this has been brought up multiple times, as I found solutions in the following link, but I can’t get a once-a-day sensor working (i think)

I have the following template sensor

- trigger:
    - platform: time_pattern
      hours: 0
      minutes: 0
      id: 'midnight'
    - platform: state
      entity_id: sensor.server_rack_electricity_cost
      id: 'change'
  sensor:
    - name: Max Rack Cost
      state: >
        {% set outt = states('sensor.server_rack_electricity_cost')|float(2) %}
        {% set maxt = states('sensor.max_rack_cost')|float(2) %}
        {% if  outt != None  and (trigger.id == 'midnight' or maxt == None
          or outt > maxt) %} {{ outt }}
        {% else %} {{ maxt }} {% endif %}

Basically I have a counter that shows the cost of my server rack over time. If i look at this in grafana, it looks like this:

What I want, is a single value to show up every day at midnight for the max of that day. With the code above though, the trigger doesn’t seem to be working, as I still am getting a saw-tooth pattern:

How can i get this to JUST show me the value recorded at midnight and midnight only?

Does this help you?

platform: time
at: "00:00:00"

I use that for my midnight actions.

let me try that!

didn’t work, it’s still just ticking up every 30 seconds (prometheus collection interval) if i watch the state in the developer pain, it also keeps increasing.

You appear to be using Grafana, you can do this by setting these three options (with your time zone obviously):

Screenshot 2024-05-04 at 12-15-05 Grafana – Home Assistant

Furtermore, if you are using InfluxDB as the grafana source, you can actually create an HA sensor with the same query as Tom shows in Grafana

With the mini graph or Apex cards you can group by day and show the max for that day.

Example: GitHub - kalkih/mini-graph-card: Minimalistic graph card for Home Assistant Lovelace UI.

(Pedantic note: midnight is the start of the next day.)

You appear to be using Grafana

Thanks! This looks like a influx query. I’m using prometheus, but I think I know what the issue is for me. I’m guessing that the metric is actually recording correctly, but prometheus is still scraping it every 30 seconds. I was able to get close by doing the following:
max_over_time(hass_sensor_state{domain="sensor", entity="sensor.max_rack_cost"}[1d]) but that still wasn’t close, then I found a post on stack overflow that got me even closer: changing the step interval to 1d.

now with max_over_time(hass_sensor_state{domain="sensor", entity="sensor.max_rack_cost"}[24h]) and step:1d I get

however now it just shows 00:00 and not the date. I’ll have to see if I can get this to display the date instead of the time. Either that, or I might have to export the costs to influx as well and then use a query similar to yours.

ok I got it. I ended up using the original metric that records every minute or so:

sum(increase(hass_sensor_monetary_usd{entity="sensor.server_rack_electricity_cost"}[24h]))

I then had to change the min step to 1d, change the time shift back one day 0d-1d,and then override the time field with a custom date:
image

This gives me the graph I was looking for:

Thanks!