Adding school morning routine for my son via Homeassistant
Tags: homeassistant, smarthome, homeautomation, iot
Published: 2023-03-05 12:36:57

---
title: Adding school morning routine for my son via Homeassistant
published: true
date: 2023-03-05 12:40:52 GMT
tags: Homeassistant, Smarthome, Homeautomation, IoT
canonical_url: https://blog.bajonczak.com/adding-local-transit-into-my-homeassistant/
---

 ![Adding school morning routine for my son via Homeassistant](https://images.unsplash.com/photo-1618846926878-2c967e11b308?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDR8fFNjaG9vbGJ1c3xlbnwwfHx8fDE2NzQ0NTI5NTY&ixlib=rb-4.0.3&q=80&w=2000)

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.

![Adding school morning routine for my son via Homeassistant](https://blog.bajonczak.com/content/images/2023/01/image-10.png)

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.

![Adding school morning routine for my son via Homeassistant](https://blog.bajonczak.com/content/images/2023/01/image-11.png)

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:

![Adding school morning routine for my son via Homeassistant](https://blog.bajonczak.com/content/images/2023/01/image-12.png)

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

{% embed https://gist.github.com/SBajonczak/c3ef621f5a6dd6e420dfa97b8f38373c.js %}

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

{% embed https://gist.github.com/SBajonczak/7b29a0b418570f509cc39a9981ccad17.js %}

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:

{% embed https://gist.github.com/SBajonczak/baa9d9282d3189b00078aadf1a21a59e.js %}

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 : Amazon.de: Baumarkt]
(https://www.amazon.de/Intertechno-YWT-8500-Funk-Wandsender/dp/B000W3XHFA/ref=sr_1_31?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&crid=16QJXL6QY4C15&keywords=funkschalter+technotrend&qid=1675600106&sprefix=funkschalter+technotrend%2Caps%2C91&sr=8-31)

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 s.th. 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:

{% embed https://gist.github.com/SBajonczak/356d06111bbfb8a7bbba87479349783f.js %}

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.

# Conclusion

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.