*The article below contains affiliate links. If you purchase an item after clicking on that link, I will receive a commission. Thank you!
Does your kindergartner ride the bus to school? Would you like to receive a notification to your phone when they arrive? Want to know when they get on the bus at the end of the day? Home Assistant has the ability to do this with a very simple automation. It will also allow you to see when they arrive/leave any zone that you’ve built in to your system. You can also expand this to any device tracker, not just children. Maybe you want to know when your wife goes to Target…..AGAIN.
Prerequisites
This automation requires a few different pieces. You will need to have the mobile app installed on your phone. You will also need to get a device tracker for your child. After reviewing the options out there, I decided that the best way to do this is to actually have a phone for them. You will also need that phone to have a data plan through a cell carrier. That is the tough bit. I have also read about people using the XPLORA brand of kids watches such as the X5 that can integrate into Home Assistant. You can check out that watch on Amazon with this link. Full disclosure, I have not used this product so I’m not sure how well it works.
You will also need to have the zones set up in your server to receive the information on their whereabouts. I have a guide on zones in case you have not done that or need a refresher.
Also, you will need Node-Red installed on your server.
Node-Red Flow
This is the full flow from start to finish. It begins with the child changing zones, then the switch decides if they left a defined zone or entered a defined zone. If they entered a defined zone, there is a template node to grab the zone they entered. If they left a zone, there is a template to grab the zone they left. In both cases, they go back to the notification node to be sent as a push notification. Let’s break these down.
The Events: State Node
The first node for this child tracking flow is the Evenst: State node. This node is triggered by the device tracker changing zones.
Name: As always, I recommend naming every node to know what they do.
Entity: This is where you select the person you want to be tracking. The dropdown after the name defaults to “exact” and that is how we will keep it.
If State: We will actually be leaving all of this as default. The dropdown should say “is” and the field after that should remain blank. This means that every zone change, regardless of what it is, will get sent to the rest of the flow.
This is it for the events: state node. Now this node will trigger the flow every time the person you are tracking changes zones.
The Switch Node
The switch node is used to check if the person you are tracking either left a defined zone, or entered a defined zone. When you leave a zone, the device tracker goes from ZONE to not_home. Not_home is the system’s way of saying “away.” When you enter a new zone you go from not_home to ZONE. So the way we can decide if they entered or left, is by checking where they were before.
Name: This is where you name the node to keep things clean
Property: Property is the information we are trying to track. We want the old state of the device tracker to find out where they were before changing zones. This information is held within the message at msg.data.old_state.state. This is what you will input for the property field so it matches abov.
Switches: Both switches will use the phrase not_home. We need to know if the device was previously not_home, or if it was previously in a zone. The first line has the dropdown as “==” which means that it was not_home before it switched zones. The second line has the dropdown as “!=” which means NOT not_home before it switched zones. The “==” switch will be connected to the “Entering a New Zone” template node and the “!=” switch will be connected to the “Leaving a Zone” template switch.
The Template Nodes
The two template nodes get a little interesting. The template node is used to change or add information that you want sent further down the flow. They will use Jinja2 templating to grab information from the entity that you are tracking. It might sound scary but you can just copy and paste my code and it will work for you. We will start with tracking when a person enters a defined zone.
Entering a New Zone
Name: Again, name your node so that you know what it does when you are looking at your flow.
Property: Payload is the default property and we want to change the payload, so we will keep that as is. The payload is the information that gets sent from one node to the next.
Template: The template is where we will create the message we want sent to our push notification. In the example, you can change “Child” to your child’s name if you would like. You can also add or change words if you prefer. The next part is how we will get the zone using the Jinja2 templating engine. This string grab the new state (aka, new zone) for the device tracker and inserts it.
Child is at {{data.new_state.state}}
Leaving a Zone
Name: Name your node.
Template: Just like the node for entering a zone, we will create the template for leaving a zone. This uses Jinja2 templating to grab the information on the old state (old zone) that the device tracker just left.
Child left {{data.old_state.state}}
Call Service Node
The final node in the flow is the call service node. This is how we will send the push notification about the zone changes to your phone.
Name: This should be second nature by now. Name your node.
Domain: The domain is the service type that you want to use. For push notifications, that would be the “notify” domain.
Service: Service is where you select which phone you wish to receive the notifications.
Data: Data is where we will craft the message to the phone. I like to work with json so use the dropdown to select “{}.” JSON is formatted with pairs for this notification. The title can be whatever title you choose to see on your phone. The message we want to send was actually created in the nodes before this. We used the template nodes to make the message about which zone the person entered or left, so we just want to send that message along. This will use Jinja2 templating like those nodes, and we will use {{payload}} to get the message.
{"message":"{{payload}}","title":"Child Zone Change"}
And that is it! Your automation is done and you’re ready to receive push notifications.
Final Thoughts
This automation is very helpful for tracking young children but it could be used to track anybody you add in to your system. There are some consent issues with tracking adults that you should bear in mind so make sure you are only tracking people who have agreed with it.