How would one write a function/template for propane gas and energy monitoring?

You need three sensors .

{% set yesterday_percent = 37.6 %}
{% set today_percent = 80.2 %}
{% set filled_percent = 44.11 %}
{{ (yesterday_percent + filled_percent - today_percent) | round(2) }}

So one place to hold yesterdays reading, one sensor to hold today reading and one to hole today’s filled percentage. And a sensor to get todays percentage used as shown above (this is what you need to calculate daily percentage). An automation to zero out the filled percentage for the next day.

Does this make sense?

here it is in the developer

This makes sense. I will have to do a bit more logic though, as the set percentages are not consistent, so I will have to read and calculate each one, but this is closer to working for filling.

Mr. Flibble - were you able to make any progress on this. Not sure I follow all the logic?

I understand all the logic, I haven’t been able to get mine to work with the basics yet though. The object shows up:

But it isn’t writing any data. So that is what I need to work through first, is to get the sensor to actually provide updates. Once I have that, I need to figure out the way Jinja2 really wants me to define the 3 sensors to handle that.

So no, not there yet.

The sensor is updating though:

This is what I have currently… I am not getting my sensor to show up in the energy panel??
How did you get that done?
This is the logic I am working on. I can query yesterdays_propane_percent then subtract from todays_propane_percent.

Not sure if the formatting is correct?

  - platform: template
    sensors:
      tank_gallons:
        value_template: "{{(((states.sensor.tank_percentage.state) | int)*4) }}"


#get value of tank percentage from prior day at 11:59 pm
      yesterdays_propane_percent:
        value_template: >-
          {% if now().hour == 23 and now().minute == 59 -%}
            {{ states('sensor.tank.percentage') }}
          {%- endif %}

# calculate propane BTUs - my tank each percentage change is 4 gals, thus 453577/% for 5 gallons = therefore 0.8 for 4 gal
# 1 BTU = 0.00029307107017 kWh  = 106.344237436     
      
      todays_propane_kwh:
        value_template: "{{(((states.propane_used_percent.state) | int)*106.344237436) }}"
        unit_of_measurement: kWh
#        last_reset: '1970-01-01T00:00:00+00:00'
      
# trigger template daily propane usage
# sensor.tank.percentage is the current tank percent full from website

template:
  - trigger:
      - platform: time
        at: '23:59:59'  
      - platform: event
        event_type: event_template_reloaded
      - platform: homeassistant
        event: start
  sensor:
      - name: "Propane Used"
        unit_of_measurement: "%"
        unique_id: "propane_used_percent"
        state: >-
         {% set todays_propane_percent = (states('sensor.tank_percentage')) | float(0) %}
         {% if todays_propane_percent < 0 %} 
           {% set propane_used_percent = 0 %}
           {% set propane_filled_percent = (todays_propane_percent - yesterdays_propane_percent) %}
         {% else %} 
         {{ (todays_propane_percent - yesterdays_propane_percent) }}
         {% endif %}

      
utility_meter:
  energy:
    source: sensor.todays_propane_kwh
    cycle: daily
    net_consumption: true

I think my problem is related the recorder not holding states:

Here is my config:

template:
  - trigger:
      - platform: time
        at: '23:59:59'  # Time to execute calculation daily
      - platform: event
        event_type: event_template_reloaded
      - platform: homeassistant
        event: start
    sensor:
      - name: "Propane Used"
        unit_of_measurement: "kWh"
        unique_id: "propane_used"
        state: >-
         {% set todaygas = (states('sensor.tank_utility_00350025333031310a473832')) | float(0) %}
         {{ todaygas * 453577 * 0.00029307107017 }}

utility_meter:
  daily_energy_propane:
    source: sensor.propane_used
    name: '24h Propane'

ok I fixed my issue with it not registering in the Energy panel

changed utility meter setting to–

utility_meter:
  daily_energy_propane:
    source: sensor.todays_propane_kwh
    name: 'Propane Used Last 24h'
    cycle: daily
    net_consumption: true

After putting in the code you see, I went to → Configuration → Energy → Gas Consumption

Then I selected “Add A Gas Source”

Note, that error “Entity Not Defined” error you seen in the screenshot went away after my system first went past the 24 hour count cycle. It has since reappeared as I have been testing various permutations and editing my configs.

Yes, finally got it working by changing my utility meter yaml.
I need to wait a few days and see what gets reported. I will report back to you

Thanks! let me know if your sensors start working, the fact that mine are buggered in recorder seems to be the root cause of why mine isn’t working yet.

I think this is working for the most part. I will know more tonight to see if it calculates correctly. The main issue I am having is that "yesterdays_propane_percent " value does not stay persistent after a restart. So if the system restarts then it will not calculate the usage for the day. I am not sure how to program it to do another way. Open to suggestions…

    sensors:
      tank_gallons:
        value_template: "{{(((states('sensor.tank_percentage')) | int)*4) }}"
        unit_of_measurement: "gal"
        
      todays_propane_percent:
        value_template: "{{((states('sensor.tank_percentage')) | int) }} "
        unit_of_measurement: "%"
        
######
      temporary_propane_used_today:
        value_template: >-
          {% set y = states('sensor.yesterdays_propane_percent') | float %}
          {% set t = states('sensor.todays_propane_percent') | float %}
          {{ (y-t)}}
        unit_of_measurement: "%"
        
# calculate propane BTUs - my tank each percentage change is 4 gals, thus 453577/% for 5 gallons = therefore 0.8 for 4 gal
# 1 BTU = 0.00029307107017 kWh  = 106.344237436     
      
      todays_propane_kwh:
        value_template: "{{ (((states('sensor.propane_used_percent') | int) * 106.344237436)) }}"
        unit_of_measurement: kWh

# trigger template daily propane usage
# sensor.tank.percentage is the current tank percent full from website

template:
  - trigger:
    - platform: time
      at: "23:59:50"
    - platform: event
      event_type: event_template_reloaded
    - platform: homeassistant
      event: start
    sensor:
      - name: "Propane Used Percent"
        unit_of_measurement: "%"
        unique_id: "propane_used_percent"
        state: >-
         {% if (states('sensor.temporary_propane_used_today') | float) < 0 %} 
           0
           {% set propane_filled_percent = states('sensor.temporary_propane_used') | float %}
         {% else %} 
           {{ states('sensor.temporary_propane_percent_used') | float }}
         {% endif %}
         
# reset yesterdays propanne percent
  - trigger:
      - platform: time
        at: "23:59:59"  
      - platform: event
        event_type: event_template_reloaded
      - platform: homeassistant
        event: start
    sensor:
      - name: "Yesterdays Propane Percent"
        unit_of_measurement: "%"
        unique_id: "yesterdays_propane_percent"
        state: >-
          {{ (states('sensor.tank_percentage')) }}
        
utility_meter:
  daily_energy_propane:
    source: sensor.todays_propane_kwh
    name: 'Propane Used Last 24h'
    cycle: daily
    net_consumption: true

I will try your code out on my instance, mine is still more or less blank, which isn’t helpful.

I found a post here about retaining template sensors through a reboot, I assume the code in here will be portable to this code:

I looked at that. Not very helpful to me. Maybe you can determine how to make it work with my setup so far???

The code does not seen to work for the template trigger (at 23:59:50) to get the values into propane_used_percent - not sure why. I do not see an issue with the yaml??

Any suggestions?

store the percentage in a input_number helper via an automation instead of making a template sensor.

Petro - thanks for your help. I have done some reading on input_number…but still have questions

I need the propane_used_percent in a sensor so that I can calculate usage - Correct???

I therefore assume you mean to not use the temporary_propane_used_today sensor and store the difference of yesterdays_propane_percent -todays_propane_percent in the input.daily_propane_percent_used…

Therefore

Goto Helpers and create new helper called input.number.daily_propane_percent_used (as a number type)
Go to automations
new automation with trigger time of 23:59:50.
Action: Choose Call service-inout.number.set_value
Target: Choose input.number.daily_propane_percent_used

Question #1:
Not sure how to get the value from the temporary sensor into the input.number? It will only let me enter a number into the value field. instead of the states (‘temporary_propane_Percent_used’)???
I even tried to edit the yaml but it will not save??? (see below)

Question #2: After the #1 is fixed how do I get the input.number.daily_propane_percent_used into my propane_percent_used_today location so I can used it to calculate consumption? Do I use the same yaml code but evaluate the state of the input.number??..If <0 the set propane_used to 0 else set to …states(‘input.number.daily_propane_percent_used’) | float)?

service: input_number.set_value
target:
  entity_id: input_number.daily_propane_percent_used
data:
  value: 0

OK. I think I got it all working. This is what I did. The values appear to be preserved for restarts. The only thing that it lacks is that on the day the tank is filled … it will not calculate any usage and it does not tell you how much was filled in the tank. This can be added into the calculations if you want once you get it working.

ONE QUESTION
From the research I have done it appears the it is about 27kWh for each gallon of LPG. On cold days my tank uses about 1% which is 5 gallons. That correlates to 135 kWh…that seems like a lot ???

Does that make sense for usage kWh usage? I only use 5 kWh for electricity…???


Programming Info:

  1. I get my value for the propane tank filled percentage (tank_percentage) from a website (you may get yours from a sensor.)
  2. Created three sensors - gallons in tank, today’s propane_percentage, and todays_propane_kWh - energy consumption
  3. Create daily and hourly utility_meters for propane (see below)
  4. I then created two helpers and two automations.
    a. Helpers: yesterdays_propane_percent input number, and daily_propane_percent input number
    b. Automations -
    a. Automation 1 runs at 23:59:00: Set todays propane usage… (It gets the max of the value therefore if negative (ie the tank was filled it will report 0)
service: input_number.set_value
target:
  entity_id: input_number.daily_propane_percent_used
data:
  value: >-
    {% set propaneused = (states('input_number.yesterdays_input_number') | int)
    - (states('sensor.tank_percentage') | int), 0 %}  {{ propaneused|max}}

b. Automation 2: runs at 23:59:59…Then just before midnight set the end of the day propane percent for the prior day

service: input_number.set_value
target:
  entity_id: input_number.yesterdays_input_number
data:
  value: {{(states('sensor.tank_percentage') | int)}}

Three sensors and Utility_meters info:

    sensors:
      tank_gallons:
        value_template: "{{(((states('sensor.tank_percentage')) | float)*5.2) }}"
        unit_of_measurement: "gal"
        
      todays_propane_percent:
        value_template: "{{((states('sensor.tank_percentage')) | float) }} "
        unit_of_measurement: "%"
      
        
# calculate propane kWh - my tank... each percentage change is approx.5.2 gals, thus for each % of 5.2 gallons ~= 140 kWh  From websites   
      
      todays_propane_kwh:
        value_template: "{{ (((states('input_number.daily_propane_percent_used') | float) * 140)) }}"
        unit_of_measurement: kWh

# Utility Meters
 
utility_meter:
  daily_energy_propane:
    source: sensor.todays_propane_kwh
    name: 'Propane Used Last 24h'
    cycle: daily
    net_consumption: true
    
  hourly_energy_propane:
    source: sensor.todays_propane_kwh
    name: 'Propane Used Last Hour'
    cycle: hourly
    net_consumption: true

A gallon of propane = 91690 btu +/-

1 btu = 0.00029307107017 kWh

1 gallon of propane = 91690 * 0.00029307107017 = 26.87 kWh.

Hydrocarbons are a dense form of energy and why they are a great fuel.

134.4 kWh is correct. Electric resistive heat is expensive

Thanks. I had to edit the automation in the automations.yaml. I could not get it to work thru the GUI for automations. Not sure why??