The database in the home assistant and the smart home server

In Home Assistant, of course, there is one useful feature that is the ability to build different graphs, by default they are added there for all objects of all parameters, data is saved in the database, of course they make it so that you can change it to another database, and not which is used by default, but this makes no sense, because the one that that is, it works fine, if of course you want to extract data somewhere from there or use it in other systems, then this is another topic and of course you can get so confused, but by and large if you just want to see how the values changed, draw some conclusion from this, then it will be quite interesting to do it in Home Assistant.

I did it quite differently from the beginning, I have been maintaining data of all values, all their changes, on external hosting for a very long time, well, because first of all, although it is expensive enough, because it is hosting, well, it is paid hosting, it turns out somewhere 300 rubles a month, then there are almost 3 thousand and even more per year, well, it depends on how to pay, if for the whole year at once, then a little cheaper, and as a result there is quite a lot of space, a backup copy of this data is made there and you don't have to worry too much, the only negative is that you need an Internet connection to transfer data there, well, you need to figure out how to do this, that is, I have it done in such a way that all the devices and all their parameters are encoded and there is a separate script that, for example, through zegbee2mqtt receives a description of this device with all its values there and then each attribute separately transcodes it to a specific system and sends it to this external server, all these values.

This is convenient because I use the ability to build different charts there through Google Chart.

I made a page where you can display four charts, well, there can be less if desired, and you can specify the period for which these charts are displayed, and you can also specify in what detail they are displayed, that is, let's say in seconds or in minutes and in hours, up to This was still in months, I made an additional one recently in weeks, because statistics have already been running for many years.

Sometimes you need to somehow average the value so that it is not as small as the daily value, the month is too large, but apparently you will soon have to make an assignment by year, but this doesn't make much sense, for example, by month.

watch the change in temperature, average, let's say in rooms, on the street, I also have data saved from the weather server, and this is convenient, I even made it so that I have stock quotes saved in this database for some of the most interesting ones, that is, in principle, you can use it for anything.

I also made it possible to display a graph, the average value, the minimum, the maximum, and if necessary, and there are still all sorts of additional such chips, so let's say to show the delta, that is, changes from the previous value, for example, if by month then I will have as usually in the graphs of water or electricity consumption is plotted that in this month spent so much, that's also what I have displayed in this graph.

And there is also such a feature that I can calculate the total electricity consumption, I did it in such a way that each change in the parameter it is there in terms of time and power consumption can simply add consumption for this period, for example, I have a light bulb, it consumes 20 Watts there, if it burned there for 1 hour, it means 20 Watts will be recorded in this database, if, for example, when it was turned off, it burned for half an hour, then 10 watts will be recorded and so I can just see in total how much was consumed by this light bulb, that is, when it is in on mode, it is multiplied by one and written if it is in off mode, it is multiplied by zero and accordingly, it is not added, it is convenient.

There is an even more clever way when let's say I know the consumption in amperes, I also indicated there in my database that it is necessary to multiply by 230, that is, volts in fact, and I get the consumption already in cotton wool, that is, it is also very convenient and easy to recalculate in this way.

Well, I can also just look at the number of changes in the database, that is, if a value is sent once a minute or more, I can easily see how many messages this value was sent, that is, for example, if the light turned on and off, then these are two sends, this allows you to take some sensor, for example, an opening sensor, and see how much it worked in a month and surprisingly it turns out that many sensors that I use, for example, from Xiaomi, they have already made 10,000 shipments there and the battery has not even sat down, and the sensors that I have on the radio frequency at 433 MHz have been working for a very long time, then they have already made 100,000 shipments there and still work from the battery, there it is larger than CR2450, but they can still do a normal high-quality transfer of information to the smart home system.

Well, in Home Assistant, too, of course, all this can be configured, but it turns out that the base is growing quite quickly and many either simply stop recording, for example, when I used an SD card on raspberry, this was actually one of the main reasons why the SD card quickly switched to read-only mode and that's all, in fact no longer worked, it looks of course strange when it goes into this mode like you go in everything works everything is saved there, rebooted and all the old values remain, all the files are old, that is, everything it essentially only saves it somewhere in the cache, in memory, but in reality it can no longer write to the map, well, I understand if the card is large, it will certainly live there longer, but still, if it fails, it will be even more offensive than, say, if a small and cheap card comes out.

If you turn off the recording of these values in the database, then there will be no statistics, this is also not very interesting, for example, I turn off all logs at once, there are all sorts of system logs, in Home Assistant, in Zigbee mode there are also only errors to record, in OpenHAB I immediately turn off all logging, because it also writes all changes to all events, it sometimes happens a lot of data just they are not even written to the database, but to a text file, and of course such a stream just any card there and firstly it will score it, and secondly it will certainly quickly exhaust its resource.

Therefore, people think how to connect SSD disks there and there are generally other solutions, there are such adapters for EMMC memory that stick into the SD card, into the connector, but this is all of course such crutches it is better to first really limit the amount of writing, there is a utility in Linux that is easy to check which ones there are applications and how much they write, well, when I turned everything off, there was practically very little that was recorded.

In Home Assistant, it is also possible to configure this monitoring, that is, so that not everything is recorded in the first place, and I have done it so that basically it only writes for 7 days and writes to the database only once a minute, well, probably the recording itself does not particularly affect the resource, because it is recorded anyway there, at least once a second, at least once a minute, there is no big difference, the only thing is that when, for example, the server reboots, data for this minute can simply be lost, because they did not have time to register, but this is not very critical, because nothing really changes so quickly in Home Assistant, for example, if zigbee2mqtt is used, and in general there is an MQTT service, then there is such a retain attribute, that is, these values are stored in mqtt itself, this also creates a lot of traffic additionally, everything is also recorded in the database and of course you also need to pay attention to this if a lot of such values are saved and they are constantly updated, constantly with if they are recorded in this way, then this is also such an additional data source in order for the SD card to fail faster.

Therefore, Home Assistant of course pulls up these default values and it does not really need the old values from the database, it never takes them from there when turned on, for example, I have a system that I use self-written, it takes the latest data from the database, loads it so that there is no need, for example, to show that this value is still it was not received, and in Home Assistant this often happens when rebooting, it simply writes either the old value that was pulled up from MQTT, or writes that the value is missing and it can be very long right there, for example, until some device sends the value, in this mode it lights up and for example when you do some-then the user dashboard, then there just can also display this value crookedly and there in general ugly writes that there is no value, or there it is not a numeric value, and there is some scheme, well, let's say some element on the dashboard it is displayed incorrectly, or not displayed at all.

In this mode, for example, I have quite a lot of different sensors and devices through MQTT about 50 devices I have got, well, it is clear that each device has its own separate sensors, for example, a temperature humidity sensor, it records temperature, humidity, even if it is an Aqara sensor, it also writes pressure, and communication quality separate parameters I have recorded, also recorded the value of the battery voltage and in percentage measurement the remaining charge, that is, a lot of such data goes and this is only this sensor, and it quite strongly restricts its traffic, its transmission, and for example a smart outlet, it does not limit itself in any way, it does not have the task to save battery there and it feeds from the network, and it sends there every 10 seconds a huge stream of data that also changes, there is a voltage in the network, and the current consumption there can send up to hundredths, and in general there is current and consumption in cotton wool, and other different parameters that it has.

this socket, and also the quality of communication, in general, a lot of different parameters are also stored in the database and in the end it turns out that I have 7 days it takes about 500 MB of space, and 7 days is certainly not enough, that is, for example, not to look there, for example, Monday to Monday, that is, in this mode, or for example, you can not compare there month to month changes, but in general, of course, you want to look for a whole year, well, then there is a way out or just use a large SD card and store all this information on it, well, then you need to be ready for the fact that the SD card will fail sooner or later, but I use the built-in EMMC memory in the Orange Pi 3, it seems to be more reliable, and because of this, of course, it turns out to be very limited in the size of this log for monitoring at my smart home.

Automation in a smart home should be useful and create additional comfort, but sometimes everything goes wrong… The reasons for this, and how to configure everything, see on my channel.

You may also watch

Article By Alex

If you have any ideas on how to improve automation, make it more reliable, fast and useful, then you can leave a comment below. I will try to implement the proposals and tell you in the following videos.