Sensor value_template not working (TP-Link HS110 - total energy attribute)

Hey guys,

something is wrong with my configuration. I want to show the total energy consumption from one of my TP-Link HS110 switches in my dashboard and defined the following sensor in the configuration.yaml file.

sensor:
  - platform: template
    sensors:
      flimheizcurrent:
        value_template: '{{ states.switch.flimheiz.attributes["current_power_w"] | replace(" W", "") | float }}'
        unit_of_measurement: 'W'
      flimheiztotal:
        value_template: '{{ states.switch.flimheiz.attributes["total_energy_kwh"] | replace(" kW", "") | float }}'
        unit_of_measurement: 'kwh'

By using the above statement I get the right value in the template editor but nothing shows up in my dashboard. Did I miss something?

The parser has to figure out which entities are referenced so the sensor can watch for their state changes, and given the way you wrote the template, maybe it’s getting confused. I would try this instead:

sensor:
  - platform: template
    sensors:
      flimheizcurrent:
        value_template: '{{ state_attr("switch.flimheiz","current_power_w") | replace(" W", "") | float }}'
        unit_of_measurement: 'W'
      flimheiztotal:
        value_template: '{{ state_attr("switch.flimheiz","total_energy_kwh") | replace(" kW", "") | float }}'
        unit_of_measurement: 'kwh'

If that doesn’t work, are there any errors in the log that might help?

I’m really not sure what has changed but it is working now. Anyways thanks for your help.