inconsequentia

Home Assistant

Posted at — Sep 15, 2020

Components and information for the Home Assistant home automation system.

Snoo custom component

A custom componnent to monitor the Snoo Smart Sleeper bassinet. This uses the HTTP API to get current state of the Snoo. Happy Baby use an entirely separate PubNub mechanism to send commands to the Snoo, which hasn’t been implemented in this component.

The component repository is at https://bitbucket.org/pjhardy/homeassistant-snoo/src/master/

The unfortunate reality of open source code for things like the Snoo is that contributors run low on motivation to continue working on the code once their child hits about six months, and have precious little free time because they have a six month old kid to deal with.

As of August 2020 our child has graduated to a big boy cot, and I no longer own a Snoo. PRs for the component will be gratefully received and reviewed when I get to them, but otherwise I’m not planning on doing any other work. If you like this component, please do consider forking it.

Interacting with Baby Buddy

Baby Buddy is an open source tracker to let caregivers track bits and pieces about their baby’s activities to better learn and predict their needs.

I don’t (yet) have a custom component for Baby Buddy. But I am making heavy use of the Home Assistant RESTful Command integration to send commands to Baby Buddy from HA. I haven’t yet found a good reason to pull data from BB to HA, so I haven’t written anything to do it, sorry.

Authenticating

I use an entry in Home Assistant’s secrets storage to store auth details for Baby Buddy. The BB API key can be found on the settings page after logging in. Take that key and store it like this:

babybuddy_api_key: "Token <paste API key here>"

Baby ID

All of my examples include this in the payload to identify which child the activity relates to:

{"child": 2}

That will probably need to be altered for your setup. Browsing to https://babybuddy/api/children/ will list all of the children defined in your BB database, with their associated IDs. Make sure you substitute the correct ID in the example code below.

Logging nappy changes

I realised that there are two main types of nappy change. It’ll be wet, or it’ll be wet and have a nice healthy green solid. Creating commands for these two handle > 99.9% of changes for us. So I put these rest_commands in configuration.yaml:

rest_command:
  babybuddy_log_wee:
    url: https://babybuddy/api/changes/
    method: POST
    content_type: application/json
    headers:
      Authorization: !secret babybuddy_api_key
    payload: '{"child": 2, "time": "{{ now() }}", "wet": true, "solid": false}'
  babybuddy_log_poop:
    url: https://babybuddy/api/changes/
    method: POST
    content_type: application/json
    headers:
      Authorization: !secret babybuddy_api_key
    payload: '{"child": 2, "time": "{{ now() }}", "wet": true, "solid": true, "color": "green" }'

Calling one of these commands will log the appropriate change in BB with the time of the change set to the current time. For example, here’s an automation I use for an Ikea Tradfri remote to log changes:

automation:
  - id: Log wee
    alias: Log wee
    trigger:
      platform: state
      entity_id: sensor.babys_room_tradfri_remote_action
      to: 'arrow_left_click'
    action:
      - service: rest_command.babybuddy_log_wee

Logging sleep and tummy time

These are more complicated, because they require a start and end time. Right now, I’m using input_text entities to track the start time in HA. First define the input_texts:

input_text:
  nap_last_start_time:
    name: Nap last start time
  tummy_time_last_start_time:
    name: Tummy time last start time

The next part is a couple of new rest_commands to post sleep and tummy time to BB:

rest_command:
  babybuddy_log_sleep:
    url: https://babybuddy/api/sleep/
    method: POST
    content_type: application/json
    headers:
      Authorization: !secret babybuddy_api_key
    payload: '{"child": 2, "start": "{{ start }}", "end": "{{ end }}"}'
  babybuddy_log_tummytime:
    url: https://babybuddy/api/sleep/
    method: POST
    content_type: application/json
    headers:
      Authorization: !secret babybuddy_api_key
    payload: '{"child": 2, "start": "{{ start }}", "end": "{{ end }}"}'

The final step is setting up a few scripts. One to call at the start of the activity. It will update the appropriate input_text with the start time. Then another one to call at the end of the activity. It will gather the start and end times, and feed them to the appropriate rest_command.

This is what the nap scripts look like. The tummy time ones are identical, just modifying the entity IDs.

script:
  napstart:
    alias: nap start
    description: "Start a nap timer"
    sequence:
      - service: input_text.set_value
        data_template:
          entity_id: input_text.nap_last_start_time
          value: "{{ now() }}"
  napend:
    alias: nap end
    description: "End a nap timer, and log it in babybuddy"
    sequence:
      - service: system_log.write
        data_template:
          level: warning
          message: "Nap timer logging start {{ states('input_text.nap_last_start_time') }}, end {{ now() }}"
      - service: rest_command.babybuddy_log_sleep
        data_template:
          start: "{{ states('input_text.nap_last_start_time') }}"
          end: "{{ now() }}"

At the start of a sleep cycle, an automation should call script.napstart. And then, at the end of a sleep cycle, an automation should call script.napend, and the sleep will be logged in BB.

Exposing nappy changes to Google Assistant

The Google Assistant integration can’t call automations directly. But it can call scripts. So I’ve added two very small scripts for the nappy change automations:

script:
  logpee:
    alias: Log pee
    description: "Log a wet nappy in babybuddy"
    sequence:
      - service: rest_command.babybuddy_log_wee
  logpoop:
    alias: Log poop
    description: "Log a wet and dirty nappy in babybuddy"
    sequence:
      - service: rest_command.babybuddy_log_poop

This means I’m able to log nappy changes in Baby Buddy by giving commands to my Google Home. By setting up Routines in the Google Home app, this can be as simple as saying “OK Google, wet nappy”.