How to calculate the daily sum from a sensor

I have a sensor that tracks my baby’s sleep amount, however, I’m unable to do so because the sensor might start at 11 pm and end at 1 am the next day. So I should get a value for 1hr today and 1hr for tomorrow. The thing is the sensor goes off when I log it and i may forget to log the time till the next day hence why I need the template to go off the the Attributes timestamps Start & End. See image

I have no skill in this so I have been trying to use Chat GBT, but none of the examples are working.

Any help would be great

Here are the examples are the ChatGBT results

create a yaml template that collects the sensor.beau_wilder_last_sleep per day where the attributes Start and End on the same day

{% set start = now().date() %}
{% set end = start + timedelta(days=1) %}

utility_meter:
  beau_wilder_sleep_per_day:
    source: sensor.beau_wilder_last_sleep
    cycle: daily
    method: sum
    offset:
      hours: 0
      minutes: 0
      seconds: 0
    duration:
      days: 1
    only_include_interval:
      start: '{{ as_timestamp(start) }}'
      end: '{{ as_timestamp(end) - 1 }}'

create a yaml utility meter that collects the sensor.beau_wilder_last_sleep per day where the attributes Start and End on the same day

utility_meter:
  beau_wilder_sleep_per_day:
    source: sensor.beau_wilder_last_sleep
    cycle: daily
    method: sum
    offset:
      hours: 0
      minutes: 0
      seconds: 0
    duration:
      days: 1
    only_include_interval:
      start: '{{ as_timestamp(now().date()) }}'
      end: '{{ as_timestamp(now().date() + timedelta(days=1)) - 1 }}'

Maybe this will help figuring it out. The variables start and end represent the attributes of your sensor.

{%set start = today_at("23:00") - timedelta( days = 1)  %}
{%set end = today_at("01:15")  %}

{% if start.date() != end.date() %}
  Slept yesterday: {{ today_at("00:00") - start }}
  Slept today: {{ end - today_at("00:00") }}
{% else %}
  Slept today: {{ end - start }}
{% endif %}

This should result in:

Slept yesterday: 1:00:00
Slept today: 1:15:00

Replace the current ‘end’ with {%set end = today_at("23:15") - timedelta( days = 1) %} and see the result if start and end are on the same day. The result:

Slept today: 0:15:00

Thanks, mate, im not sure what this is telling me or how I use this :frowning:

I have this value template which gives me 2.9 for the last state which is a 171/60, however, that’s not the duration of the attribute, its 2hrs and 51 mins… I’m lost and thank you again for helping.

{% set start_time = state_attr('sensor.beau_wilder_last_sleep', 'start') %}
{% set end_time = state_attr('sensor.beau_wilder_last_sleep', 'end') %}
{% set duration_minutes = ((as_timestamp(end_time) - as_timestamp(start_time)) / 60) | round(1) %}
{{ (duration_minutes / 60) | round(1) }}

Not sure why you want what you want, but I think you don;t want to divide it twice.

It works if you divide it only once:

{% set start_time = today_at("23:00:01") - timedelta( days = 1) %}
{% set end_time = today_at("01:15:37") %}
{% set duration_minutes = (as_timestamp(end_time) - as_timestamp(start_time)) %}
{{ (duration_minutes / 60) | round(1) }}

Since I don’t have your sensor, I created my own time values.

You might even want to do it slightly different:

{% set start_time = today_at("23:00:01") - timedelta( days = 1) %}
{% set end_time = today_at("01:15:37") %}
{% set duration_minutes = (end_time - start_time).seconds / 60 %}
{{ duration_minutes }}

Hope this helps. Here you have it converted to minutes in my first example:

{%set start = today_at("23:00:04") - timedelta( days = 1)  %}
{%set end = today_at("01:15")  %}

{% if start.date() != end.date() %}
  Slept yesterday: {{ ((today_at("00:00") - start).seconds / 60) | round }} minutes
  Slept today: {{ ((end - today_at("00:00")).seconds / 60) | round }} minutes
{% else %}
  Slept today: {{ ((end - start).seconds / 60) | round }} minutes
{% endif %}

can you explain when the sensor updates and where it comes from? There’s a number of ways to achieve this and we want to choose the easiest path. Using a template will be painful.

Hey Petro, so im using Buddy Baby and you add database entries for Sleep, feeding etc.
There is a sensor entity for each of these activities. Below is for feeding.


The sensor updates when I add an entry to the Baby Buddy database. See below Database entries.

The potential issue is I can forget to add say a feed session that was for 10pm-11pm until the next morning. If I were to use Utility Meter that feed is counted for the day and not based on the Attributes Start and End.

Does that help?

Does the main state on the entity update when you add it in the other app?

yeah it updates 1 min later with what I add in Buddy baby.
So if I add a feed session and add the volume of 90 MLS, it then updates sensor.beau_wilder_last_feeding main state to 90ml and updates all the attributes for that sensor

If that’s the case, you can just make a template sensor that updates via a trigger. Adding the time to some input helpers and then reset the input helpers at midnight. Or you can attempt to make this into template entities, which would behave similar. I can help you make those if you’re interested.

yeah i have no idea how to do that. So yes any help would be welcomed!

Hey petro, any chance of some help?
I’ll flick ya a 6 pack lol

Sorry, wasn’t available for extended time.

Try this. Keep in mind it will be unknown until you first add some time. It is not retroactive.

template:
- trigger:
  - platform: state
    entity_id: sensor.beau_wilder_last_sleep
    attribute: start
    variables:
      adding: >
        {% set midnight = today_at() %}
        {% set start = trigger.to_state.attributes.start %}
        {% set end = trigger.to_state.attributes.end %}
        {% if start < midnight < end %}
          {{ {'yesterday': (midnight - start).total_seconds(), 'today': (end - midnight).total_seconds() } }}
        {% elif start < midnight and end < midnight %}
          {{ {'yesterday': (end-start).total_seconds(), 'today': 0} }}
        {% else %}
          {{ {'yesterday': 0, 'today': (end-start).total_seconds()} }}
        {% endif %}
  - platform: time
    at: "00:00:00"
    variables:
      adding: false
  sensor:
  - name: Beau Wilder Sleep Yesterday
    unique_id: beau_wilder_sleep_yesterday
    device_class: duration
    unit_of_measurement: seconds
    state: >
      {% if adding %}
        {{ this.state + adding.yesterday if this.state is defined else adding.yesterday }}
      {% else %}
        0
      {% endif %}
  - name: Beau Wilder Sleep Today
    unique_id: beau_wilder_sleep_today
    device_class: duration
    unit_of_measurement: seconds
    state: >
      {% if adding %}
        {{ this.state + adding.today if this.state is defined else adding.today }}
      {% else %}
        0
      {% endif %}

Hey Petro, no worries

So im trying understand this, is this considered a package or is this one big Template?

Ok i added it to my template yaml file. I added a sleep entry to the Baby Buddy app.

The sensors didnt update…

Look for errors in your logs. I can’t test it because I don’t have this.

cant find anything when i search for the new sensor…

Then you definitely have errors in your logs or you didn’t restart when adding the template sensor.

i have restarted a a few times since i added this. Ill keep looking

Just look in your logs, it will have an error. Feel free to say “I don’t know where my logs are” if you don’t know where the logs are.

just to be sure, they are here
My logs are 17gb and 45 gb lol i wondered why my back ups were so large