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

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??

Your code looks awesome - and I would test it - if the energy settings hadn’t disappeared from my control panel in Home Assistant with the latest updates…

I think you have the magic code that will handle propane energy from things like Tank Utility and others!

Now if I could just find where that config went…

In case anyone is following this thread for directions, the latest 2022 update had a breaking change to the location of the “Energy” feature. Its now underneath Configuration → Dashboards → Energy.

UPDATE
Error in Code Above - There is an error in my code for the automation. Maybe you or someone else can tell me what I am doing wrong.

- id: '1643814344142'
  alias: Percent Propane Yesterday Input Automation
  description: ''
  trigger:
  - platform: time
    at: '23:59:50'
  condition: []
  action:
  - service: input_number.set_value
    target:
      entity_id: input_number.yesterdays_input_number
    data:
      value_template: '{{((states(''sensor.tank_percentage'')) | float) }} '
  mode: single
- id: '1643898478033'
  alias: Set Todays Input Propane Percent Used
  description: ''
  trigger:
  - platform: time
    at: '23:59:00'
  condition: []
  action:
  - 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}}'
  mode: single

ERROR MESSAGES - 2

Error while executing automation automation.percent_propane_today_input_automation: extra keys not allowed @ data[‘value_template’]

Percent Propane Yesterday Input Automation: Error executing script. Invalid data for call_service at pos 1: extra keys not allowed @ data[‘value_template’]

Revised yaml to fix value_template and quotes. Will know more after I use some propane and test.

- id: '1643814344142'
  alias: Percent Propane Yesterday Input Automation
  description: ''
  trigger:
  - platform: time
    at: '23:59:50'
  condition: []
  action:
  - service: input_number.set_value
    target:
      entity_id: input_number.yesterdays_input_number
    data:
      value: "{{((states('sensor.tank_percentage')) | float) }}"
  mode: single
- id: '1643898478033'
  alias: Set Todays Input Propane Percent Used
  description: ''
  trigger:
  - platform: time
    at: '23:59:00'
  condition: []
  action:
  - 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}}"
  mode: single
1 Like

Any luck? I am still trying to get mine to display.

Yes seems to be working. Not sure about day to day sum. May need to change the settings to not reset everyday because it is a consumption sensor (goes positive one day and when nothing is used goes negative amt)