Automation: weekly reports about statistics

N.B - This topic is old, and what’s described below may not work anymore

In my opinion, what home-assistant lacks the most is the possibility to do something interesting with all the collected data.

A few months ago, I developed the history_stats component, which is now fully integrated in home assistant. (doc here)

I can get interesting stats about my habits, as shown on the following screenshot:





But it’s not enough. Most of the time, I look at the data and immediately forget it. What I wanted is a weekly report about my habits, delivered in my inbox.

I’m going to show you how to configure home assistant to receive the following e-mail at the end of every week:





Let’s start working :desktop: :tools:

Requirements

You need to have the following components activated in your home-assistant configuration

  • IFTTT
  • A history_stats component with a period time of 1 day or less.

Both the “Today” and “Yesterday” examples from the documentation will work perfectly

IFTTT event

To send an automated email from the home assistant machine, I thought IFTTT was the less painful solution.

No need to setup an e-mail server or anything complicated, plus home-assistant API can trigger an IFTTT event.

Create a new applet by clicking here, or do it yourself and choose the Maker Webhooks service > Receive a web request.

Choose an event name, for example weekly_report.

For the THEN THAT part of the applet, choose the Gmail or the Email service (I prefer the Gmail one, because the Email service adds crappy stuff at the end of the mail body).

Configure your applet like on this screenshot (webhooks | Gmail | Email):

{{Value1}} will be the name of the report, and {{Value2}} will hold a HTML string that can be sent via e-mail.

Script

In order to fetch data, analyse it, and generate the body of an e-mail, I have created a small python script, that you can find here.

Save the script somewhere on your home assistant machine, and edit the following lines.

    api_password = 'my_password'
    server_url = 'http://localhost:8123'
    ifttt_event = 'weekly_report'

The variables are self explanatory.

The script can now be called like that (long / short syntax):

 python weekly_report.py --name 'Report name' --entity 'sensor.tv_yesterday' --offset 1
 python weekly_report.py -n 'Report name' -e 'sensor.tv_yesterday' -o 1

The report name (option -n) will be stored on the {{Value1}} variable, and the entity (option -e) should be the history_stats component your created earlier.

N.B: Use an offset (option -o) of 1 if your sensor is a ‘yesterday’ sensor, and 0 if your sensor is a ‘today’ sensor (defaults to 0). This change is because I’ve recently removed the from and to attributes from the component.

You can try to call the script to see if it works, before going to the next step. You might need to replace python with python3, depending on how you installed python.

Automation

Now that you have a working script calling IFTTT, you need to configure when and how the script will be called.

Just add one or many command line notifiy in your configuration.yaml. You can configure multiple reports using different values for the -e, -n, and -o options, just like that:

notify:
  - name: TV report
    platform: command_line
    command: "python /home/pi/.homeassistant/scripts/weekly_report.py -e sensor.tv_yesterday -n 'Weekly TV report' -o 1"
  - name: Lights report
    platform: command_line
    command: "python /home/pi/.homeassistant/scripts/weekly_report.py -e sensor.lights_today -n 'Weekly lights report'"

Finally, create a new automation to trigger one or many notifications automatically.
In my case, I trigger them every monday at 00:05 AM.

automation:
    - alias: Send TV report
      initial_state: true
      hide_entity: false
      trigger:
        platform: time
        after: '00:05:00'
      condition:
        - condition: time
          weekday:
            - mon
      action:
        - service: notify.tv_report
          data:
            message: ""

Note that you have to provide a data.message, even if you let it empty, otherwise the automation won’t work.

Restart your home assistant and try to trigger the automation manually from the UI. If it works, you’re done, and you can change hide_entity to false if you want.

Conclusion

I have just finished to create and configure this process, but I’m sure it can be improved a lot.

Please don’t hesitate to share your ideas about a better version of the script, some additional features, or any feedback you have.

Thanks for reading

:thumbsup:

9 Likes

Thanks for this, if for nothing else than reminding me about your history stats component.

I remember when you were first creating it that I wanted to integrate it but it somehow got lost in the shuffle. I need to work on this over the weekend!

This has a lot of potential. I’m already thinking of using it to get a better sleep schedual. - having HA use my status over the week to tell me how long i sleept each night, maybe have a bit of nuging in stating the optimal sleep time. :slight_smile:

Thanks man

slightly off topic: i really like the idea of the History Statistics Sensor, unfortunatly i find it completly unusable. this might be due to my setup (my recorder component is saving data to mysql, i run hass on a mac), but even if i only use two sensors for ysterdays data, my CPU load stays above 100% permanently, due to the History Statistics Sensors. i filed an issue and would be glad if there was a solution for this, because i’d really like to use more of those History Statistics Sensors and these weekly reports.

https://github.com/home-assistant/home-assistant/issues/7800

Yeah, that would be a great idea! I guess the script could be improved to compute an average value so you can have a better idea of how well your slept during the whole week.

BTW, how do you track your sleep time ?

Please share the interesting things you manage to do :+1:

Oh, I never though there would be issues with the performance. I guess I should run benchmarks with the default database too, just to measure the impact of the history_stats component.

As a temporary solution you could try a higher scan-interval, like one or two hours for example, as you don’t need real-time data in a “yesterday” config.

1 Like

I’m gonna take a closer look at it after the exams, time is precious at the moment :wink:

To track my sleep im using a status-select-system, showcased by Ron Mar in this video.

Right now I have the system detect when I’m home, away (using iPhone wifi and SNMP) or waking up (using motion sensors, time of day and time since last motion in the livingroom).
I have not yet found a way to detect when I’m going to sleep, in an automated way that is. This is top priority after the exams :smiley:

tnx, setting a high scan inerval lead to a lower CPU load after 3 minutes. so that helped. but still, HA startup was delayed for about 2 or 3 minutes by the history stats component.