Tag: script

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

Bash script to clean movies name


The issue when you download movies online, is that most of the time the file name is going to be complete trash , filled with useless info like the team that made the rip or the website from where the movie was downloaded.
This is very annoying when you sort by name and the website made the idiotic decision to put their name in front of the actual film name.

for example here a typical movie name
[ Torrent9.info ] Dont.Think.Twice.2016.FRENCH.WEBRip.XviD-NEWCiNE.avi
the actual useful data that i wish to keep is Dont.Think.Twice.2016.avi

for this purpose i’ve developped a small bash script that will list all the files in a chosen directory then , if necessary , will rename the file.

this is not a very efficient script
but it’s fulfill it’s purpose and very rarely encounter a filename that he is unable to clean.

BASH:check if a number is even or odd


In a bash script you might need to check if a var is even or odd , 

in my case im going to use a if to make that check

You can use this simple code to make that check and display the result.

the result will be : 

BASH : IP validation 

if you have a list of ip to validate ,
you can use this little bash script ,
this will open your IP list ,
then test if all the 4 numbers that make a standard IP.are beeten 0 and 255

if the ip is valid then the script will return valid , else it will return : not valid

after execution , you will get the following result :

Bash : Delete some characters from a file with TR

in this little example of usage of the command tr
we want to clean a file so the values contained in it can be used
Example File

In my example i wish to only keep the value and deleting the character < and >

the tr command used with the -d (delete) option will allow you to delete the list of characters contained between the quotes.

You will obtain the following result

BASH : Delete the last X chars from a string

sed delete last x chars

When you scrap data in HTML , it’s common , depending on the quality of you regex to end up with a string with some useless character at the end (Or the Beginning),

If you string looks like that :

My useful data</a> <

You can use the command head to drop the X last character of your string ,
So in my Case , to Keep only the string My useful data and drop the </a> <

I’m going to pipe my string to

This will delete the last 6 chars of my string , giving the data that i need later in my script

but in some implementation of the head command this will not work ,

you can use an alternative with sed

This will delete the last 4 characters of your line ,  or alternatively