jcgoette / baby_buddy_homeassistant

This custom integration provides sensors for Baby Buddy API endpoints.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Last feeding entity time rather than amount

drax-uk opened this issue · comments

commented

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.

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.

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

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

commented

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

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.

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.

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