What's wrong with my configuration.yaml?

Trying to use history_stats: and sensor: to create sensors that show (1) running total propane gas usage for most recent 10 days and (2) running average daily propane gas usage over past 10 days.

I’m not stuck on this method, and I’m very far from an expert at this.

What did I mess up?

I’ve got this in my configuration.yaml, but it’s not working.:

sensor:
  - platform: history_stats
    name: "630_propane_level_now"
    entity_id: sensor.propane_tank_neevo_630_gallons_rest
    state: "any"
    type: "time"
    end: now
    duration:
      seconds: 1

  - platform: history_stats
    name: "630_propane_level_10_days_ago"
    entity_id: sensor.propane_tank_neevo_630_gallons_rest
    state: "any"
    type: "time"
    end: now - timedelta(days=10)
    duration:
      seconds: 1

template:
  - sensor:
      - name: "630_propane_10_day_diff"
        unit_of_measurement: "gal" # Adjust this to match your sensor's unit
        state: >
          {% set current_value = states('sensor.630_propane_level_now') | float %}
          {% set past_value = states('sensor.630_propane_level_10_days_ago') | float %}
          {{ (current_value - past_value) | round(2) }}

  - sensor:
      - name: "630_propane_10_day_ave"
        unit_of_measurement: "gal"
        state: >
          {{ (sensor.630_propane_10_day_diff / 10 ) }}

That is too much

“It isn’t working” doesn’t help anyone. What isn’t working, and what do the logs say?

For one thing the end parameter of the first two sensors clearly is not correctly specified. It should be a datetime or timestamp or a template that results in either of them, you currently provide a string.

end: "{{ now() }}"
end: "{{ now() - timedelta(days=10) }}"

Will the above work? I have absolutely no idea as I do not know how the history stats integration works, but they are at least valid templates.

What you circled is valid yaml, it’s not too much.

@josephny Please explain what’s not working. The yaml itself is valid but you do need to incorporate @Mayhem_SWE’s changes

I do apologize. I did a poor job of the title/subject.

Please let me try again.

I have a sensor named: sensor.propane_tank_neevo_630_gallons_rest

This sensor is created by this code:

rest:
  - resource: https://ws.otodatanetwork.com/neevoapp/v1/DataService.svc/GetAllDisplayPropaneDevices
    authentication: basic
    username: "<user>@<domain>.com"
    password: "<password>"
    scan_interval: 720000000
    headers:
      Content-Type: application/json
    sensor:
      - name: "Propane Tank Neevo 630 Gallons Rest"
        unique_id: propane_tank_630_gallons_rest
        value_template: "{{ value_json[2].Level|float(0) * 1.2 if value_json[2].Level is defined else this.state }}"
        state_class: measurement
        unit_of_measurement: "gal"

Because the data provider updates the data at random times, and I wanted to get the value as of a set time each day, I set the scan_interval too high to be of any useful value and use the following authomation to update this sensor:

alias: Neevo update 5min past midnight
description: ""
triggers:
  - trigger: time
    at: "00:05:00"
conditions: []
actions:
  - action: homeassistant.update_entity
    metadata: {}
    data:
      entity_id:
        - sensor.propane_tank_neevo_630_gallons_rest
mode: single

I would like to have two sensors, as follows:

  1. A sensor that shows the difference in value between the latest (or current) reading and the value 10 days earlier – in other words, the total amount of propane used over the past 10 days

  2. A sensor that shows the prior 10 day’s moving average of the sensor above (that is, the avergae usage over the past 10 days)

A challenge is that there might be refills of the propane tank, leading to some days showing a higher reading that the prior day. Currently, I use the following code to calculate each day’s difference (hopefully, by taking into account refills, it actually calculates usage, but there is no way (that I know of) to separate figure out any specific day’s usage where there is also a refill of the tank. (To be clear: If the tank starts with 100 gallons in it, and I use 10 gallons of propane that day, and 50 new gallons is delivered, the next reading will show 140 gallons and I will have no way to know if 10 galllons were used and 50 delivered, or (for example) 20 gallons used and 60 delivered.

      - name: "Daily Propane Usage 630 trigger"
        unit_of_measurement: "gal"
        state: >
          {% set midnight = states('input_number.midnight_propane_level_630_claude') | float %}
          {% set current = states('sensor.propane_tank_neevo_630_gallons_rest') | float %}
          {% set refill = states('input_number.propane_refill_amount_630_claude') | float(0) %}
          {% if ((midnight - current) + refill) | round(2) > 0 %}
            {{ ((midnight - current) + refill) | round(2) }}
          {% else %}
            {{ 0 }}
          {% endif %}
        state_class: measurement
        device_class: volume_storage

And:

  # Input numbers used to store daily propane level 630
  midnight_propane_level_630_claude:
    name: "Propane Level at Midnight 630 Claude"
    min: 0
    max: 1000
    step: 0.01
  propane_refill_amount_630_claude:
    name: "Propane Refill Amount Today 630 Claude"
    min: 0
    max: 1000
    step: 0.01

Along with the following automation:

alias: Store Midnight Propane Level and Reset Refill Counter ALL Claude
triggers:
  - at: "23:55:00"
    trigger: time
actions:
  - target:
      entity_id: input_number.midnight_propane_level_630_claude
    data:
      value: "{{ states('sensor.propane_tank_neevo_630_gallons_rest') | float }}"
    action: input_number.set_value
  - target:
      entity_id: input_number.propane_refill_amount_630_claude
    data:
      value: 0
    action: input_number.set_value

I have been working on this for quite a few weeks but because I’m not a programmer and not well versed in any of the required skills, I’m stuck.

I tried getting help from AI, but that was a huge waste of time.

Repeated failures and admissions by AI that it doesn’t know what it’s doing.

Ugh.