Home » Blog » How To Master Graphs to Monitor Occupancy and Device Usage in Home Assistant.

How To Master Graphs to Monitor Occupancy and Device Usage in Home Assistant.

Hi! Since making YouTube videos, I spend a lot of time in my studio. I wanted to gain insight into how much time I spend in my studio every day. And that is, of course, possible in Home Assistant. In this tutorial video, I’ll show you how to create a bar graph to track occupancy in a specific room or to monitor the daily usage of a device. In addition, I teach you how to make custom sensors yourself! Let’s do this!

⭐⭐⭐ NOTE: ⭐⭐⭐

This article accompanies a YouTube video. I wrote it for people who would rather read than watch a video. To keep doing this, I would like to ask you to check out the video, leave a comment under the video, give the video a thumbs up, and subscribe to my YouTube channel. This means that the video is offered more often to new visitors so that they also stay informed of the latest Home Assistant tutorials.

Thank you for your support!


Click here to watch the video


I was curious about how much time I spend in my studio every day and created this bar chart to monitor this. The technique that I use for this can be applied to various other use cases. You can use this for instance also to monitor how long your washing machine is running every day, how long your lights are turned on every day, or how long your heating is turned on. The possibilities are endless to monitor the usage of your devices when you use the code that I created for this video. Let’s not waste any time and start creating this!

We are going to make use of the history_stats sensor in Home Assistant. The history_stats sensor platform provides quick statistics about integrations or platforms using data from the history integration. This will work for each device or entity that is included in the history recorder. By default, all devices and entities are included in the history. If you excluded entities from the recorder, then you cannot create these graphs for that entity of course. If you want to know how to include and exclude entities in the recorder, then watch this video where I explain how to do it.

How to create a Custom Sensor in Home Assistant to monitor its usage.

First, we have to create some custom sensors in Home Assistant. I will create a custom sensor for tracking my daily occupancy in my studio. And I will create a custom sensor to track the daily time that my lights in the living room are on. There are multiple ways to do this. I like to keep my Home Assistant set up tidy, so I make use of a sensors.yaml file that I created. To do this, you have to install Studio code server first.

How to install Studio Code Server in Home Assistant

  • You can install Studio Code Server by going to Settings->Add-ons.
  • Click on Add-on store.
  • Search for Studio Code Server and install studio code server.
  • Make sure that Show in sidebar is selected and start the add-on.

How to create a sensors.yaml file in Home Assistant

Now open Studio code server. You see all the files of your Home Assistant server here. Let’s create a sensors.yaml file.

  • Make sure the Config folder is the currently selected folder.
  • Click on the New File icon.
  • Give the file the following name: sensors.yaml.
  • Now open your configuration.yaml file
  • and add the following line: sensor: !include sensors.yaml.
  • Save this file and reboot Home Assistant by going to the Developer Tools, check your configuration, and restart Home Assistant.

How to add the custom sensor code to Home Assistant

Now we are going to add the custom sensor code to Home Assistant. I already added the code to my system and will explain what it exactly does.

  • Open Studio Code server and open your sensors.yaml file.
  • Now add the following code.
  • I’ve added two custom sensors here, one for getting the total time that the office was occupied today. And one to get the total time that the lights in the living room were on today.
  • First, we are defining the platform. In our case that is the history_stats platform.
  • Then we are defining the type, which is time.
  • The name is the name of the sensor so that we can retrieve the sensor by this name in Home Assistant. I called this one “Office Occupied Today”.
  • Next, we need to specify the entity for which we want to calculate today’s total time. In this case, it’s my office motion sensor.
  • The state of the sensor determines for what state the time should be used. I want to know the total time that someone was in my office, so I am going to set this state to “on”. Now only the time will be calculated when the state for the entity was “on” during the day.
  • The next part might be a bit tricky to understand, but what I am doing here is defining the Start and End time for the interval that the total time has to be calculated. The start time is midnight or 12AM. What I do here is get the current time which is Now() and I am replacing the hours, minutes, and seconds by 0. This way it will result in being 12 AM.
  • The end time is the current time, which is Now().
The living room lights

I did the same for the Living Room Lights on statistics. The only difference is that I changed the Entity ID and the name of the sensor.

So, you can use this code for all kinds of sensors in your house. You can type this code from the screen, but if you sponsor me, you can download this code in return so that you can just copy and paste it into your own sensors.yaml. This will save you time and frustration. See the download link in the description of this video.

Test the sensor code

Now let’s test this code.

  • Save the sensors.yaml file.
  • We have to restart Home Assistant to activate these two new sensors.
  • Go to developer tools and check your configuration.
  • If the configuration is correct, then click Restart to restart Home Assistant.
  • After Home Assistant is restarted, go to the States tab.
  • Search for lights_on_today
  • You will see the total time that the lights were on today.
  • Now search for office_occupied.
  • You will see the total time that the office was occupied today.

Great!, the sensors work! Now let’s use them in a bar chart!

I need your help!

You will be doing me a huge favor if you subscribe to my channel if you haven’t already. And, you help me a lot if you also give this video a thumbs up and leave a comment. This way, YouTube will present this video to new people and that will make the channel grow! In the description of the video, you will also find information about how you can sponsor me so that I can continue to make these tutorials for you.

Thank you!

Setting up the bar graph charts for our sensors to monitor the usage.

To create the Bar Charts for these sensors, we are going to make use of a HACS integration called Apex Charts. If you want to know how you can install HACS, then check this video.

To install the Apex Charts, go to HACS.

  • Go to Frontend.
  • Click Explore & Download Repositories.
  • Search for Apex.
  • Click Apexchats-card
  • Click Download.
  • Click Download again.
  • Click Reload.

The ApexCharts integration is now installed. Let’s add it to our dashboard.

Add the Bar Chart to a Dashboard in Home Assistant

  • Open one of your dashboards.
  • Click on the three dots in the upper right corner.
  • Click Edit Dashboard.
  • Click Add card.
  • Search for Apex.
  • Click the Custom ApexCharts Card. If it doesn’t show up, you might have to restart Home Assistant first.
  • Replace the code with this code.
  • I will explain what each line in this code does.
  • First, the type is declared, which is the apexcharts-card.
  • Then I want to use different colors for different thresholds.
  • The graph span is 7 days, so it will show data for the last 7 days.
  • The span ends on the current day.
  • I also want to see when the chart was last updated, so I’ve added the last_updated option here.
  • Then, I am defining the header of the chart.
  • I want to show a header with the title Studio Occupancy.
  • I also want to see the current state of the sensor and I want the state to get the defined threshold color that I will show in a minute.
  • After defining the header, I define how the y-axis looks.
  • The minimum value of the y-axis is 0.
  • The maximum value is softbound around 9.
  • I do not want to see decimals on the hours and the tick amount is 5
Define the series

Now we’ve defined the general look of the graph. In the next step, we are going to define how the value series will look.

  • We want to view the entity: sensor.office_occupied_today, which is the custom sensor that we defined earlier.
  • The header_color_threshold will colorize the header’s state based on the threshold.
  • The extremas option makes sure that you see the maximum value on top of the bar.
  • The as_duration option makes sure that the state in the header is formatted correctly. I’ve set it to hour.
  • The graph type is column because I want to show a bar graph.
  • The name of the series is Duration.
  • The group_by options group the data of the sensor. In this case, it is grouped by the last value of the sensor during 1 day.
  • Then, the last setting is the color threshold setting. For each threshold, the color is applied to the bar from the threshold value.
  • Now click save.

I did the same for the Living Room Lights. The only thing that I changed is the header name, the entity name of the sensor, and the y-axis max value.

This graph code works exactly the same for every custom sensor that you add. Just like the code for the custom sensor, you can type this from the screen or sponsor me and download this code in return. See the download link in the description below.


I only created these graphs last week, but they already turned out to be very valuable. As you can see, the lights in the living room are on for many hours a day. It turned out that I have to tweak my motion sensor in the living room so that it doesn’t trigger based on our cat so much. This will save me quite some money!

I hope you’ve enjoyed this tutorial and that you have some use cases for it yourself. If you have any questions or comments, please leave them down below and I will do my best to respond to as many as possible. If my videos save you time and you feel they are valuable to you, please consider becoming one of my patrons, just like these wonderful people did. They play a vital role in making sure that I can keep creating these videos for you. See the links in the description below on how to become my patron. Thanks for watching and don’t forget to give this video a thumbs up and subscribe to my channel to help the channel grow!

I’ll see you soon in my next video!

Bye Bye!

home assistant, insight, monitor, graph, statistics, chart, occupancy, apexcharts, motion sensor


Back to all articles