Create a template sensor failed

Hi, i am trying to setup a template sensor like this:

template:
sensor:
- name: Hausverbrauch
state: {{ states(‘sensor.sn_301456377_metering_power_absorbed’) | float(0) }}

In template editor, the preview windows shows the value needed.

But when integrate it into /config/configuration.yaml i get this error:

Error loading /config/configuration.yaml: invalid key: “OrderedDict([(“states(‘sensor.sn_301456377_metering_power_absorbed’) | float(0)”, None)])”
in “/config/configuration.yaml”, line 20, column 0

No idea what i am doing wrong.

thank you for help.

Seems ok but you might have slipped formatting … please format your config between 3 backtics (```)-

You need quotes around single line templates:

state: "{{ states('sensor.sn_301456377_metering_power_absorbed') | float(0) }}"

This is only copying an existing sensor state. Why?

And now I am interested to know HOW you deducted this to be an existing sensor :slight_smile:

?

All this template does is copy the state of the source sensor. You can’t template non-existent sensors. Therefor I assume it actually exists.

Ah… that way, I was thinking: why could this not be a template-sensor…anyhow…thx

Thank you very much. Works great now!

No problem. Can you explain why you need to duplicate the source sensor?

Yes, sure.

There a two things about that,

  1. I´m new to HA and still have to learn syntax :smiley:

  2. I am using the SMA Solar Integration (SMA Solar - Home Assistant). It´s running fine instantly. Now, i want to use these entities in the Energy Dashboard. It looks like the dashboard display only entities with the device class: energy. But most of SMAs Integration Entities have device class: power, so i am not able to choose them.
    So I thought it maybe make sense to copy the data value from the desired entity to a new template sensor. If I understood it correctly, I can define the device class myself for a template sensor.
    I dont know if this is the right way.

It’s one way of adding a state_class and device_class, but not the best way.

The best way is to open an issue for the integration. These classes should be present.

The other way to do it without creating a new entity is to use customize.

configuration.yaml

homeassistant:
  customize:
    sensor.sn_301456377_metering_power_absorbed:
      device_class: energy
      state_class: total_increasing

Or if you want to put all your customizations in a seperate file:

configuration.yaml

homeassistant:
  customize: !include customize.yaml

customize.yaml

sensor.sn_301456377_metering_power_absorbed:
  device_class: energy
  state_class: total_increasing
1 Like

ah, i am able to override existing sensors and give them a new class.

Thank you.

Not sure if the device classes of the SMA integrations are wrong. Maybe some entities must have the device class power.

I think it where better, when the energy portal does not filter the entities on device class energy.

But i ask the author.

I have read, that all Values for Energy Dashboard must be kWh or kW. My value are Wh or W.
I open an issue.

It never allowed power sensors. It is an Energy dashboard, it only accepts energy sensors.

Changing your power device class to energy will not give you meaningful results. Power and energy sensors are very different things.

I use the SMA integration and all my Sunnyboy inverter sensors have the correct classes.

Wh or kWh is an energy sensor.

W or kW is a power sensor, you can not use this with the energy dashboard.

:man_facepalming:

yes, you are right.

I saw the dashboard as a real-time dashboard for the current consumption, just like it happens in the SMA app. But the dashboard refers to today’s generation for PV generation, house consumption and grid consumption.

Everthings is fine. Thank you again for your support.

looks like this now:

energy1

Dont know if the sensor “metering_total_absorbed” is the right one for grid consumption, but i will see later.