Template: how to define m3 to use in energy dashboard?

Hi,

I want to use my own custom template sensor in the energy dashboard.

In test, I defined following template sensors:

      - name: "gasmeter_total_gas_test1"
        unique_id: "sensor.gasmeter_total_gas_test1"
        icon: mdi:gas-cylinder
        unit_of_measurement: "m3"
        device_class: energy
        state_class: total_increasing
        state: >
            {{ states('sensor.p1_meter_total_gas') | float(default=0.0)  | round(3) }}
        availability: >
            {{ states('sensor.p1_meter_total_gas')  }}

      - name: "gasmeter_total_gas_test2"
        unique_id: "sensor.gasmeter_total_gas_test2"
        icon: mdi:gas-cylinder
        unit_of_measurement: "m3"
        device_class: energy
        state_class: total_increasing
        state: >
            {{ states('sensor.p1_meter_total_gas') | float(default=0.0)  | round(3) }}
        availability: >
            {{ states('sensor.p1_meter_total_gas')  }}

      - name: "gasmeter_total_gas_test3"
        unique_id: "sensor.gasmeter_total_gas_test3"
        icon: mdi:gas-cylinder
        unit_of_measurement: "mÂł"
        device_class: energy
        state_class: total_increasing
        state: >
            {{ states('sensor.p1_meter_total_gas') | float(default=0.0)  | round(3) }}
        availability: >
            {{ states('sensor.p1_meter_total_gas')  }}

      - name: "gasmeter_total_gas_test4"
        unique_id: "sensor.gasmeter_total_gas_test4"
        icon: mdi:gas-cylinder
        unit_of_measurement: "mÂł"
        device_class: energy
        state_class: total_increasing
        state: >
            {{ states('sensor.p1_meter_total_gas') | float(default=0.0)  | round(3) }}
        availability: >
            {{ states('sensor.p1_meter_total_gas')  }}

There seems to be something wrong with the unit_of_measurement definition:

  • I can only add gasmeter_total_gas_test3 and gasmeter_total_gas_test3 to the energy dashboard
  • but even with “m³” defined, I still get following error:

Unexpected unit of measurement

The following entities do not have an expected unit of measurement (either of GJ, kWh, MJ, MWh, Wh for an energy sensor or either of CCF, ftÂł, mÂł for a gas sensor):

  • sensor.gasmeter_total_gas_test3 (mÂł)

My yaml config file is encoded in UTF8…

So my question: how can I define a custom template sensor with m3 to use in my energy dashboard?

kind regards,
Bart

As the error state the device class for a gas with units of m3 is gas and not energy

1 Like

Is there an option to redefine a template sensor?

definitin was

      - name: "Gasmeter Total Gas"
        unique_id: "sensor.gasmeter_total_gas"
        icon: mdi:gas-cylinder
        unit_of_measurement: "m3"
        device_class: energy
        state_class: total_increasing
        state: >
            {{ states('sensor.p1_meter_total_gas') | float(default=0.0)  | round(3) }}
        availability: >
            {{ states('sensor.p1_meter_total_gas')  }}

and is now

      - name: "Gasmeter Total Gas"
        unique_id: "sensor.gasmeter_total_gas"
        icon: mdi:gas-cylinder
        unit_of_measurement: mÂł
        device_class: gas
        state_class: total_increasing
        state: >
            {{ states('sensor.p1_meter_total_gas') | float(default=0.0)  | round(3) }}
        availability: >
            {{ states('sensor.p1_meter_total_gas')  }}

This should work, but I can’t add sensor.gasmeter_total_gas.
What I already did:

  • removed my template
  • restarted HA
  • removed entity from HA
  • added my template
  • restarted HA

but entity is still not available :frowning:

Following just works (same definition, but just another name/id)

      - name: "gasmeter_total_gas_test7"
        unique_id: "sensor.gasmeter_total_gas_test7"
        icon: mdi:gas-cylinder
        unit_of_measurement: mÂł
        device_class: gas
        state_class: total_increasing
        state: >
            {{ states('sensor.p1_meter_total_gas') | float(default=0.0)  | round(3) }}
        availability: >
            {{ states('sensor.p1_meter_total_gas')  }}

any suggestions?

I’m gonna guess it’s because you have historical data that is using the wrong device_class. If you look it up in Developer Tools - Statistics, I THINK you’ll have the ability to fix the issue and have it show up.

Had a similar issue with my gas meter reader when I first set it up.

1 Like

Ben is likely correct. There are other issues though.

Availability templates need to return true (available) or false (unavailable). Yours returns a number.

Try this:

        availability: >
            {{ states('sensor.p1_meter_total_gas') | is_number }}

Also your state template is simply copying the state of sensor.p1_meter_total_gas

Why?

1 Like

Why?

I always make abstraction between my “physical layer” (sensor.p1_meter_total_gas) and “functional layer” (sensor.gasmeter_total_gas).
This has several reasons:

  • If I upgrade my meter to let’s say a p2_meter and want to keep my p1_meter: by just redifine my logical sensor, I do not have to rename anything
  • I have beter control over long term statistics: my logical sensor will alway exist. I can’t say that from the physical one
  • here it’s a 1-1 relation, but other sensors have an offset defined etc…

So in general: it’s an abstraction layer.
I do the same with lights: I never toggel a light, but only lightgroups. Even if they only have one light member.
etc…

It’s my personal organisation. Gives me some work in the beginning, but afterwards you get the benefits.

grtz
B

Err, you have to change the sensor in the template. Which is more work than renaming the new entity id to match the old one, as that can be done in the frontend rather than searching for and messing with YAML.

Again if the entity id’s match there is no advantage to this.

Fine, that’s where you use a template sensor.

I do not fully agree

That’s why I mentioned that I wanted to keep the old one. I.e. I’m not gonna rename my “shelly_plug”’ to “tasmota_plug” just because I started log time ago with a tasmota and now replaced it with a shelly.

Not all integrations handle this well.
I.e. Tuya: you can rename the entity in HA. But when an IP device loses it’s network, you have to remove/add it in the tuya app to reconnect it to you wifi. Than it will get the old tuya-name and you have to rename it again in HA. Sometimes you will also have another name in tuya than in HA → template sensor to have your own name convention, etc…

For long term continuity: If something is broken and I want to keep let’s say total_energy: I can adjust the value of the new sensor by adding/substracting the last value of the broken sensor.

It’s also not always a replacement: I even have more virtual sensors than physical ones.
I.e.

  # ---------------------------------------------------
  - name: "Elektriciteit koelkast power"
    unique_id: "sensor.elektriciteit_koelkast_power"
    icon: mdi:flash
    unit_of_measurement: "W"
    state_class: measurement
    state: >
        {{ "0" | float(default=0.0) }}

A virtual sensor, just set to zero. No powerplug connected connected at this moment, but with a state, showed on my dashboard, on my floorplan etc… The object exist in HA an you can use it in automations, scripts, …

And if I want to measure my “koelkast”, I put my tasmota device in between and modify my yaml to

  # ---------------------------------------------------
  - name: "Elektriciteit koelkast power"
    unique_id: "sensor.elektriciteit_koelkast_power"
    icon: mdi:flash
    unit_of_measurement: "W"
    state_class: measurement
    state: >
      {{ states('sensor.tasmota_108_energy_power') | float(default=0.0) }}
    availability: >
      {{ states('sensor.tasmota_108_energy_power') | is_number }} 

In both situations, my virtual sensor will be available, long term statistics will be intact, no errors, no orphaned entities, etc…
If I want to measure something else: do the same with that virtual sensor

I even did designs in some houses, including automations, graphical designs, dashboards, … without any hardware! Afterwards you just buy the devices they want and rewrite the definition of the lightgroup/logical sensor/… Job done. In fact, I even have a fully working HA instance as template that I can deploy without any hardware attached. Most houses have for 80% same rooms: living room, bathroom, kitchen, bedroom 1,…bedroom 4 etc…

that is true

B

Did u manage to get this working?