Sensors using historical data?

Hi,
My weatherstation has a rain sensor that return an absolute counter. Is it possible for me to do a virtual sensor that gets a 24h old value and subtracts from the newest value ?
Right now I have a counter that says 5007. If it says 5009 tomorrow, I would like to have the output 2. I have integrated the weather station using MQTT (mosquitto_pub).

You should be able to use a sql sensor - here’s mine that does a daily low and high temp

  - platform: sql
    queries:
      - name: "Daily Outside Low Temp"
        query: "SELECT min(state) min FROM states WHERE entity_id = 'sensor.small_shed_temperature' and created >= date('now', '-1 days');"
        column: 'min'
        unit_of_measurement: '°F'

  - platform: sql
    queries:
      - name: "Daily Outside High Temp"
        query: "SELECT max(state) max FROM states WHERE entity_id = 'sensor.small_shed_temperature' and created >= date('now', '-1 days');"
        column: 'max'
        unit_of_measurement: '°F'

https://www.home-assistant.io/integrations/sql/

That is one way. Seems most recommendations is influxDB, which I have not installed.
My problem is, that many of my sensors are not registered as sensors, but as entities under the unit of measurement. This is somewhat confusing. Working on figuring that out.