What's a good way to log changes in a sensor?

I’ve got a sensor in my EV charger called “sensor.juicebox_energy_lifetime” that has the total amount of Wh delivered from my EV charger since it was installed. I’d like to track changes to that value on a daily basis so I can plot trends in usage, compare them to overall consumption (which I can pull from my rooftop solar controller), etc.

So I’m thinking an automation that wakes up once per hour, compares the value to the previous days value, and then outputs the data to a csv file (?).
Questions

  1. Is this this best approach? Is there something inside HA that might do much of this heavy lifting for me?
  2. If this is a good approach, how do I persist the previous day’s value? Is that a helper->number?
  3. How do I output to a csv file?
  4. I assume I’ll have to create some sort of host volume in my docker container so I can both persist the csv file and get it out of the docker container.

Thanks

Note - I have not tried this- I use Node Red for my file operations. But the following is from my notebook:

  1. Add notify to configuration.yaml:
   notify:
     - name: write_to_file
       platform: file
       filename: /config/attic_temperature.txt
       timestamp: True
  1. Create an Automation:
    Example: log the temperature from a sensor.
   automation:
     - alias: Log Attic Temperature to a file
       trigger:
         platform: state
         entity_id: sensor.attic_temperature
       action:
         - service: notify.write_to_file
           data_template:
             message: "{{ now() }} - Temperature: {{ states('sensor.attic_temperature_sensor') }}"
  1. Restart Home Assistant to apply the new settings.
1 Like

agree this is a good way to do this.

one additional note… i believe you need to add the directory of the file to allowlist_external_dirs.

also the modern way to do the notify is no longer to call notify.write_to_file (ie, name of entity) but is to do notify.send_message with the entity as the target.

more info here:

Yes, you do. I don’t recall how old my notes are… I have this in my configuration.yaml:

  allowlist_external_dirs:
    - "/config"

Sticking with question 1, some other built-in features worth investigating might be:

  1. Utility Meter sensor, which can be configured to reset hourly/daily based on a kWh reading;
  2. Derivative sensor, which can be configured to calculate watts per minute/hour/day given Wh sensor;
  3. Trigger-based template sensor, which can be configured to save a new calculated value at a certain time trigger.

I’m not sure of your motivation to save a csv file, but if a normal sensor with recording isn’t adequate there are also integrations to log to external databases which can be easily queried for graphing, for example InfluxDB.

One remark: since version 2024.6 of Home Assistant the File integration is available from the UI, so you don’t have to configure it in configuration.yaml anymore:

In case anyone wants a step-by-step using the new configuration. Here’s what I did:
I decided to place the file in the directory /config/exports/energy_log_file.csv

  1. mkdir on that directory.
  2. create the file and put in a header. in my case “date,lifetime_supplied_Wh” (if you don’t do this, Home Assistant will create the file and put a default header in that won’t be csv compatible.
  3. you have to allow writing to that directory inside homeassistant if you haven’t already. In configuration.yaml search for the homassistant: tag. If you don’t already have it, add it in. Here’s what I put:
homeassistant:
  allowlist_external_dirs:
    - "/config/exports"
  1. Add the file integration to HA. Set it up as a “notification service”. Type in the full path to the file (in my case /config/exports/energy_log_file.csv". Turn timestamp OFF
  2. Find the new file notification in the entities. It’ll probably get an entity_id of notify.file. change the entity to something sane (I called mine notify.energy_log_file )
  3. Create the automation. I created mine via the UI so I can maintain it there, but doing it in the yaml is fine too. Here’s my yaml for the UI created automation:
- id: '1724341852725'
  alias: Update EV Energy File
  description: Updates the file with the current lifetime EV charging energy supplied
    data
  trigger:
  - platform: time
    at: '23:59:31'
  condition: []
  action:
  - action: notify.send_message
    metadata: {}
    data:
      message: '{{ now().strftime(''%Y-%m-%d'') }},{{ states.sensor.juicebox_energy_lifetime.state
        }} '
    target:
      entity_id: notify.energy_log_file
  mode: single

The information should already be available in long term statistics. First check that is being created correctly (see here if not). Then ApexCharts can plot the daily long-term statistics. So you don’t need to create anything, just ensure the values are being stored by using the Statistics tab under Developer Tools.