"Virtual" sensor to guess heater power from its state

Hello,

I would like to track some energy usage for my heating system.
What I have right now:
Heater is controlled by a generic thermostat, let’s call it climate.thermostat here, this part works well without issue.
This thermostat is using a basic switch to drive the heater, switch.heater.
I know that my heater itself is drawing 2kW when it’s running.

From that, I think there is enough information to create a sensor using configuration.yaml that would be at 2000W when switch.heater is on (and then the heater is heating), and 0W when switch.heater is off, and from there, derive another sensor that would aggregate the daily energy used by the heater, that i would be able to plug into the Energy panel of HA.
(Why i don’t want to use a shelly1pm or something like that to measure power consumption precisely? Well, if I can achieve the same result using a few lines of yaml, that’s about the same, and less intrusive right?)

template:
  - sensor:
      - name: heating_power
        state_class: measurement
        unit_of_measurement: "W"
        device_class: energy
        state: >
          {% if states('switch.heater') | state == 'on') }
            {{ 2000 | float }}
          {% else %}
            {{ 0 | float }}
          {% endif %}
      - name: heating_daily_energy
        state_class: measurement
        unit_of_measurement: "kWh"
        device_class: energy
        [...]

I am failing to create those sensors so far. From what I understand, to be able to plug them into the Energy dashboard, the state_class, and device_class attributes must be set.

Thanks. R

Try this:

2 Likes

It seems that the problematic attributes can not be used for modern templates, but in the legacy templates, that might be able to handle them, the required values to get them into the energy dashboard is not supported.

I’m trying powercalc right now (thanks francisp), looks to be exactly what i’m looking for. I’ll follow-up here tomorrow if it works.

What are you talking about. Modern template sensor/binary_sensor can do everything legacy template sensor/binary_sensor can do.

You have many errors in your template.

There’s a number of ways to do this

          {% if is_state('switch.heater', 'on') %}
            2000
          {% else %}
            0
          {% endif %}
          {{ 2000 if is_state('switch.heater', 'on') else 0 }}

          {% if states('switch.heater') == 'on' %}
            2000
          {% else %}
            0
          {% endif %}
          {{ 2000 if states('switch.heater') == 'on'  else 0 }}

Small typo:

is_states
        ^
1 Like