Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Last feeding entity time rather than amount #45

Open
drax-uk opened this issue Nov 7, 2021 · 10 comments
Open

Last feeding entity time rather than amount #45

drax-uk opened this issue Nov 7, 2021 · 10 comments
Labels
enhancement New feature or request

Comments

@drax-uk
Copy link

drax-uk commented Nov 7, 2021

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.

@jcgoette jcgoette added the enhancement New feature or request label Nov 7, 2021
@jcgoette
Copy link
Owner

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.

@five2seven
Copy link

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
Copy link

five2seven commented Dec 23, 2021

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```

@jcgoette
Copy link
Owner

@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.

@luther84
Copy link

@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!

@xtraorange
Copy link

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.

@tango2590
Copy link

tango2590 commented Jan 26, 2023

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.

@socbrian
Copy link

socbrian commented Feb 3, 2023

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

@20BBrown14
Copy link

20BBrown14 commented Jan 28, 2024

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.

@McDAlexander
Copy link

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 %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

9 participants