Looking at what you have and what you want, I’m not sure I’d do it this way.
I would create the sensor as you suggest as that gives me how much I’ve used today (useful)
So : -
sensor: #### not sure where you are putting your sensors, but this should be okay for both main config or packages.
#### If you have a sensor.yaml then you don't need the *sensor:* and indents go back two.
- platform: template
sensors:
water_m3:
friendly_name: "Water"
unit_of_measurement: "m³"
value_template: "{{ (states('sensor.testswitch_counter_today') | float * 0.01) | round(2) }}"
This is almost exactly what you have. (why repeat the M3 bit ??? Also ‘metre’ units are ‘m’ not ‘M’)
Now, the next bit is where opinions may diverge …
I’d create a binary sensor that is ‘on’ when the sensor you just created exceeds a threshold you have in a given input_number (ie input_number.m3_cut_off - again, what you have, assuming you have 1 to 10 as your range, but whatever range you actually need)
so the binary sensor would be : -
binary_sensor:
- platform: template
sensors:
bs_water_control:
value_template: >
{% set limit = states('input_number.m3_cut_off') | float %}
{% set current = states('sensor.water_m3') | float %}
{{ current >= limit }}
friendly_name: Water Shut Off/On
#### inverted would be : - {{ current < limit }} ## see further down
This ‘could’ be shortened (not always simplified) to : -
binary_sensor:
- platform: template
sensors:
bs_water_control:
value_template: "{{ states('sensor.water_m3') | float >= states('input_number.m3_cut_off') | float }}"
friendly_name: Water Shut Off/On
This way (with the binary sensor you can switch the valve ‘on’ and ‘off’ from one automation.
e.g. : -
automation:
#water valve control
- alias: au_switch_water_valve
mode: restart
trigger:
- platform: state
entity_id: binary_sensor.bs_water_control
action:
- service_template: switch.turn_{{trigger.to_state.state}}
entity_id: switch.switch_water_valve
This assumes ‘exceeded’ cuts valve ‘off’ by turning the switch ‘on’ (otherwise invert the logic in the binary sensor) [Note it will also turn the valve back ‘on’ when the counter is reset]
I also assume you have an automation that fires every ‘midnight’ ??? to reset the counter ???
- alias: au_water_reset
mode: single
max_exceeded: silent
trigger:
- platform: template
value_template: "{{ states('sensor.time') == '00:01' }}"
#### Note: best to avoid '00:00' if you can, a lot of HA activity goes on at '00:00'
action:
- service: input_number.set_value
data:
entity_id: input_number.m3_cut_off
value: 0
Note this is a template time trigger and is 60 times more efficient than a standard ‘time’ trigger as that evaluates every second.
@finity, can you please cast your eye over this, I hate posting code I can’t check
@jbizeray, I don’t use the Automation Editor so you version ‘may’ look a bit different, but at least this gives you a general structure to be aiming at.