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
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