Photo by Benjamin Wedemeyer / Unsplash

Adding school morning routine for my son via Homeassistant

Homeassistant Feb 5, 2023

Every morning, we have a more extensive discussion about when the next bus comes when school starts, and so on.

So for one of them, I wanted to create an automatism. My idea is, to push a button, then it will play on Alexa a nice sound like

"Hey wake up! Your bus will depart in 20 minutes"

Instead of yelling at him ;)

Starting point

First, we need a button, so I decided to use a button or switch. For this, I decided to take a wall switch that I have in my storage boxes laying around. After integrating it into my Homeassistant via FHEM (not part of this blog entry). I want to act on a signal from this switch.

Getting the transit information

There is no general information database that hosts the data worldwide (afaik). Instead of using this, I will use the local one. This is called Bogestra, they have a own portal to get the information I need.

So first of all, they provide a portal to enter my start point and my target.

I type in the data, and after that, the results will be displayed in the table below. But instead of using a complex parser, I look at what is working behind it. I hit F12 to open the developer tools and look at which calls are made when I hit the red search-Button.

Uh, that was simple. There is one call that will be done, so I copy and paste the call and try to execute it in a separate tab. So this will result me a JSON output:

That sounds interesting. So when we format the JSON output I figure out that there will be the results that I need:

So now we have structured JSON data, that I can use.

Generate a HA sensor

The next step is to store the data from a rest call in a sensor. For this, I will use the following definition

In the first line, I define the interval to fetch in minutes. In the next line, I will set the URL that I want to fetch. The next part is the definition of the sensor itself.

I name the sensor "Departure" it will take the information from the JSON Path "$.routes[0].partialRoutes[0].points[0]" so the first route result, then the first element from the partial routes and the first point. The value template is only static value because the sensor needs this. The next interesting part is the json_attributes. Here I will list the attributes that will automatically be mapped from the current JSON object. Especially for this, the JSON-Object will look like this:

So for example the value from "timeScheduled" will be assigned to the attribute "timeScheduled" on the sensor.

Where is the trigger?

The next question is when will it trigger? I thought to use a scheduled trigger, but I want to remember him manually. So I use a button like this:

Intertechno YWT-8500 Funk-Wandsender : Baumarkt
Intertechno YWT-8500 Funk-Wandsender : Baumarkt

I use several of these switches in my house together with the FHEM software. I have integrated it over FEHM into the homeassistant instance (via MQTT).  You can use any device if you want, but I don't want to use my smartphone or other devices like light sensors or else. Especially for the easy-to-use factor, so that my wife can handle also it.  

Now when the data is fetched by Homeassistant, and we already can use a switch, let's put them all together.

Create an automation

So the easiest way to create an automated process in Homeassistant is, to create an automation. For this, I will create a simple Automatic workflow. Let me show you some code:

Let me explain it a little bit the first line will define the trigger to act don't. In my case, I use the wall switch (as explained above) as a trigger. Every time when the switch was pushed, It will get a signal that will execute the flow.  I do not use any condition so that I act on any state of the switch.

The next important part, are the defined actions, in my case, it is only one action. I use as an output my Alexa device, because Homaeassistant has a great integration for this, that I can send a test over to my Alexa or some Alexa (in case of a multiroom), and he will do a text-to-speech for me.

In this actual case, it will say "Achtung! Aufstehen! Dein Bus Kommt in 10 Minuten" In, English it will be like "Hey! Wake up! Your bus will arrive in 10 minutes!".

You will notice the formula in the text, this will take our fetched departure time from the sensor and calculate the difference to the actual time in minutes.  Alexa TTS (text to speech) then will take this value and put it together in the sentence then.


For some reason, it started when we want to remember our son every time the bus will arrive shortly. But we got tired to remember and yelling when the bus arrives shortly. So I decided to use a switch to create an action that will execute a routine to remember him.    

This took some stress from us and we must not look every time on the timetable for the bus and calculate in the morning the minutes. It will now be done with a push of a button very easily.

The hardest step was, that we must fetch the departure data from the webpage, but fortunately, I have luck and there was a nice webapi for this. So that I can fetch the required data very simply.  

Of course, it is a little bit geeky, but I figured out that a Smart home is not only for keeping lights on or of.