Nanit Baby Monitor Integration

rebooting the docker solves the issue with the stream file building (couldnt think of anything more elegant)

So it seems the things to solve are:

  1. How to run the POST to refresh the token
  2. How to use the resulting token in the url (regularly updating it)
  3. How to create a stream from the rtmps url
  4. how to ingest it into HomeAssistant

Hello Jamie,

I came down with this solution: https://gitlab.com/adam.stanek/nanit. It is not exactly the same thing as you are doing, but you can easily fork it and adjust the FFMPEG command to dump it to the file instead. The Auth + Stream URL part is covered.

I havenā€™t got to the Home Assistant yet. Currently only trying to stream to my TV. Btw. you should be able to also use the HLS stream on your dashboard somehow, but it is only supported in Safari AFAIK.

Now I am thinking about exposing metrics like temperature + times between events through MQTT. From there it should be possible to integrate them into HA.

3 Likes

damn, this is excellent, thank you for sharing!

This is great, I even got it working. Now we just need a Nanit Integration for Home Assistant, with camera feed or ā€˜snapshotsā€™ on events. :thinking:

Great work, everyone. I can grab a single frame using ffmpeg -i "rtmps://media-secured.nanit.com/nanit/{{baby_id}}.{{access_token}" -frames:v 1 ~/tmp/test.jpg. One can then display that image in Lovelace I presume.

Personally, Iā€™d love to have it restreamed as a camera to Homekit via the HA bridge.

4 Likes

Actually, right when I wrote that, I realized that HA/Homebridge uses ffmpeg to restream. I got the Homekit camera showing up with this Homebridge config (I havenā€™t fully migrated my setup from HB to HA). Iā€™m sure you can tweak it and have something similar via HA:

        {
            "platform": "Camera-ffmpeg",
            "name": "Camera FFmpeg",
            "cameras": [
                {
                    "name": "Baby Monitor",
                    "manufacturer": "Nanit",
                    "motion": true,
                    "videoConfig": {
                        "source": "-i rtmps://media-secured.nanit.com/nanit/{{baby_id}}.{{access_token}}",
                        "maxWidth": 0,
                        "maxHeight": 0,
                        "maxFPS": 0,
                        "audio": true
                    }
                }
            ]
        }

This is awesome!

3 Likes

Also, this means Nanit video on Apple Watch:

I had one access token expire though, so they might be short lived. Might need some refresh token code.

4 Likes

This is great ā€” I got something similar working last night and want to build a basic HomeBridge plugin with authentication.

Did anyone manage to pull the humidity/temperature from the sensor? It also looks like there are references to web sockets. I imagine those might be used for the events. Did anybody figure that part out already as well?

1 Like

I got it working in Home Assistant btw, Steps:

  • Add camera to your config (make sure ffmpeg is enabled)
    Config:
  - platform: ffmpeg
    input: -i rtmps://media-secured.nanit.com/nanit/{{baby_uid}}.{{accesstoken}}
    name: Nanit

This will add the camera to your home assistant, and you can just use it or include it into your homekit. I am worries about the token that needs to be refreshed. Maybe it just needs a simple component to do that at first, in addition you might want to add things like events etc.

3 Likes

For myself and newbies like me, Iā€™m trying to make sense of everything in this post. Can you confirm that the full steps to get it working on HA are the following?

  1. Install docker

  2. Run the docker container like this:

docker run --rm \
  -e [email protected] \
  -e PASSWORD=XXXXXXXXXXXXX \
  -p 8080:8080 \
  registry.gitlab.com/adam.stanek/nanit:latest

  1. Enable ffmpeg in HA like this: FFmpeg - Home Assistant

  2. Add this to the config:

Have I missed or misunderstood anything?

2 Likes

The docker container is meant to live on its own. It currently cannot be used with homebridge / HA just yet. I will look into it, stay tuned :slight_smile:

1 Like

This is awesome news! I donā€™t have any experience with HomeBridge so I didnā€™t realized it uses FFMPEG under the hood. I think I have a way how to solve it now together with authentication. Before I have written the go app for restreaming I was using nginx-rtmp. It is possible to just republish the RTMP stream that way. It would be already authenticated and you can just pass it to Homebridge! :slight_smile:

I think I have discovered the way how to read the sensor data. Yes, it goes through web sockets. But it sends binary messages using protobuf. I am gonna try to reverse engineer it today. Already managed to get it output some temperature info.

3 Likes

Is there any way to run this container on a Raspberry Pi?

Sorry, not at the moment. But there is no reason for it not being possible. It is just necessary to build the image for ARM platform.

Iā€™ve followed these instructions but Iā€™m getting this error:

Logger: homeassistant.components.stream.worker
Source: components/stream/worker.py:83
Integration: Stream (documentation, issues)
First occurred: 1:27:13 PM (14 occurrences)
Last logged: 1:42:41 PM

Error opening stream rtmps://media-secured.nanit.com/nanit/{{XXX}}.{{XXX}}

Make sure you donā€™t include the {{ & }} in your stream url.

WOW! It works! Thank you so much for your help!!!

So for now I have mapped the homeassistant www folder to the nanit docker.
So the docker is doing the authentication, and outputting the stream to an HA accessible location.

Then for HA Iā€™ve simply added it as an FFMPEG camera :slight_smile:

Success!

I just seem to have trouble with the still image, anyone else having this issue ?