One of several template sensors doesn't show

I have finally exchanged my RPI to a used desktop windows PC, allowing me to actually restart HA with edited configuration.yaml. With much appreciated help here on the forum I have created some template sensors.

The first one, “Sensor - Sum consumption lights” doesn’t show in HA, but the other two is available and working as expected. As basic touble-shooting I have moved the code to other positions in the file, with the the result “Sensor - Sum consumption sockets” fro example comes first in the file. As expected, this does nothing. “Sensor - Sum consumption sockets” and “Sensor - VVB consumtion” are always available, but lights are not. From reading the code, I can not find the answer to why it doesn’t work. What am I missing here?

# Loads default set of integrations. Do not remove.
default_config:

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
template:
  - sensor:
      - name: 'Sensor - Sum consumption lights'
        state: >
          {{ states("sensor.belysning_kok_channel_1_power")|float(0) 
            + states("sensor.belysning_kok_channel_2_power")|float(0)
            + states("sensor.belysning_vardagsrum_channel_1_power")|float(0)
            + states("sensor.belysning_vardagsrum_channel_2_power")|float(0)
            + states("sensor.belysning_viggos_rum_power")|float(0)
            + states("sensor.shellyswitch25_c45bbe778a5e_channel_1_power")|float(0)
            + states("sensor.shellyswitch25_c45bbe778a5e_channel_2_power")|float(0)
            + states("sensor.shellyplus1pm_44179393335c_power")|float(0)
            + states("sensor.shellyplus1pm_44179394e6ec_power")|float(0)
            + states("sensor.shellyplus1pm_30839809fcd4_power")|float(0)
            + states("sensor.shellyplus1pm_a8032ab9d160_power")|float(0)
            + states("sensor.shellyplus1pm_441793952b48_power")|float(0)
            + states("sensor.shellyplus1pm_4417939324f0_power")|float(0)
            + states("sensor.pub_belysning_bardisk_power")|float(0) )|round(1) }}
        availability: >
          {{ states("sensor.belysning_kok_channel_1_power")|is_number
            and states("sensor.belysning_kok_channel_2_power")|is_number
            and states("sensor.belysning_vardagsrum_channel_1_power")|is_number
            and states("sensor.belysning_vardagsrum_channel_2_power")|is_number
            and states("sensor.belysning_viggos_rum_power")|is_number
            and states("sensor.shellyswitch25_c45bbe778a5e_channel_1_power")|is_number
            and states("sensor.shellyswitch25_c45bbe778a5e_channel_2_power")|is_number
            and states("sensor.shellyplus1pm_44179393335c_power")|is_number
            and states("sensor.shellyplus1pm_44179394e6ec_power")|is_number
            and states("sensor.shellyplus1pm_30839809fcd4_power")|is_number
            and states("sensor.shellyplus1pm_a8032ab9d160_power")|is_number
            and states("sensor.shellyplus1pm_441793952b48_power")|is_number
            and states("sensor.shellyplus1pm_4417939324f0_power")|is_number
            and states("sensor.pub_belysning_bardisk_power")|is_number }}
        unit_of_measurement: 'W'
        device_class: power
        state_class: measurement
  - sensor:
      - name: 'Sensor - Sum consumption sockets'
        state: >
          {{ ( states("sensor.shellyplus1pm_441793952b38_power")|float(0) 
            + states("sensor.shellyplus1pm_441793933308_power")|float(0)
            + states("sensor.uttag_sovrum_tv_power")|float(0)
            + states("sensor.shellyplus1pm_4417939324e8_power")|float(0)
            + states("sensor.shellyplus1pm_a8032ab9b95c_power")|float(0) )|round(1) }}
        availability: >
          {{ states("sensor.shellyplus1pm_441793952b38_power")|is_number
            and states("sensor.shellyplus1pm_441793933308_power")|is_number
            and states("sensor.uttag_sovrum_tv_power")|is_number
            and states("sensor.shellyplus1pm_4417939324e8_power")|is_number
            and states("sensor.shellyplus1pm_a8032ab9b95c_power")|is_number }}
        unit_of_measurement: 'W'
        device_class: power
        state_class: measurement
  - sensor:
      - name: 'Sensor - VVB consumtion'
        state: >
          {{ ( states("sensor.shellyem3_485519c9f729_channel_a_power")|float(0)
            + states("sensor.shellyem3_485519c9f729_channel_b_power")|float(0) )|round(1) }}
        availability: >
          {{ states("sensor.shellyem3_485519c9f729_channel_a_power")|is_number
            and states("sensor.shellyem3_485519c9f729_channel_b_power")|is_number }}
        unit_of_measurement: 'W'
        device_class: power
        state_class: measurement```

Keep this first line containing sensor: and remove the others.

template:
  - sensor:     

What he’s doing is still valid. His issue lies in his first template.

@Micke77 You’re missing a ( on your first line in the state template.

  - sensor:
      - name: 'Sensor - Sum consumption lights'
        state: >
          {{ (states("sensor.belysning_kok_channel_1_power")|float(0) 
            + states("sensor.belysning_kok_channel_2_power")|float(0)
            + states("sensor.belysning_vardagsrum_channel_1_power")|float(0)
            + states("sensor.belysning_vardagsrum_channel_2_power")|float(0)
            + states("sensor.belysning_viggos_rum_power")|float(0)
            + states("sensor.shellyswitch25_c45bbe778a5e_channel_1_power")|float(0)
            + states("sensor.shellyswitch25_c45bbe778a5e_channel_2_power")|float(0)
            + states("sensor.shellyplus1pm_44179393335c_power")|float(0)
            + states("sensor.shellyplus1pm_44179394e6ec_power")|float(0)
            + states("sensor.shellyplus1pm_30839809fcd4_power")|float(0)
            + states("sensor.shellyplus1pm_a8032ab9d160_power")|float(0)
            + states("sensor.shellyplus1pm_441793952b48_power")|float(0)
            + states("sensor.shellyplus1pm_4417939324f0_power")|float(0)
            + states("sensor.pub_belysning_bardisk_power")|float(0) )|round(1) }}
        availability: >
          {{ states("sensor.belysning_kok_channel_1_power")|is_number
            and states("sensor.belysning_kok_channel_2_power")|is_number
            and states("sensor.belysning_vardagsrum_channel_1_power")|is_number
            and states("sensor.belysning_vardagsrum_channel_2_power")|is_number
            and states("sensor.belysning_viggos_rum_power")|is_number
            and states("sensor.shellyswitch25_c45bbe778a5e_channel_1_power")|is_number
            and states("sensor.shellyswitch25_c45bbe778a5e_channel_2_power")|is_number
            and states("sensor.shellyplus1pm_44179393335c_power")|is_number
            and states("sensor.shellyplus1pm_44179394e6ec_power")|is_number
            and states("sensor.shellyplus1pm_30839809fcd4_power")|is_number
            and states("sensor.shellyplus1pm_a8032ab9d160_power")|is_number
            and states("sensor.shellyplus1pm_441793952b48_power")|is_number
            and states("sensor.shellyplus1pm_4417939324f0_power")|is_number
            and states("sensor.pub_belysning_bardisk_power")|is_number }}
        unit_of_measurement: 'W'
        device_class: power
        state_class: measurement
1 Like

For Template Sensors that reference many other entities, it can be useful to put those “other entities” in a group. Then your template can simply reference the group wherever it needs it (instead of duplicating long lists of entities everywhere).

For example:

# Loads default set of integrations. Do not remove.
default_config:

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

group:
  consumption_lights:
    name: "Consumption Lights"
    entities:
      - sensor.belysning_kok_channel_1_power
      - sensor.belysning_kok_channel_2_power
      - sensor.belysning_vardagsrum_channel_1_power
      - sensor.belysning_vardagsrum_channel_2_power
      - sensor.belysning_viggos_rum_power
      - sensor.shellyswitch25_c45bbe778a5e_channel_1_power
      - sensor.shellyswitch25_c45bbe778a5e_channel_2_power
      - sensor.shellyplus1pm_44179393335c_power
      - sensor.shellyplus1pm_44179394e6ec_power
      - sensor.shellyplus1pm_30839809fcd4_power
      - sensor.shellyplus1pm_a8032ab9d160_power
      - sensor.shellyplus1pm_441793952b48_power
      - sensor.shellyplus1pm_4417939324f0_power
      - sensor.pub_belysning_bardisk_power
  consumption_sockets:
    name: "Consumption Sockets"
    entities:
      - sensor.shellyplus1pm_441793952b38_power
      - sensor.shellyplus1pm_441793933308_power
      - sensor.uttag_sovrum_tv_power
      - sensor.shellyplus1pm_4417939324e8_power
      - sensor.shellyplus1pm_a8032ab9b95c_power

template:
  - sensor:
      - name: 'Sensor - Sum consumption lights'
        state: "{{ expand('group.consumption_lights') | map(attribute='state') | map('float') | list | sum | round(1) }}"
        availability: >
          {% set x = expand('group.consumption_lights') | map(attribute='state') | map('is_number') | map('abs') | list %}
          {{ x | sum == x | count }}
        unit_of_measurement: 'W'
        device_class: power
        state_class: measurement

      - name: 'Sensor - Sum consumption sockets'
        state: "{{ expand('group.consumption_sockets') | map(attribute='state') | map('float', 0) | sum | round(1) }}"
        availability: >
          {% set x = expand('group.consumption_sockets') | map(attribute='state')
            | map('is_number') | map('abs') | list %}
          {{ x | sum == x | count }}
        unit_of_measurement: 'W'
        device_class: power
        state_class: measurement

      - name: 'Sensor - VVB consumtion'
        state: >
          {{ ( states("sensor.shellyem3_485519c9f729_channel_a_power")|float(0)
            + states("sensor.shellyem3_485519c9f729_channel_b_power")|float(0) )|round(1) }}
        availability: >
          {{ states("sensor.shellyem3_485519c9f729_channel_a_power")|is_number
            and states("sensor.shellyem3_485519c9f729_channel_b_power")|is_number }}
        unit_of_measurement: 'W'
        device_class: power
        state_class: measurement

The size of the first two template sensors is substantially reduced and their legibility is improved. The third Template Sensor only adds the values of two sensors so it wouldn’t benefit as much from using a group.

Thanks, this worked… half way. It now appears in HA, but as unavailable. Apparently I need a unique ID for this sensor, but not for the others. I have tried the following code, with no success:

      - name: 'Sensor - Sum consumption lights'
        unique_id: sensor.sum_consumtion_lights
        state: >
          {{ ( states("sensor.belysning_kok_channel_1_power")|float(0) 
            + states("sensor.belysning_kok_channel_2_power")|float(0)
            + states("sensor.belysning_vardagsrum_channel_1_power")|float(0)
            + states("sensor.belysning_vardagsrum_channel_2_power")|float(0)
            + states("sensor.belysning_viggos_rum_power")|float(0)
            + states("sensor.shellyswitch25_c45bbe778a5e_channel_1_power")|float(0)
            + states("sensor.shellyswitch25_c45bbe778a5e_channel_2_power")|float(0)
            + states("sensor.shellyplus1pm_44179393335c_power")|float(0)
            + states("sensor.shellyplus1pm_44179394e6ec_power")|float(0)
            + states("sensor.shellyplus1pm_30839809fcd4_power")|float(0)
            + states("sensor.shellyplus1pm_a8032ab9d160_power")|float(0)
            + states("sensor.shellyplus1pm_441793952b48_power")|float(0)
            + states("sensor.shellyplus1pm_4417939324f0_power")|float(0)
            + states("sensor.pub_belysning_bardisk_power")|float(0) )|round(1) }}
        availability: >
          {{ states("sensor.belysning_kok_channel_1_power")|is_number
            and states("sensor.belysning_kok_channel_2_power")|is_number
            and states("sensor.belysning_vardagsrum_channel_1_power")|is_number
            and states("sensor.belysning_vardagsrum_channel_2_power")|is_number
            and states("sensor.belysning_viggos_rum_power")|is_number
            and states("sensor.shellyswitch25_c45bbe778a5e_channel_1_power")|is_number
            and states("sensor.shellyswitch25_c45bbe778a5e_channel_2_power")|is_number
            and states("sensor.shellyplus1pm_44179393335c_power")|is_number
            and states("sensor.shellyplus1pm_44179394e6ec_power")|is_number
            and states("sensor.shellyplus1pm_30839809fcd4_power")|is_number
            and states("sensor.shellyplus1pm_a8032ab9d160_power")|is_number
            and states("sensor.shellyplus1pm_441793952b48_power")|is_number
            and states("sensor.shellyplus1pm_4417939324f0_power")|is_number
            and states("sensor.pub_belysning_bardisk_power")|is_number }}
        unit_of_measurement: 'W'
        device_class: power
        state_class: measurement```

I renamed it from the HA entity tab and am now having two of those (with different names). When removing them from the configuration.yaml and restarting it seems to change nothing. They are both appearing as entities, unavailable, and can not be removed.

Thanks! This made the sensor available.

The two old ones are still in the list of entities, as available, but this new one works as intended. They are reoved from the configuration.yaml and when searching google, there are a lot of those going around. Nothing seems to work for me. Any suggestionson how to get rid of them?

I don’t know what you mean by “old ones”.