Sensor for water consumption

I get my water consumption reported once a day at the same time. I’ve been playing around with total and tota_increase. However, the values ​​are correct one day and then no longer the other day.
How exactly do I have setup the sensor?

Both are not working

    sensor:
      - name: water_consumption_test1
        value_template: "{{ value_json.totalWaterConsumption | float(default=0) | round(0) }}"
        unit_of_measurement: "L"
        device_class: water
        icon: mdi:water
        state_class: total
    - name: water_consumption_test2
        value_template: "{{ value_json.totalWaterConsumption | float(default=0) | round(0) }}"
        unit_of_measurement: "L"
        device_class: water
        icon: mdi:water
        state_class: total_increasing

Do I need something like L/day or a last_Reset?

What value is reported each day? The total consumption or the consumption of the day?

The consumption of the day.

Next try was a helper via UI:

state_class: total
source: sensor.bwt_water_consumption_yesterday
status: collecting
last_period: 0
meter_period: daily
cron pattern: 0 0 * * *
last_reset: 2023-03-24T07:55:57.007439+00:00
icon: mdi:counter
friendly_name: wasserverbauch

If the sensor provides the consupmtion of the day, then you should add those up to get the overall consumption. I would add a template sensor for the day, test with an if-clause, whether the value has changed and then add the day to the sum. Perhaps someone has a smarter way but this should work.

Ok i can try that, but if the water consumption is the same 1 day, then this edge case will not work, because nothing changed, correct?

I thought maybe this utility helper classes make this for me.

Do you have a link or example for me for the day sensor?

The values ​​are supplied from a REST call. Can I also build a sensor that always resets the last reset with now()? Is something like this possible?
total + lastReset now() should be enough, right?

@dontinelli maybe something like this?

homeassistant:
  customize_glob:
    sensor.bwt_water_consumption_yesterday_total:
      unit_of_measurement: "m³"
      device_class: water
      state_class: total
      last_reset: "{{ states('sensor.date_time') }}"

But last reset is not working, but could this work?

Ixm not sur if there is a possibility with a property as drafted by you. But you could use a helper where you store the time the sensor last has been updated.

I’m not sure if I understand you correctly. Remove the last_reset and use a helper for it?
Or use the helper in the last_reset?

@dontinelli do you have an example?

A utility meter is not working, because it’s not synchron with the rest call and the last period is also not updated on the same time.

Also not possible to show in the energy board.

  water_daily:
    source: sensor.water_consumption_total
    cycle: daily

It must somehow be possible to simply display the daily water consumption on the dashboard if the value is delivered once a day (via REST) ​​or after a restart.

So the water daily looks good so far with the values and attributes

Attributes:

state_class: total_increasing
source: sensor.bwt_water_consumption_yesterday_total
status: collecting
last_period: 0.597
meter_period: daily
cron pattern: 0 0 * * *
last_reset: 2023-03-29T22:00:00.060796+00:00
unit_of_measurement: m³
icon: mdi:counter

But i have no chance to add it to the energy dashboard. What i have todo now?

@MelleD
Do you get the water consumption value in any way into the HA instance? In developer/template editor or states? If not, you might not have the time values included in your URL.
I played around with it and found a solution to get the value into HA.

The only thing is, that two different API calls are needed to get all values from the BWT Perla system, and running them at the same time only delivers one or the other, but not both. But you already have mentioned this in your thread here in the forum.

My current configuration for the water consumption value in file: configuration.yaml

...

default_config:

rest:
  - resource_template: >>>your_secret_URL<<<
    headers:
      content-type: "application/json"
      ApiKey: !secret >>>your_secret_API_token<<<! 
    scan_interval: 86400
    timeout: 45
    sensor:
      - name: bwt_water_consumption_yesterday
        unique_id: bwt_water_consumption_yesterday
        value_template: "{{ value_json.totalWaterConsumption | float | round(0) }}"
        unit_of_measurement: "L"
        device_class: water
        icon: mdi:water

URL format:
https://bwt-real-smarthome-backend.azurewebsites.net/api/Perla/>>>your_product_code<<</waterconsumption?since={{ now().day -1 }}%20{{ ((as_timestamp(now()) - (24*3600)) | timestamp_custom("%B", True)) [0:3]}}%20{{ now().year }}&until={{ now().day }}%20{{ now().strftime(’%B’) [0:3] }}%20{{ now().year }}

Yes i know but this is not enough for the energy dashboard. There you get then wrong numbers some days. The sensor have always the same values, but not the UI on the dashboard
/waterconsumption?since={{ (now() - timedelta(days = 1)).strftime("%Y-%m-%d") }}

Is it easier to build a water sensor that provides the following data?

Daily every 30 min

"00:30
- 00:59 [l]" : 0,
"01:00
- 01:29 [l]" : 0,
"01:30

Monthly every day

"Day 16 [l]" : 0,
"Day 17 [l]" : 0,
"Day 18 [l]" : 0,

And yearly every month

"Month 1 [m3]" : "0.0",
"Month 2 [m3]" : "0.0",
"Month 3 [m3]" : "0.0",

Can I build a water consumption with the data?

Actually it’s pretty easy. With total_increasing and querying the water values ​​every 30 minutes, it works quite well. However, as soon as the REST endpoint is requested more often, e.g. due to a restart etc., the value is added too often. How can this be prevented?

I can query my water consumption every 30 minutes and I have no idea how to build a suitable total increasing sensor
I have the following two problems:

  1. The REST endpoint can be queried more often after a restart, which means that the values ​​are falsified the total increasing sensor.
    Idea is:
    I would probably need a sensor or helper, which has saved the last status. What is best for this?

  2. How can I only update the total_increasing sensor if this value has really changed?

Is it easy to build a python script directly for this?