Streaming and casting youtube and more .. via Raspberry pi with Gotubecast and KODI TubeCast

How difficult is it to transfer youtube music or videos via your phone or other portable, mobile devices to your Raspberry pi? What if they’re not mobile? Is it easy to manage media content via Windows or Linux PC?

It turns out that it is not difficult. I want to consider installing and configuring several applications on your raspberry with which we can do this.

How it works!

We will assume that streaming in this situation is the process of transferring some kind of media content that is located somewhere on sites on the Internet. Or, say, on a local server with our media library to play it on Raspberry pi. And casting, when we “transfer control” of the media content of an external device to our TV. Or rather, on our Raspberry pi. We connect it to a TV or music center. In this situation, through the mobile, by launching your youtube application, we redirect the output of video or music through our raspberry to the TV. Remember to connect your Raspberry pi to the TV via HDMI.

There are two options to consider these applications. The first option: we have configured everything in our ViaMyBox project. You can not bother with various nuances of configuring linux in this version of raspbian buster and download the distribution or directly with the Raspberry pi from the repository:

cd /home/pi
git clone

And run script -> Home Theatre -> Cast youtube :

sudo /home/pi/viamybox/scripts/

Or set up everything yourself as only you like, following the examples below. I will not compare and describe in detail the functionality of these applications in this article, but just share the experience of configuring and installing them and the nuances that I encountered.

Welcome! Gotubecast!

Project page:

Gotubecast is a small program to make your own TV player on YouTube. In other words, by running gotubecast on the Raspberry pi, we can connect to the Raspberry pi via the phone’s youtube app or other devices and launch a youtube video on the TV.

But in order for this to happen, it will take several actions to do this.
This program is written in Go. Therefore, you need to install it:

cd /home/pi
#download it
sudo tar -C /usr/local/ -xvzf go1.13.7.linux-armv6l.tar.gz
#creating a folder for our project
mkdir -p projects/{src,pkg,bin}
#edit our file
nano /home/pi/.profile

And write our environment variables to it:

export PATH=$PATH:/usr/local/go/bin
export GOBIN="$HOME/projects/bin"
export GOPATH="$HOME/projects/src"
export GOROOT="/usr/local/go"

Checking this package:

go version

Then install gotubecast:

cd /home/pi
go get

In the project, which is located in our created folder /home/pi/projects/src/src/, the author prudently wrote the script for raspberry, which simplifies the launch of gotubecast.
We launch it and now the magic is close ..

cd /home/pi/projects/src/src/

After running the script, we will see a string, something like
Your pairing code: 901-900-123-183
This will allow us to find our TV via telephone. Then, we go to the youtube application on the phone and through the “account” icon, then “settings -> watch TV”, and enter our code. After that, an icon appears to view youtube through our Raspberry pi.

But the TV can be lost if the raspberry pi is not connected to the google app for a long time. And so, in addition to pairing code, you can link your TV “forever” to your google app using Screen Id.
Let’s generate it on our Raspberry pi:

cat generate_screen_id

And add it to the file:

nano /home/pi/projects/src/src/

To a variable on the next line :
export SCREEN_ID=”aauaju8Example5539vbb”

To run all this immediately at start, make yourself a small service daemon in the folder:

sudo nano /lib/systemd/system/gotubecast.service

and in it:

Description=Cast youtube

ExecStart=/bin/bash /home/pi/projects/src/src/


Next, initialize it in the system :

systemctl enable gotubecast.service

And it will start when the system starts. As it turned out, the start of the daemon in the absence of a network falls. More precisely, does not find the network. You can use sudo make the network wait before services start. But in General, it is more pleasant when loading raspbian is fast). So let the script check the network itself. Add a waiting check to the file

while ! ping -q -c 1 >/dev/null ; do sleep 1; done

Copy gotubecast to the folder for our executable files:

sudo cp /home/pi/projects/bin/gotubecast /usr/bin/

And everything is ready)

And yes, if you are not a linux fan and your eyes are rippling with commands, I have automated the entire process described above in the script, and it looks like this:


To use it download the project via the website or via github:

git clone

And run the script /home/pi/viamybox/scripts/ and go to the “Home theater” menu:

cd /home/pi
git clone
sudo /home/pi/viamybox/scripts/

Tested on Raspbian Buster OS.

Launching Youtube via KODI

If you have KODI on your raspberry, it is much easier to launch a youtube video through it. Actually, you just need to install the TubeCast plugin. On the plus side, this Kodi plugin does not require initial configuration and the plugin perfectly “sees” our Raspberry pi without entering the pairing code manually.

And after installing the plugin, you will immediately see the cast icon on your android youtube application or on the youtube page in the browser:

From the description of the plugin it follows that this is an implementation of the Cast V1 protocol for Youtube mobile applications. In turn, this protocol is based on the DIAL protocol via SSDP. This technology allows you to connect to your youtube channel as an external service via the Internet. Thus, it allows you to find your device with KODI on board. Moreover, not even being on the same network with him. Just update your pairing code on the streaming device. As a result, we connect two devices and transfer control through one device to another, but not the media content itself. Therefore, both the transmitting and receiving device (in this case, our Raspberry pi) must be on the Internet.

I hope it was interesting. If so, write comments!
In the next part, I would like to talk about how to play youtube and other media content from the console terminal with the mps-youtube and straw-viewer and raspicast media combines.


Connecting the motion sensor, alarm and video recording in Home Assistant on Raspberry pi

I’d like to tell you about my little experience of working with Home Assistant (hereinafter – HA) on Raspberry pi. Further I’ll speak about connecting the video registration functionality, the motion sensor and, accordingly, the home security function with receiving photos by e-mail  in case of triggering “security”.

Of course, one of the reasons is the desire to understand from your reviews what I “did wrong” when implementing this scenario.

There are three ways to configure the above functionality: “with no problem”,” short” and “independent”. In the first case, you can download the developed image from the site 

for your Raspberry pi, and you can see how everything looks in the already configured form. In the second case, you can download the zip from the site or from the github project, install it and then install the HA docker image through the utility (sudo to see how everything is set up.  And finally, the third way is to set up “everything in your own way”: take something useful from the site or githab project, or from this tutorial. Links to all this you will find at the end of the article.

Assume that you already have a running Home Assistant (HA). In this article, we will only consider the aspects of creating yaml configurations and describing the sequence of rules and conditions that lead to a specific action. In our case, the action is the connection of the “security” mode and the triggering of the script, which starts taking photos within 5 seconds after the motion sensor is activated. And as a result, Home Assistant sends the captured photos by mail. I’m not an expert on yaml or creating configurations for Home Assistant, but following the working examples, I got a working configuration that I want to share.

All the files that we will contact, I will lay out at the end of the article for more convenient consideration. I’m not going to touch on how video recording scripts work in bash or python in this example. We will speak about Home Assistant only. But if you have any questions – you are Welcome!

In the end, I have everything looks like this (red highlighted what we will consider in the article):

Naturally, I use Raspberry pi as a platform. And my path to the configuration files is /usr/share/hassio/homeassistant/ . The path to your configuration files may differ from my path. At that time, I had Home Assistant 0.101.3. We will be interested in the configuration files in this directory: configuration.yaml and automation.yaml.

After each change to these files, it is important to remember that you will need to check the configuration after our changes for possible errors made in it. This is done in the Home Assistant dashboard -> Configuration -> Server Controls -> Check Config. And then, in the same place, we do Reload Automations, and Reload Scripts, if the check was successful. And, if in doubt, Server  Management restart the configuration updates exactly in the same tab.

The HC-SR501 motion sensor is connected to the Raspberry pi GPIO bus. It is necessary to connect the three contact wires to the GPIO correctly, and we will be able to use our sensor in action. The sensor has three contacts: power gcc (+), zero gnd ( -), control contact (data). After reading the description of GPIO, I connected them to my raspberry as follows. I connected the HC-SR501 to the GPIO connector: pir#2 – 5.5 vvcc; pin#26 (13 row) – data contact and pin#6-GND ( – ) of the sensor.

As a photo source, I use either a usb camera or a csi camera in conjunction with mjpg-streamer. We will not describe how to install and configure mjpg-streamer on Raspberry pi in this post. There are many examples of quick installation in the Internet. However, I want to say that I use data from this project  The installation of this module is also described in details there. Or use any method that is convenient for you to get photos and videos. After all, it’s up to you to decide which bash script to attach to HA.

Let’s describe our sensor now in configuration.yml, let’s say, via the command line:

sudo nano /usr/share/hassio/homeassistant/configuration.yaml

although using notepad++ with winscp can be more convenient..

Let’s describe the sequence of settings for HC-SR501 as follows:

  - platform: rpi_gpio
    #name: HC-SR501
      7: Sensor HC-SR501
    invert_logic: false

It should be noted that here an important point is played by the settings in the lines that describe the connection to the control (data) contacts of the sensors:      pin#7


      7: Sensor HC-SR501

Home Assistant has a built-in tool for enabling the home security feature. Let’s describe it in our configuration.yaml, using the description:

  - platform: manual
    name: Home Alarm
    pending_time: 60
     delay time 40
    pending_time: 0
    code: 1234

This setting means that we have 60 seconds to leave the house (after this time, the alarm will turn on), and 40 seconds to turn it off (with the password 1234) when we return home. For some reason, you always leave longer than you come.)

We will now describe the mechanism for enabling and disabling photography when the motion sensor is triggered in our configuration.yaml (more information about the switch – command_line platform can be found here

 - platform: command_line
        friendly_name: 'Record motion timelapse video'
        command_on: 'curl http://localhost/start_mjpgstrm.php  && curl http://localhost//rec-motion-mjpg.php'
        command_off: 'curl http://localhost/stop_mjpgstrm.php && curl http://localhost/rec-motion-mjpg-stop.php'

Here we have linked the command_on enable and command_off disable commands to our scripts that control camera recording. In this case, when you enable command_on, 2 scripts are executed sequentially. This starts mjpg-streamer and starts recording on the motion sensor. Access to bash scripts goes through the php file of the working site on the Raspberry pi. To do this, I set up nginx and web access, and when accessing through the browser as http://<ip address of your Raspberry pi>/start_mjpgstrm.php  should execute our php script.

We do not consider setting up web access in this article. Clearly, this is not secure, but due to the fact that I have HA on docker, I am faced with the fact that I have an isolated docker environment. And the “external world” of the OS I can see by accessing my site via php. There are probably a bunch of right solutions from docker or HA guru. Please, write, it would be interesting to know! 

I will leave the listing of these scripts under the article, and we will not dwell on them. In this article, I want to follow only the formation of a sequence of actions in Home Assistant.

All this is placed in the start_stop_motion_rec_timelapse object, which will be our visual switch, with which we will manage the recording of photos when the motion sensor is triggered.

We will be able to visualize these objects created by us in the Home Assistant- > Overview tab by switching the activation of Configure UI configuration mode in the upper right corner

confugure ui

Pre-reboot the HA in the browser in the “Configuration -> Server Control” tab to pick up our configuration.yaml.

Next, select the yellow plus at the bottom of the browser window, click the Entities card and link our created objects to the maps.

For our sensor, select the sensor card. It will look like this:

As you can see in the picture, the second entity field contains the above-described switch: switch.start_stop_motion_rec_timelapse.

It allows us to turn our motion sensor recording on and off, regardless of whether the “home security” function is enabled. And, in general, it should already work after all the above described actions.

 And, accordingly, we add the alarm panel map:

There is another interesting point here. I use Home Assistant as a docker container. In this regard, the path to our script will differ from the actual path to the script file. After all, the file structure inside the container is virtualized and linked to the actual file structure via docker mounted volumes. This looks, for example, like this: Path inside the container: /config/scripts/ Path inside the OS: /usr/share/hassio/homeassiatnt/scripts. So look at the settings of your container, how these volumes are configured in the Binds field. If you have HA there.

sudo docker inspect homeassistant|less

When the motion sensor is triggered, at the moment when the alarm is activated: I want that in addition to the fact that we start taking quick photos (timelapsesnapshots), I would receive a warning in the mail and a picture. I parallelized the processes. In the first case, photo – logging (timelapsesnapshots) occurs within a few seconds after the sensor is triggered. To do this, I run mjpg-streamer curl http://localhost/start_mjpgstrm.php , start php: сurl http://localhost/rec-motion-mjpg.php . It, in turn, runs python script All described files and bundles are available at the end of the article by the link.  And another script takes a picture that I send in an email. I describe these scripts in our configuration.yml this way:

#start the script that generates a snapshot for the mailing list
  take_snapshot_webcam: '/config/scripts/'
#starting our mjpg-streamer 
  start_mgpg_streamer: 'curl http://localhost/start_mjpgstrm.php'
#stopping mjpg-streamer
  stop_mgpg_streamer: 'curl http://localhost/stop_mjpgstrm.php'
#start the script that generates snapshots for 5 seconds
  start_motion_rec: 'curl http://localhost/rec-motion-mjpg.php'
#stop it
  stop_motion_rec: 'curl http://localhost/rec-motion-mjpg-stop.php'

In the same configuration.yml describing our object for mailing:

  - name: ha_sendmail
    platform: smtp
#if mail is on gmail
    port: 587
    timeout: 15
#from whom we send
    encryption: starttls
    password: passwd
#to whom we send (you can use one box and send it to yourself, why not)
    sender_name: My Home Assistant

Important point! In order for our HA to be able to connect and send emails (the sender field), we must allow our HA service to use this mailbox on gmail. How to do this-link here:

And further, we describe the automation mechanism itself when enabling the home_alarm alarm in automation.yaml:

#first action bundle (alias “triggered by sensor if security is enabled”
- alias: 'Trigger alarm while armed away'
#alias is triggered when the sensor is in the “on” position
    - platform: state
      entity_id: binary_sensor.sensor_hc_sr501
      to: 'on'
#provided that the security mode is enabled in the absence of people at home “armed away”
    - condition: state
      entity_id: alarm_control_panel.home_alarm
      state: armed_away
#and then performs the action 
#Running mjpg-streamer (if disabled)
    - service: shell_command.start_mgpg_streamer
#Running a script that records from the camera
    - service: shell_command.start_motion_rec
#Change the visualization of our "Home security" card on the HA panel to the “alarm triggered” position
    - service: alarm_control_panel.alarm_trigger
      entity_id: alarm_control_panel.home_alarm

The next set of conditions and actions is to send a console message when “security” is turned off and turn off our motion recording scripts.

- alias: 'Send notification when alarm is Disarmed'
    - platform: state
      entity_id: alarm_control_panel.home_alarm
      to: 'disarmed'
    - service: shell_command.stop_mgpg_streamer
    - service: shell_command.stop_motion_rec
    - service: persistent_notification.create
       message: The alarm is Disarmed at {{ states('sensor.date_time') }}"

And finally, the third link-send an email with a photo:

- alias: 'Send notification when alarm triggered'
   - platform: state
     entity_id: alarm_control_panel.home_alarm
     to: 'triggered'
    - service: persistent_notification.create
       message: Notification when alarm triggered. Motion sensor HC-SR501 detected.
    - delay:
       seconds: 4
#run our script for creating a single script
    - service: script.webcam_snapshot
#and refer to the described object in configuration.yaml: notify.ha_sendmail
    - service: notify.ha_sendmail
        title: 'Intruder alert'
        message: '{{now().strftime("%H:%M %Y-%m-%d")}}:Notification when alarm triggered. Motion sensor HC-SR501 detected.'
#link to the script already made by the script.webcam_snapshot
              - /config/camera/snapshot.jpg

Remember that in yaml files, line markup is important, and space characters before commands play an important role in forming code blocks and its structure. Check all changes to your yaml via Home Assistant (hereinafter HA) Configuration -> Server Controls -> Check Config

It seems to be all. And your Raspberry pi turns into something elegant!..)

Here it is working HA automation, configured with your own hands! Be sure to write me everything you think about it!

And, if after all you read to the end, here are the promised links:

Image and zip:

Github project:


Configuration files are described here: configuration.yaml, automation.yaml,, rec-motion-mjpg.php,