Python Django API for controlling a Raspberry Pi monitor from HA

Hey everyone! Wanted to post this to the forum just in case someone is ever looking for something similar. It solved my problem so I am sure it will solve someone else’s as well!

I found myself saying “Why does the top monitor (being used to display graphs/cameras/ect) need to be on all the time? Why can’t I control it based on motion?” So that is exactly what I did. I created a Python Django API that allows you to control the state of the monitor (attached to a raspberry pi) from HA using a motion sensor as the trigger. When I am not in my office, the screen simply turns off.

I posted my code for the API on GitHub: https://github.com/lambro690/hass-screenapi if you would like to check it out. The only thing I don’t cover is creating the systemd files. You will need to find a way to run this on boot, I just created a screenapi.service file and have it run the Django app as the user I am signed in with (could be the pi user).

Django was used simply becasue it is what I use at my day job and it was easy to knock out an API real quick, but it could be replicated in other languages if you don’t like Python/Django. Such as PHP, Node.js. It really just needs to run those xset commands and you have control over the monitor.

I am using a REST API switch, which calls the Django API and runs the xset command with the given parameters. Its pretty simple, but does the job. I have been running this program for about a week and it has been working great!

Feedback welcome!

Thanks!

1 Like