Sending notifications when there is some water consumption while I am not at home

Hello masters of home assistance and programming :smiley:

I am new to Home Assistant and this is my first implementation of something own.

Some days ago I got a water leakage detection device installed (Judo I-SAFE FILT) and I wanted to use it to get informed when there is some water consumption while I am not at home.

I know there is already a HACS implementation for that kind of devices but installing some python scripts and using MQTT was a bit too much work for such a simple job.
Thats why I used RESTful requests and the (local) API of my device to get the Sensor Data:

sensor:
  - platform: rest
    resource: http://10.0.xxx.xxx/api/rest/2800
    username: "admin"
    password: "secret"
    authentication: basic
    scan_interval: 30
    name: Gesamtwassermenge
    value_template: "{{value_json['data'] | int(base=16) | pack('>I') | unpack('<I')}}"
    device_class: WATER
    state_class: TOTAL_INCREASING
    unit_of_measurement: "L"

I only had a bit trouble because the data was hexadecimal and I had to switch from LSB to MSB (FF000000 = 255 and 00FF0000 = 65.280).

It works like a charm even on energy panel :slight_smile:

Now to the automationā€¦

That one worked to inform me when my sensor changes:

triggers:
  - trigger: state
    entity_id:
      - sensor.gesamtwassermenge
conditions:
  - condition: state
    entity_id: select.homematic_sv_alarm_scharfschaltung
    state: Vollschutz
actions:
  - action: notify.mobile_app_phone
    data:
      title: Wassernutzung
      message: Es wird gerade trotz Abwesenheit Wasser verbraucht
      data:
        push:
          sound:
            name: default
            critical: 1
          volume: 1
  - action: automation.turn_off
    metadata: {}
    data:
      stop_actions: true
    target:
      entity_id: automation.wasserverbrauch_bei_abwesenheit
mode: single

I had to stop it after it triggered the first time so it wont trigger again on every further change.

There are also two automations for enabling this automation when my alarm system gets activated or disabling it when it gets deactivated.

But I would like to optimize it a bitā€¦
At the moment the automation even triggers when the value changes from 1234,9999 Liters to 1235.
Itā€™s value isnā€™t float so you donā€™t see the 0,999 part but maybe you are only one waterdrop away from next Liter.

How can I change it to only inform me after (for example) it increased 5 liters?
I already found this thread:

(thx a lot!)
So one possibility maybe using something like this:

  - condition: template
    value_template: |-
      {% set t_new = trigger.to_state.state|int(-99) %}
      {% set t_from = trigger.from_state.state|int(-99) %}
      {{ (t_from + 2) < t_new and (t_from > -99 and t_new > -99) }}

But then it only gets activated when 2 Liters are getting increased between two scan_interval (30 sec). Not since the alarm system got activated.

What would you recommend?

My idea was setting a new helper to the value of ā€œGesamtwassermengeā€ when the alarm system gets activated.
Then a I can compare those two valuesā€¦ and maybe I can also do some future features like allowing 50 Liters more if the dishwasher (or some other devices with water consumption) was turned on when the alarm system got activated.

Do you have some better ideas? What would be your best practice? :wink:

Something like:

{% set p = trigger.from_state.state|float(-99)|round %}
{% set c = trigger.to_state.state|float(-99)|round %}
{{ c > p }}

This will round both the previous value and the new value up to the nearest whole number - so that at least you will only get notified when enough water was used to trigger a change to the next litre.

You probably should though store the starting value in a helper - and compare how far over the starting reading it is.

Hopefully this bit of code gives you a jump start on ideas though.

Thank you for your response.
Unfortunately not, because the API only gives me integers and no float values. But itā€™s good to know and to memorize for future ideas :wink:

At the moment I am working on the helper idea.
Then it should also possible to implement another helper like ā€œliters_since_alarm_system_activationā€.
That may help in case of an alertā€¦