baby_buddy_homeassistant icon indicating copy to clipboard operation
baby_buddy_homeassistant copied to clipboard

Last feeding entity time rather than amount

Open drax-uk opened this issue 3 years ago • 10 comments

It would make more sense to see the time from the last feed rather than the amount on the last feeding entity. Now the entity shows the feeding amount. One could make a template sensor, take the last feeding end time attribute and calculate the time from the last feed but via the integration would be easier and cleaner to show in HA.

drax-uk avatar Nov 07 '21 17:11 drax-uk

I am thinking the best solution is to create new sensors for ..._last_instant (or something similar) as appropriate, but set entity_registry_enabled_default=False to reduce clutter for most/average user.

jcgoette avatar Nov 11 '21 04:11 jcgoette

I feel so dumb. I spent over an hour trying to figure out why the Last Feeding and Last Sleep always show "unknown" but Last Diaper Change is working. I was going in circles trying to figure out why one sensor was working and the others were not. Now I see that if you don't enter a feeding amount or a sleep time, it won't show any data. Since I still can't get the sleep timer to work in HA, and I'm not logging feeding amounts right now (breast feeding), having HA show the time when these events were logged would be ideal. If there is ANY way to get that working, please let me know!!

five2seven avatar Nov 15 '21 20:11 five2seven

I mean to come back here and post my update a while ago but we're preparing for baby arrival so you know how it goes.

I finally have this working the way I wanted, with the last feeding times, as well as last feeding how long ago. For anyone else wanting these sensors, here's the working code from my config. Screenshot of Sensors

      ##########################################################
      ## BabyBuddy Feedings
      ##########################################################

- platform: rest
  scan_interval: 15
  name: BabyBuddy Feedings
  resource: https://babybuddy.domain.com/api/feedings/?limit=20
  value_template: N/A
  json_attributes:
    - results
  headers:
    Authorization: !secret baby_buddy_token

- platform: template
  sensors:
    bb_last_feeding_time:
      friendly_name: Last Feeding Time
      value_template: >-
        {{ as_timestamp(state_attr('sensor.babybuddy_feedings', 'results')[0]["end"]) | timestamp_custom ("%-I:%M %p", True) }}

    bb_last_feeding_how_long_ago:
      friendly_name: Last Feeding How Long Ago
      value_template: >-
        {% set up_time = as_timestamp(now()) - as_timestamp(state_attr('sensor.babybuddy_feedings', 'results')[0]["end"]) %}
        {% set seconds = (up_time % 60) | int %}
        {% set minutes = (up_time // 60) | int %}
        {% set hours = (minutes // 60) %}
        {% set days = (hours // 24) %}
        {% set weeks = (days // 7) %}
        {% set minutes = (minutes % 60) %}
        {% set hours =  (hours % 24) %}
        {% set days = (days % 7) %}
        {% macro phrase(value, name) %}
                  {%- set value = value %}
                  {%- set end = 's' if value > 1 else '' %}
                  {{- '{} {}{}'.format(value, name, end) if value | int > 0 else '' }}
        {%- endmacro %}
        {% macro phraseSeconds(value) %}
                  {%- set value = value %}
                  {%- set end = 's' if value > 0 else '' %}
                  {{- '{}{}'.format(value, end) if value | int > 0 else '' }}
        {%- endmacro %}
        {% set text = [ phrase(weeks, 'week'), phrase(days, 'day'), phrase(hours, 'hr'), phrase(minutes, 'min') ] | select('!=','') | list | join(', ') %}
        {% set last_comma = text.rfind(',') %}
        {% if last_comma != -1 %}
          {% set text = text[:last_comma] + ' and' + text[last_comma + 1:] %}
        {% endif %}
        {{ text }} ago```

five2seven avatar Dec 23 '21 16:12 five2seven

@five2seven I'll leave this comment up because maybe it will help someone else. But, this is using the REST integration and not baby_buddy_homeassistant integration.

jcgoette avatar Dec 24 '21 13:12 jcgoette

@jcgoette our new baby arrived 2 days ago and I wanted to let you know how great having this integration is. Thank you so much for sharing your work with the community. We're using it to populate a dashboard and it's a big hit with the wife. Helps us keep on top of things. Adding my 2 cents that I was also confused about the sensors initially, the template sensors listed above were closer to what I was expecting and I'll be adding them in. Thanks for leaving the comment up!

luther84 avatar Jan 24 '22 00:01 luther84

I just wanted to drop in to +1 this request. HA makes you jump through hoops to avoid this, and since we're breastfeeding (and thus not tracking amounts) we see that unknown constantly, haha.

xtraorange avatar Aug 11 '22 04:08 xtraorange

Minor detail, but the attributes for the last_feeding and last_pumping are inconsistent on the front-end compared to the last_change sensor. The last_feeding and last_pumping sensors display the full date and time, whereas the last_change sensor, while it is a full date and time, appears as "2 hours ago". I suspect it's a formatting issue between datetime and string, but I have yet to learn the code behind HA integrations. This is in the same boat as the comment @five2seven made regarding humanizing the times.

tango2590 avatar Jan 26 '23 06:01 tango2590

last_feeding for is "unknow" in Home assistant even with attribute data having the time and date. last_change has x hours ago

socbrian avatar Feb 03 '23 18:02 socbrian

I'm dropping in to give a +1 to this. When we were in the hospital I was entering feedings through HA (I didn't expose Baby Buddy to the internet for reasons) and since we were solely breast feeding (not tracking amounts, only time) the "last feeding" was only displaying as unknown and was very frustrating. I ended up just writing it all down with the intention to come home and try to fix it and add the feedings into Baby Buddy manually as I assumed the feedings weren't actually getting added. Have maybe "Last feeding amount" and a "last feeding time" as separate methods could be a decent solution. A single derived string sensor, "Last Feeding", that displays something like "start time, method, amount (displays as '- -' or similar if no amount), duration" could be good. Or simply adding more sensors for each of these items therefore they can be used in automations and displayed on dashboards as the user sees fit.

Had I known this was going to be an issue I would have considered trying to create a PR for this myself but the baby is definitely taking up a lot of time. The solution above using the rest integration to add additional sensors seems like a good temporary in the mean time.

20BBrown14 avatar Jan 28 '24 19:01 20BBrown14

FYI, just found all this and posted in a different topic, but you can use this integration to pull this data out using the same templating shown above because each entity that's pulled in by the integration has a bunch of attributes you can access. So no need to add the rest integration

    last_feeding_method:
      friendly_name: "Last Feeding Method"
      value_template: "{{ states.sensor.babymcd_last_feeding.attributes.method | title }}"
    last_feeding_start_time:
       friendly_name: "Last Feeding Start Time"
       value_template: "{{ as_timestamp(states.sensor.babymcd_last_feeding.attributes.start) | timestamp_custom('%A @ %-I:%M %p')}}"
    last_feeding_stop_time:
      friendly_name: "Last Feeding Stop Time"
      value_template: "{{ as_timestamp(states.sensor.babymcd_last_feeding.attributes.end) | timestamp_custom('%A @ %-I:%M %p') }}"
    last_feeding_duration:
      friendly_name: "Last Feeding Duration"
      value_template: "{{ ((states.sensor.babymcd_last_feeding.attributes.duration.split(':')[0]|int * 60) + (states.sensor.babymcd_last_feeding.attributes.duration.split(':')[1] | int)) }}"
      unit_of_measurement: "minutes"
    last_feeding_type:
      friendly_name: "Last Feeding Type"
      value_template: "{{ states.sensor.babymcd_last_feeding.attributes.type | title }}"
    time_since_last_feeding:
      friendly_name: "Time Since Last Feeding"
      value_template: >-
        {%- set time = as_timestamp(now()) - as_timestamp(states.sensor.babymcd_last_feeding.attributes.start) | int  %}
        {%- set minutes = ((time % 3600) // 60) | int %}
        {%- set minutes = '{} minutes'.format(minutes) if minutes > 0 else '' %}
        {%- set hours = ((time % 86400) // 3600) | int %}
        {%- set hours = '{} hours '.format(hours) if hours > 0 else '' %}
        {%- set days = (time // 86400) | int %}
        {%- set days = '{} days '.format(days) if days > 0 else '' %}
        {{ 'Less than 1 minute' if time < 60 else days + hours + minutes }} ago
    next_feeding_side:
      friendly_name: "Next Feeding Side"
      value_template: >-
        {% if is_state("sensor.last_feeding_method", "Left Breast") %}
        Right Breast
        {% else %}
        Left Breast
        {% endif %}

McDAlexander avatar Feb 02 '24 17:02 McDAlexander