Hi there! I would like to add a table to my existing HA database, which is SQLite. My HA is installed on a RPI4 with attached SSD. The OS is setup and installed via the RPI imaging program, and use of HA has been so easy for my automations.
The table would consist of the following columns:
month (number)
timeslot (number)
timeslotCount (number)
usage (number)
This table would contain 576 rows max and would update 1 record every 30 minutes. I would like to perform this update via a HA script called by an automation, if that is possible. I have not done anything SQL-related with my HA other than using the SQLite Web addon to browse data (trying to understand the model).
I’m very familiar with SQLite from my day job, and comfortable with the HA scripts and automations I’ve built so far, but wondering if this idea is doomed or dangerous (I understand it’s dangerous to fiddle with the HA database, and I would rather not fiddle with Recorder), and so I’m thinking it’s better to add a second database purely for my use that could be reached from scripts I write - this would be my preferred option.
Clearly I am trying to avoid having to create 576 input_number entities if I can help it!
So my question - Is this do-able or not? Am I being naive about my goal here? If it is possible, and you know how to do this, please could you give me some pointers, such as:
can SQL Web be used to query a second db in the config (or other) folder?
how do scripts write to a different database?
complications & things that have clearly not occurred to me!
I did say, it was to avoid creating 576 helpers. My intention is to maintain an average of my half-hourly power usage.
I have a local-tuya power meter attached to my consumer unit and the HA sensor reports the current power usage since midnight.
Prior to having a string inverter and solar panels, my smart-meter would send half-hourly usage to the DCC where all smart meter data goes, and this data would have been available to me via my energy supplier. From that I was able to build up monthly averages of each half-hour. But since having PV installed, I only draw from the grid at certain times, and so this data is useless.
I hope that explains the goal. Do you have any info on my requirement?
You’re definitely trying to fit a round peg into a square hole here. HA is not meant for database management and you’re actively circumventing the entire statistics & history engine. Is there a reason you’re not using the energy tab and it’s built in abilities, which already calculates these values on an hourly basis? You can even run comparisons to previous months/weeks/days/years.
Or, use one entity to calculate the 1/2 hour average and add it to a total. Then at the end of the month divide the total by 576 to get the average of the half hours for the month. That’s three entities.
I was hoping not to have to go into why I didn’t use the Energy dashboard - it is related to the behaviour of the sensor linked to my power meter - it has spikes every so often (electrical-related) which skew the readings by a factor of 1000, and due to the fact that my helper could not be changed when this is detected, I had to create a input-template and manual midnight resetter. The template entity is not compatible with the device selector in the energy dashboard.
There is a reason I want the data in the structure I mentioned. It is to feed into an external model.
You can likely fix that at the source. I suggest providing more information so we can help you detect these. The template integration in combination with filter integration can likely remove all false results.
A simple single SQL sensor can get you this value with an automation. You don’t need to provide all the data at once. Just make a sensor that runs the calculation every specified half hour and send the data at the time of the calculation. I.e. 1 calculation that always runs, and you send 48 times throughout the day… if that makes sense. Granted, you may need 12 of these, still unclear what the “12 of these” means.
That problem is very common, you likely would have gotten hits if you put it against the energy tag. Is this a template entity? If yes, post the template you’re using.
You would get the history of your current sensor through SQL, average the value over the last month (Will require templating). That sensor will update normally. You write a second automation that fires on the half hour and pulls the current value and sends it whereever you want to send it. No need to muck with adding items to a database.