Create a sensor for gas consumption

Hi!

I am trying to create a sensor that tracks my consumption of wood pellets which I use for heating. The stove has an API which I have been able to access by adding a sensor to the configuration.yaml. It reads the total consumption of pellets since the stove was installed:


# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Text-to-speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

sensor:
  - platform: rest
    state_class: total
    name: "ETA Gesamtverbrauch"
    scan_interval: 120
    resource: http://192.168.543.12:8080/user/var/40/10021/0/0/12016
    value_template: '{{ value_json.eta.value["@strValue"] | replace(",", ".") | int }}'
    unit_of_measurement: "kg"

I understand that I have to use gas consumption in the energy tab but my sensor does not show up there. (I am able to read the value in different tiles though)

Could anybody kindly help me with adjusting the sensor?

Thank you!

I think you might need device_class: gas, but that requires unit_of_measurement: m³. (You also need state_class: total or state_class: total_increasing, but you already have that.)
If you really want to do it, you could add a template sensor to set the unit of measurement to m³ and device_class: gas (and state_class, to enable long-term statistics for your template sensor). You would need to remember that 1 m³ in the graph equates to 1 kg of pellets, though.

Thank you!
I have tried this but it is not recognized in the energy tab:

template:
  - sensor:
      - name: newPellets
        resource: http://192.168.543.12:8080/user/var/40/10021/0/0/12016
        value_template: '{{ value_json.eta.value["@strValue"] | replace(",", ".") | int }}'
        unit_of_measurement: m³  
        device_class: gas
        state_class: total

Try waiting an hour after adding the sensor. I’m not sure if it can show up before the first hourly long-term statistic data points are available.

Ah, I just got an error (even though it originally passed the config check): Invalid config for [template]: [resource] is an invalid option for [template]. Check: template->sensor->0->resource. (See /config/configuration.yaml, line 31).

It seems it does not like the resource but I have no clue how to add the rest API differently…

Ah, sorry, I didn’t read your config thoroughly enough.
You need to keep your rest sensor and use that in the state template in your template sensor.

Actually, now that I think about it, RESTful sensor already supports everything that you need.
Try this:

sensor:
  - platform: rest
    name: "ETA Gesamtverbrauch"
    scan_interval: 120
    resource: http://192.168.543.12:8080/user/var/40/10021/0/0/12016
    value_template: '{{ value_json.eta.value["@strValue"] | replace(",", ".") | int }}'
    unit_of_measurement: "m³"
    device_class: gas
    state_class: total

You can use this if you are OK with seeing m³ everywhere.
If you want to use the correct unit of measurement (kg) and only fool the energy dashboard into thinking it’s gas, you’ll need to use the template sensor.

Yay, I have the sensor in the energy tab now :slight_smile:
Are you sure I can get “kg” in the energy tab? For me, during setup it says the only possible measures are CCF, ft³, m³, GJ, kWh, MJ, MWh, Wh.
Plus, I think I did not get the template sensor right:

        
sensor:
  - platform: rest
    name: "ETA Sensor"
    scan_interval: 120
    resource: http://192.168.543.12:8080/user/var/40/10021/0/0/12016
    value_template: '{{ value_json.eta.value["@strValue"] | replace(",", ".") | int }}'
    unit_of_measurement: "m³"
    device_class: gas
    state_class: total
    
    
template:
  - sensor:
      - name: "ETA Gesamtverbrauch"
        state: states('sensor.ETA_sensor')
        unit_of_measurement: kg  
        device_class: gas
        state_class: total

Your template sensor would need to be like this:

template:
  - sensor:
      - name: "ETA Gesamtverbrauch"
        state: '{{ states("sensor.ETA_sensor") }}'
        unit_of_measurement: "m³"
        device_class: gas
        state_class: total

while your rest sensor should have unit_of_measurement: kg and no device_class. You would use the template sensor for the energy dashboard only (it will think it’s gas, in m³) and use the rest sensor with the correct unit of measurement (kg) everywhere else.

I am pretty sure you cannot.

I should have chosen better wording here.

Well, would have been nice if kg were possible :wink:

I have tried again, but the template sensor does not show up as an option in the energy tab:

sensor:
  - platform: rest
    name: "ETA Sensor2"
    scan_interval: 120
    resource: http://192.168.543.12:8080/user/var/40/10021/0/0/12016
    value_template: '{{ value_json.eta.value["@strValue"] | replace(",", ".") | int }}'
    unit_of_measurement: "kg"
    #device_class: gas
    state_class: total
    
template:
  - sensor:
      - name: "ETA Gesamtverbrauch"
        state: '{{ states("sensor.eta_sensor2") }}'
        unit_of_measurement: "m³"
        device_class: gas
        state_class: total
 

Does the template sensor have a state? If yes, I don’t see why it shouldn’t work.
Check if you see an error here
Open your Home Assistant instance and show your statistics developer tools.
Maybe you need to wait again.

I don’t think it is possible with Energy dashboard, but you can certainly make custom long-term statistic graphs with something like apexcharts-card.

1 Like

Ah, there was a whole bunch of unused sensors from playing around in the statistics section. I think I have had the name for the template sensor before and it was still counting kg.

Now it works! Thank you!!!

Is this the only place where I have to delete sensors from my experiments? :sweat_smile:

If you can no longer see the old sensors’ states
Open your Home Assistant instance and show your state developer tools.
then you only needed to clean up long-term statistics.
Short-term history will get purged automatically after a few days.