ok, so I deleted the statistics and min/max to leave the regular trigger template, and trigger template with action recorder.get_statistics.
Ive adapted the triggers a bit more, gave all a trigger.id to play with in conditions, and am seeing good behavior now.
correct resetting at midnight, and steady state changes during the day
to be sure it only updates on increase I added a condition template. At first I feared that would only be useful for the action block, and not for the regular sensor, but the documentation clearly states
Define conditions that have to be met after a trigger fires and before any actions are executed or sensor updates are performed (for trigger-based entities only)
so that is great. Otherwise I would have had to take these 2 out of the 1 trigger config…
(also, it seems to not harm that the recorder.get_statistics sensor is triggered by the reset trigger. its own configuration does as much itself, works fine as far as I can see)
the only difference between the 2 now is that the state sensor is more accurate during increase than the statistics version, simply because the DB isnt updated as often and it lags a it because of that.
you can clearly see that in the attributes, where the last triggered shows they are updated at the same time, while state is behind
final config for now:
yaml for state based and recorder based trigger templates
##########################################################################################
# Templates
# https://www.home-assistant.io/integrations/template/
##########################################################################################
template:
- triggers:
- trigger: time
at: "00:00:00"
id: reset
# - trigger: time_pattern # <-- not used, replaced by to_state > from_state condition
# minutes: /15
- trigger: state
entity_id: input_button.trigger_zp_opbrengst
id: trigger_manual
- trigger: state
entity_id: input_button.trigger_reset
id: reset_manual
- trigger: state
entity_id: sensor.zp_actuele_opbrengst
id: update
conditions:
# see: https://www.home-assistant.io/integrations/template/#conditions
# also needs to be met before sensor updates are performed
# so not only for the actions block
- >
{{trigger.to_state.state > trigger.from_state.state or
trigger.id in ['reset','reset_manual','trigger_manual']}}
# - condition: state # <--- not useful as template condition will capture all
# entity_id: sun.sun
# state: above_horizon
actions:
action: recorder.get_statistics
data:
statistic_ids:
- sensor.zp_actuele_opbrengst
period: day
start_time: "{{ today_at().isoformat() }}"
types:
- max
response_variable: opbrengst_statistics
sensor:
- unique_id: zp_dagelijkse_maximale_opbrengst
# name: Zp dagelijkse maximale opbrengst
state: >
{% set current = states('sensor.zp_actuele_opbrengst') | float(0) %}
{% set previous = this.state | float(0) %}
{% if trigger.id == 'reset' or trigger.id == 'reset_manual' %}
0
{% else %}
{{[current, previous] | max}}
{% endif %}
<<: &power
device_class: power
state_class: measurement
unit_of_measurement: W
attributes:
last_changed: >
{{(this.last_changed|as_local).strftime('%X - %x')}}
last_trigger: >
{{trigger.id}}