Entity not available - template value

Hi all,

wonder if anyone can help, I’m having an issue with a template sensor where the value appears to be generated, when I use the template editor, but then when put into the configuration.yaml file, restart template entries, its not available to choose as an entity to display on the lovelace dashboard:

- platform: template
  sensors:
  - name: current_power_usage_w
    unit_of_measurement: W
    value_template: {{ ((states('sensor.shellyem_a4e57cbaa014_channel_1_power')|float) + (states('sensor.shellyem_a4e57cbaa014_channel_2_power')|float) + (states('sensor.shellyem_c45bbe6c32ab_channel_1_power')|float)|int) }}

The 3 sensors are shown in the gauges below, and the current_power_usage_w is expected to be the sum of all these together.

Could anyone advise what is going wrong, and how I can resolve?

Thanks for all help
Tony

When a template is on the same line as the option, the template must be wrapped in quotes. You’re already using single-quotes inside the template so the outer quotes should be double-quotes.

- platform: template
  sensors:
    current_power_usage_w:
      friendly_name: current power usage w
      unit_of_measurement: W
      value_template: "{{ ((states('sensor.shellyem_a4e57cbaa014_channel_1_power')|float) + (states('sensor.shellyem_a4e57cbaa014_channel_2_power')|float) + (states('sensor.shellyem_c45bbe6c32ab_channel_1_power')|float)|int) }}"

Reference: Important Templating Rules

As an alternative, you can add the > line-continuation character and move the entire template to the next line with additional indentation. If you do that then the template doesn’t need outer quotes.

- platform: template
  sensors:
    current_power_usage_w:
      friendly_name: current power usage w
      unit_of_measurement: W
      value_template: >
        {{ ((states('sensor.shellyem_a4e57cbaa014_channel_1_power')|float) + (states('sensor.shellyem_a4e57cbaa014_channel_2_power')|float) + (states('sensor.shellyem_c45bbe6c32ab_channel_1_power')|float)|int) }}

If you are interested, here’s another way to add the values of all three sensors.

- platform: template
  sensors:
    current_power_usage_w:
      friendly_name: current power usage w
      unit_of_measurement: W
      value_template: >
        {{ ['sensor.shellyem_a4e57cbaa014_channel_1_power', 
            'sensor.shellyem_a4e57cbaa014_channel_2_power',
            'sensor.shellyem_c45bbe6c32ab_channel_1_power']
          | map('states') | map('float', 0) | sum | int(0) }}

EDIT

Correction. Troon identified use of modern format in this legacy format Template Sensor. Replaced name with friendly_name and other things. See Troon’s post below.

1 Like

Hi,

thanks for your feedback.

The configuration.yaml file is still being checked, and no issues found - however, the dashboard is not providing the sensor template value. It has worked a few months ago, and since, nothing has changed. The entity is still not available when you connect to the dashboard, and try and add it as an entity.

After modifying the sensor’s configuration, using one of the three methods I described in my previous post, you must execute Developer Tools > YAML > Reload Template Entities to load the sensor’s new configuration.

After reloading, go to Developer Tools > States and confirm that sensor.current_power_usage_w appears in the list.

Hi,

Thanks, I used the template entities, however the entity doesn’t appear still :disappointed_relieved:

If it’s not appearing then it means there’s an error in the sensor’s configuration and Home Assistant has refused to load it. The error will be reported in Logs, so check it for errors.

Where exactly did you put the sensor’s configuration within the configuration.yaml file? It should be placed wherever you currently have other sensors configured. That’s typically under the sensor: key.

Hi there,

thanks again for your help :slight_smile:

The current_power_usage_w is the first entry in a whole list of entities I’ve created, all grouped together:

- platform: template
  sensor:
  - name: current_power_usage_w
    unit_of_measurement: W
    value_template: >
        {{ ['sensor.shellyem_a4e57cbaa014_channel_1_power', 
            'sensor.shellyem_a4e57cbaa014_channel_2_power',
            'sensor.shellyem_c45bbe6c32ab_channel_1_power']
          | map('states') | map('float', 0) | sum | int(0) }}

There are no errors in my configuration.yaml file currently.

Thanks
Tony

That code has sensor: in the second line. Should be sensors: at that point if you’re using the legacy format, and the rest of the formatting is off.

Should be like this, and you should only have one top-level sensor: declaration:

sensor:
  - platform: template
    sensors:
      current_power_usage_w:
        friendly_name: current_power_usage_w
        unit_of_measurement: W
        value_template: >
          {{ ['sensor.shellyem_a4e57cbaa014_channel_1_power', 
              'sensor.shellyem_a4e57cbaa014_channel_2_power',
              'sensor.shellyem_c45bbe6c32ab_channel_1_power']
            | map('states') | map('float', 0) | sum | int(0) }}

Your original was a mixture of modern and legacy format — was it generated by an AI, perhaps?

1 Like

Thanks @Troon, that’s got it! Thanks also to @123 for their help, always much appreciated!

I’ve never been called an AI before, not sure if that’s a complement or not :wink:

1 Like