This post is about:
- Persistence
- OpenHab 4
- InfluxDB v2
(There is already a good post about InfluxDB v1.x).
Renaming items is easy…
Contact Foo ...
to
Contact Bar ...
But if your item was persisted, you have now the old data in a measurement called Foo
and the new data in Bar
. It will likely break your charts and if you update your charts, the older data will be gone. Time to clean that up.
To get started, I find it convenient to have a Grafana chart open, showing both the OLD and the NEW item id.
So here is an example.
I renamed my DoorSensorOffice_SensorDoor
into Office_Door_SensorDoor
.
The value is the state so it is either 0 or 1 but it does not matter, it could be a battery level, a temperature, etc… anything.
Here is the graph:
We see the old ata in red and the new data in green.
Reminder: the following applies to InfluxDB v2.x and not InfluxDB v1.x.
My InfluxDB bucket for OpenHAB is called… openhab
.
The following FLUX query will copy the old data into the new measurement (while keeping the new data obviously). I am going back 4 years but you can adjust as needed.
from(bucket: "openhab")
|> range(start: -4y, stop: now())
|> filter(fn: (r) => r._measurement == "DoorSensorOffice_SensorDoor")
|> set(key: "_measurement", value: "Office_Door_SensorDoor")
|> to(bucket: "openhab")
After submitting the query, we can see in InfluxDB:
and grafana confirms the copy: we can see green everywhere and some remains of red
Now time to clean up. I did not find a FLUX query that can do that strangely but the command line will do.
We will need:
- an InfluxDB
TOKEN
with proper rights - a
MEAS
-urement variable - 2 dates
Let’s first set store the MEAS
value:
MEAS=DoorSensorOffice_SensorDoor
We can now run the command from the terminal:
influx delete \
--start '2020-01-01T00:00:00Z' \
--stop '2025-01-01T00:00:00Z' \
--token $TOKEN \
--bucket openhab \
--predicate '_measurement="'$MEAS'"'
A refresh in Grafana shows that we did clean things up:
I hope this can help some.
My tip would be: If you rename items, make sure to keep track both OLD and NEW name somewhere. It makes such a migration easier.