QUESTION: Template-defined sensor not appearing

Hello. I’m trying to extract the attributes Current consumption, Voltage, and Current attributes from a localtuya-defined smart switch switch.server.

I can see the attribute data:

I’ve defined the sensor as a template as follows, trying two methods I’ve seen in discussions:

Nothing shows up.

Can anyone advise (i) what I’m doing wrong (ii) how I fix it (iii) if there is a better way of doing this?

Thank you!

First off please don’t post screenshots of code. use copy/paste of the yaml directly and make you properly format it using ``` on the line before and after the code block.

If we need to correct the code then that means we need to type the whole thing out from scratch instead of just copy/pasting/editing what you already have.

second, at a minimum you need to remove the second “- sensor:” line from the code. you only can have one of those sections.

last, how did you include the “template:” section in your config?

Did you use an !include to split the config or is it directly in your configuration.yaml?

configuration.yaml:

homeassistant:
  packages: !include_dir_named packages

packages\integrations\template.yaml:

template:
  
    # localtuya Energy sensors
    - sensor:
      - name: "Server Power"
        state: "{{ states.switch.server.attributes.current_consumption }}"
        unit_of_measurement: W
        device_class: power
    
    - sensor:
      - name: "Server Voltage"
        # state: "{{ states.switch.server.attributes.voltage }}"
        state: "{{ state_attr('switch.server', 'voltage') }}"
        unit_of_measurement: V
        device_class: voltage

re:

second, at a minimum you need to remove the second “- sensor:” line from the code. you only can have one of those sections.

I will have a dozen plugs, each with current, power, and voltage. How would you organise the code?

Many thanks :slight_smile:

Since you have sub-folders in the packages directory try this config instead:

homeassistant:
  packages: !include_dir_merge_named packages/
template:
  
    # localtuya Energy sensors
    - sensor:
      - name: "Server Power"
        state: "{{ states.switch.server.attributes.current_consumption }}"
        unit_of_measurement: W
        device_class: power
    
      - name: "Server Voltage"
        # state: "{{ states.switch.server.attributes.voltage }}"
        state: "{{ state_attr('switch.server', 'voltage') }}"
        unit_of_measurement: V
        device_class: voltage

Thank you for looking at this with me - I’ll give this a try

OK. For anyone who may be interested.

Part of my problem arose from two, interacting issues:

  1. The persistence of zombie entities (e.g. sensor.server_power, sensor.server_power_1, sensor.server_power_2, etc.) despite deleting all relevant integrations, removing all relevant configuration code, and restarting Home Asssistant.

  2. The failure the ‘localtuya’ integration to pick up Antela ‘SPL-W-TY-PM-UK’ smart plug power, current, and voltage data after a restart of Home Assistant.

These created (i) entities that never updated (ii) other entities that sometimes updated. I was incorrectly ascribing these to failure of my configuration file specification.

This was solved by:

(i) writing down on a piece of paper the entity name corresponding to the actual feed, and keeping it next to my computer for reference (I kid you not)

(ii) setting a scan interval in localtuya for the device

1 Like