You beat me to it. I was just logging on to post what I ended up doing.
In my OP I showed the automation which creates the “burner log” file. This automation creates one entry (line in the file) every time the burner on my heating system comes on (fires) or goes off. Obviously, at some point this data has be be cleaned up. I don’t want to lose it, but I don’t want an enormous file building up month after month, either.
Ideally, each month should start a new file. But the file notification component can’t dynamically set the file name based on the current month. So instead I went with the Shell Command service to use the UNIX “mv” (move) command to rename it. (Unix doesn’t have a “rename file” command.)
# Shell Command Service to rename a file
shell_command:
rename_burner_log: "mv -n burnerlog.txt {{ (as_timestamp(now()) - (60*60*24*27)) | timestamp_custom('burnerlog_%y_%m.txt') }}"
The mv command takes the switch “-n” which indicates not to move anything if the target exists.
Next are the two parameters, the “from” and “to” file names.
“From” is easy: burnerlog.txt.
“To” is a template:
{{ (as_timestamp(now()) - (60*60*24*27)) | timestamp_custom('burnerlog_%y_%m.txt') }}
First, the current time “now()” is converted from a string to a timestamp.
Next we calculate how many seconds there are in 27 days, and subtract that from the timestamp for “now.” This ensures the new timestamp is always in the previous month.
That new timestamp is piped (“|”) to timestamp_custom, which produces a string formatted as the literal “burnerlog_” followed by the current year, another “_,” the current month, and the file extension “.txt.”
Thus, since I’m writing this in April, my burnerlog.txt is renamed to burnerlog_19_3.txt.
Because of the “-n” switch, this can be run multiple times without worrying that a whole month’s worth of data will be wiped out.
Now all I have to do is trigger the service on the first of every month. Automations have a great “time” platform which lets you trigger an automation based on time of day. But I haven’t found anything which lets you trigger based on day of the month. So I came up with this:
- id: id_burner_rename
alias: run_burner_rename
trigger:
- at: '00:01:00'
platform: time
condition:
- condition: template
value_template: "{{ now().day == 1 }}"
action:
service: shell_command.rename_burner_log
At 12:01 AM local time, every day, this automation is triggered. But the action only takes place if the condition is “true.” The condition looks to see if the day-of-month is currently “1”.
So on the first of every month, the “mv” command is issued and the file gets renamed.
It would be possible to schedule more time platform triggers in this one automation, in case HA is not running at midnight for some reason, or to schedule other automations for other days, in case it’s not running at all on the 1st. But presumably I’ll be checking in on things at least once a month, and I can always manually rename and/or edit the file, as needed.