WTH - Why can I not get the last value of an incrementing sensor before it resets?

I have an incrementing sensor that resets at midnight. I want to be able to determine what the final value was before it reset.

I asked the question - Last total value for total_increasing sensor

I tried an Automation using a trigger of

    trigger:
      - platform: state
        entity_id: sensor.komfovent_heater_consumption_day_kwh
        to: '0.0'

but this never fires - if I do it at 0.01 it does fire (and give me 0)

How much X was yesterday is really useful.

Use a numeric state trigger, and look for below: 0.01.

Thanks, I’ll try it tonight, but I still think there is a place for this to be easily created (perhaps a helper) on a per sensor basis.

I’m pulling the value from {{trigger.from_state.state}} all of which does feel is a bit of a cludge.

Thanks, that worked but I think this could be a helper (as it would then create the sensor etc).

Not sure what you mean. If you want a sensor, you’d make a template sensor. Helpers are store info, so if you made it as a helper, you’d make it as a boolean with an automation. Not really useful IMO when you have a trigger right there that behaves the exact same way.

To make the template sensor, do I not need an automation to trigger it?

Not sure what you mean here.

What I’d like is, on a daily basis, to display how much energy I used yesterday and store that figure for future use.

No, template sensors just collect info.

Have you looked at utility meter?

not wanting to take away from your WTH, but you can do that with eg mqtt. create a sensor, and record that sensor. its what I do like this:

script:

  publish_sensor_daystart_values:
    alias: Publish sensor daystart values
    sequence:
      - variables:
          switches: >
            {{expand('switch.shelly_switches')|map(attribute='object_id')|list}}
          sensors: >
            {{expand('sensor.co2_living','sensor.luchtvochtigheid_living',
                     'sensor.temperatuur_living')
                     |map(attribute='object_id')|list}}
      - repeat:
          count: >
            {{switches|count}}
          sequence:
            - variables:
                oid: >
                  {{switches[repeat.index - 1]}}
# wattage, power sensors
                actueel: >
                  sensor.{{oid}}_actueel
# kWh, energy sensors
                totaal: >
                  sensor.{{oid}}_totaal
            - service: mqtt.publish
              data:
                topic: >
                  ha_main/sensor/{{oid}}/power_daystart
                payload_template: >
                  {{states(actueel)}}
                qos: 2
                retain: true
            - service: mqtt.publish
              data:
                topic: >
                  ha_main/sensor/{{oid}}/energy_daystart
                payload_template: >
                  {{states(totaal)}}
                qos: 2
                retain: true
      - repeat:
          count: >
            {{sensors|count}}
          sequence:
            - variables:
                oid: >
                  {{sensors[repeat.index - 1]}}
                value: >
                  sensor.{{oid}}
            - service: mqtt.publish
              data:
                topic: >
                  ha_main/sensor/co2_monitor_air_quality_detector/{{oid}}_daystart
                payload_template: >
                  {{states(value)}}
                qos: 2
                retain: true

and automate that:

automation:

  - alias: Set daystart sensors
    id: set_daystart_sensors
    trigger:
      platform: time
      at: '00:00:01'
    action:
      - service: script.publish_sensor_daystart_values

the Utilty meters daily sensors are a bit odd to me, (have them on all power meters) aas they seem to increase during the day.,… and not just during day change

The trigger @petro works for me so my automation looks like this;

  - alias: daily heater consumption kWh
    description: ''
    mode: single
    trigger:
      - platform: numeric_state
        entity_id: sensor.consumption_day_kwh
        below: 0.01
    action:
      - service: mqtt.publish
        data:
          topic: ha/kom_heater_yesterday
          retain: true
          payload: '{{trigger.from_state.state}}'

It sort of proves my point “WTH is it so difficult to get a figure for how much I used yesterday!”. I suppose the argument could be that this is not about automation :frowning:.

Perhaps what is needed is a generic accumulation sensor? We have specific ones for energy (and I think water now) why not make it generic?

Features:

  • Specify period or reset time (D/W/M/Y/30d/1h/24h)
  • incoming data sensor
  • units
  • attribute of last period value

The energy ones (utility_meter) work for any entity though. Either way, if you’re put off by that, you can just use the statistics sensor that does similar computations.

Does it assume kWh (I haven’t looked TBH).

The trouble with the statistics sensor (as mentioned in other threads) is you cannot specify the time period it works on. It just does 24h.

It still doesn’t solve the “I want yesterday’s max figure” issue.

It doesn’t care. The utility_meter integration was designed for what you want. Here’s a simple config of it

utility_meter:
  kom_heater_yesterday:
    source: sensor.consumption_day_kwh
    cycle: daily

EDIT: Forgot you want yesterdays info.

It would be better to call it a generic accumulator then (as there are plenty of other accumulators rather than utility).

It still does not have the “What was the last figure before reset” - which is where I came in :slight_smile:

It could be added as an attribute of the sensor.