Category: Networking

Using the REST api to read sensors on Home assistant

During one of my little project of making timelapses videos with some IP cameras, I had the following problem : How do you stop capturing new images at night when the camera see nothing,
After all , the sun rise and sun set time are changing everyday ! and i didn’t see a simple way to calculate this.
then i remembered the sun.sun integration in Home assistant.

If i had a simple way to query this integration in my script , i could very simply stop my script when the sun was no longer present in the sky !

The Home assistant REST API

In any home assistant installation there is access to a REST API that lets you do a lot a things , but in my case, I don’t want much , I just want to know if the sun is above or below the horizon.

  1. Get a Authorization: Bearer token
    you first have to generate a token to authenticate your request . You have to go to your user section , this is the circle a the bottom of the toolbar, then at the bottom of the page , you can create a long term token ,
    Please take note of this token because Home assitant can only display it one time , if you loose it , you must recreate an other one.
  2. Then in my script i can use this
  3. it will return either above_horizon or below_horizon, I can then use this in my script to stop the capture when it’s below_horizon

I used jq filter the json result , but , if you can’t or don’t want to install it , you can replace it with this simple awk