Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sstratoti
Created February 7, 2021 17:59
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sstratoti/14417d2c3882d2415f31b4d949e48643 to your computer and use it in GitHub Desktop.
Save sstratoti/14417d2c3882d2415f31b4d949e48643 to your computer and use it in GitHub Desktop.
BabyBuddy Package
sensor:
- platform: rest
scan_interval: 15
name: BabyBuddy Feedings
resource: https://babybuddy.YOURDOMAIN.com/api/feedings/?limit=20
value_template: N/A
json_attributes:
- results
headers:
Authorization: !secret baby_buddy_token
- platform: rest
name: BabyBuddy Changings
scan_interval: 15
resource: https://babybuddy.YOURDOMAIN.com/api/changes/?limit=20
value_template: N/A
json_attributes:
- results
headers:
Authorization: !secret baby_buddy_token
- platform: rest
name: BabyBuddy Sleep
scan_interval: 15
resource: https://babybuddy.YOURDOMAIN.com/api/sleep/?limit=20
value_template: N/A
json_attributes:
- results
headers:
Authorization: !secret baby_buddy_token
- platform: rest
name: BabyBuddy Temperature
scan_interval: 600
resource: https://babybuddy.YOURDOMAIN.com/api/temperature/?limit=5
value_template: N/A
json_attributes:
- results
headers:
Authorization: !secret baby_buddy_token
- platform: rest
scan_interval: 1
name: BabyBuddy Active Timers
resource: https://babybuddy.YOURDOMAIN.com/api/timers/?limit=10&active=true
value_template: N/A
json_attributes:
- results
headers:
Authorization: !secret baby_buddy_token
- platform: rest
scan_interval: 15
name: BabyBuddy Tummy Times
resource: https://babybuddy.YOURDOMAIN.com/api/tummy-times/?limit=10
value_template: N/A
json_attributes:
- results
headers:
Authorization: !secret baby_buddy_token
- platform: template
sensors:
bb_last_diaper_change_type:
friendly_name: Last Diaper Change Type
value_template: >-
{% if state_attr('sensor.babybuddy_changings', 'results')[0]["wet"] == true and state_attr('sensor.babybuddy_changings', 'results')[0]["solid"] == true %}
Pee and Poop
{% elif state_attr('sensor.babybuddy_changings', 'results')[0]["wet"] == true %}
Pee
{% else %}
Poop
{% endif %}
bb_last_diaper_change_dow:
friendly_name: Last Diaper Change DOW
value_template: >
{{ as_timestamp(state_attr('sensor.babybuddy_changings', 'results')[0]["time"]) | timestamp_custom ("%A", True) }}
bb_last_diaper_change_time:
friendly_name: Last Diaper Change Time
value_template: >
{{ as_timestamp(state_attr('sensor.babybuddy_changings', 'results')[0]["time"]) | timestamp_custom ("%-I:%M %p", True) }}
bb_last_diaper_change_color:
friendly_name: Last Diaper Change Color
value_template: >-
{{ state_attr('sensor.babybuddy_changings', 'results')[0]["color"] }}
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_type:
friendly_name: Last Feeding Type
value_template: >-
{% if state_attr('sensor.babybuddy_feedings', 'results')[0]["amount"] is not none %}
{{ state_attr('sensor.babybuddy_feedings', 'results')[0]["amount"] }}oz of {{ state_attr('sensor.babybuddy_feedings', 'results')[0]["type"] }}
{% else %}
{{ state_attr('sensor.babybuddy_feedings', 'results')[0]["type"] }}
{% endif %}
bb_last_feeding_from:
friendly_name: Last Feeding From
value_template: >-
{{ state_attr('sensor.babybuddy_feedings', 'results')[0]["method"] }}
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
bb_todays_feeding_count:
friendly_name: Todays Feeding Count
value_template: >-
{% set count = namespace(value=0) %}
{% set total = namespace(value=0) %}
{% set start_of_day = as_timestamp(as_timestamp(now())|timestamp_custom ("%Y-%m-%d 00:00:00.1", True)) %}
{% set end_of_day = as_timestamp(as_timestamp(now())|timestamp_custom ("%Y-%m-%d 23:59:59.0", True)) %}
{% for row in state_attr('sensor.babybuddy_feedings', 'results') %}
{% if as_timestamp(row.get('end'))|int > start_of_day|int and as_timestamp(row.get('end'))|int < end_of_day|int %}
{% set count.value = count.value + 1 %}
{% if row.get('amount')|float > 0.0 %}
{% set total.value = total.value + row.get('amount') %}
{% endif %}
{% endif %}
{% endfor %}
{{count.value}} Feedings(s) Today{% if total.value > 0 %} for {{total.value}} total{% endif %}
bb_last_sleep_duration:
friendly_name: Last Sleep Duration
value_template: >-
{% set up_time = as_timestamp(state_attr('sensor.babybuddy_sleep', 'results')[0]["end"]) - as_timestamp(state_attr('sensor.babybuddy_sleep', 'results')[0]["start"]) %}
{% 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 %}
for {{ text }}
bb_last_sleep_end_time:
friendly_name: Last Sleep Time
value_template: >-
{{ as_timestamp(state_attr('sensor.babybuddy_sleep', 'results')[0]["end"]) | timestamp_custom ("%-I:%M %p", True) }}
bb_last_sleep_how_long_ago:
friendly_name: Last Sleep How Long Ago
value_template: >-
{% set up_time = as_timestamp(now()) - as_timestamp(state_attr('sensor.babybuddy_sleep', '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
bb_last_tummytime_duration:
friendly_name: Last TummyTime Duration
value_template: >-
{% set up_time = as_timestamp(state_attr('sensor.babybuddy_tummy_times', 'results')[0]["end"]) - as_timestamp(state_attr('sensor.babybuddy_tummy_times', 'results')[0]["start"]) %}
{% 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'), phraseSeconds(seconds) ] | select('!=','') | list | join(', ') %}
{% set last_comma = text.rfind(',') %}
{% if last_comma != -1 %}
{% set text = text[:last_comma] + ' and' + text[last_comma + 1:] %}
{% endif %}
for {{ text }}
bb_last_tummytime_dow:
friendly_name: Last TummyTime DOW
value_template: >-
{{ as_timestamp(state_attr('sensor.babybuddy_tummy_times', 'results')[0]["end"]) | timestamp_custom ("%A", True) }}
bb_last_tummytime_time:
friendly_name: Last TummyTime Time
value_template: >-
{{ as_timestamp(state_attr('sensor.babybuddy_tummy_times', 'results')[0]["end"]) | timestamp_custom ("%-I:%M %p", True) }}
bb_todays_tummytime_count:
friendly_name: Todays TummyTime Count
value_template: >-
{% set count = namespace(value=0) %}
{% set start_of_day = as_timestamp(as_timestamp(now())|timestamp_custom ("%Y-%m-%d 00:00:00.1", True)) %}
{% set end_of_day = as_timestamp(as_timestamp(now())|timestamp_custom ("%Y-%m-%d 23:59:59.0", True)) %}
{% for row in state_attr('sensor.babybuddy_tummy_times', 'results') %}
{% if as_timestamp(row.get('end'))|int > start_of_day|int and as_timestamp(row.get('end'))|int < end_of_day|int %}
{% set count.value = count.value + 1 %}
{% endif %}
{% endfor %}
{{count.value}} TummyTime(s) Today
bb_timer_status:
friendly_name: Timer Status
value_template: >-
{% if state_attr('sensor.babybuddy_active_timers', 'results')[0]["active"] == true %}
{% set up_time = as_timestamp(now()) - as_timestamp(state_attr('sensor.babybuddy_active_timers', 'results')[0]["start"]) %}
{% 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'), phraseSeconds(seconds) ] | select('!=','') | list | join(', ') %}
{% set last_comma = text.rfind(',') %}
{% if last_comma != -1 %}
{% set text = text[:last_comma] + ' and' + text[last_comma + 1:] %}
{% endif %}
{{ text }}
{% else %}
NA
{% endif %}
input_text:
bb_command:
name: BabyBuddy Command
max: 255
input_boolean:
bb_timer_on:
name: BabyBuddy Timer
bb_log_pee:
name: BabyBuddy Log Pee
bb_log_pee_and_poop:
name: BabyBuddy Log Pee and Poop
bb_log_pee_and_green_poop:
name: BabyBuddy Log Pee and Green Poop
bb_log_pee_and_yellow_poop:
name: BabyBuddy Log Pee and Yellow Poop
bb_log_pee_and_brown_poop:
name: BabyBuddy Log Pee and Brown Poop
bb_log_pee_and_black_poop:
name: BabyBuddy Log Pee and Black Poop
bb_log_poop:
name: BabyBuddy Log Poop
bb_log_green_poop:
name: BabyBuddy Log Green Poop
bb_log_yellow_poop:
name: BabyBuddy and Yellow Poop
bb_log_brown_poop:
name: BabyBuddy Log Brown Poop
bb_log_black_poop:
name: BabyBuddy Log Black Poop
homeassistant:
customize:
input_boolean.bb_timer_on:
friendly_name: "Baby Timer"
@poldim
Copy link

poldim commented Aug 13, 2023

Thanks for this as it's been very helpful in displaying some of the data from BabyBuddy.
Could you help figure out how to extract total naps for today? I'm struggling as the json data is an hour timeperiod rather than a number such as "duration":"01:12:00". I think the template processes this as a string and therefore can sum multiple strings. I'd like to display the value as total hours of naps today as #.#

    bb0_todays_naps_total:
      friendly_name: Todays Naps Total
      value_template: >-
        {% set total = namespace(value=0) %}
        {% set start_of_day = as_timestamp(as_timestamp(now())|timestamp_custom ("%Y-%m-%d 00:00:00.1", True)) %}
        {% set end_of_day = as_timestamp(as_timestamp(now())|timestamp_custom ("%Y-%m-%d 23:59:59.0", True)) %}
        {% for row in state_attr('sensor.bb0_sleep', 'results') %}
            {% if as_timestamp(row.get('start'))|int > start_of_day|int and as_timestamp(row.get('start'))|int < end_of_day|int  %}
                {% if (row.get('duration')|float > 0.0 and row.get('nap')=="true") %}
                  {% set total.value = total.value + row.get('duration') %}
                {% endif %}
            {% endif %} 
        {% endfor %}
        {{total.value}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment