What is wrong with sensors.yaml

I put this code in sensors.yaml but not valid

 sensors:
  - platform: template
    number_lights_on:
      friendly_name: "Number Lights On"
      value_template: >
        {{ states.light
          | rejectattr('attributes.entity_id', 'defined')
          | selectattr('state', 'eq', 'on')
          | list | count }}
      icon_template: "mdi:lightbulb-group"
  - platform: tcp
    anthem_up:
      host: 192.168.1.59
      port: 14000
      payload: "xyz"

I could not figure out how one sensor being template other sensor being tcp can be placed in sensors.yaml

Remove the first line. Currently your configuration.yaml file !include should be this:

sensor: !include sensors.yaml

Which with your file shown above would resolve to:

sensor:
  sensor:
    - platform: etc...

When it should be:

sensor:
  - platform: etc...
1 Like

What Tom said… but with one addition… you need sensors too:

  - platform: template
    sensors:
      number_lights_on:
        friendly_name: "Number Lights On"
        value_template: >
          {{ states.light
            | rejectattr('attributes.entity_id', 'defined')
            | selectattr('state', 'eq', 'on')
            | list | count }}
        icon_template: "mdi:lightbulb-group"

  - platform: tcp
    anthem_up:
      host: 192.168.1.59
      port: 14000
      payload: "xyz"
2 Likes

Thanks. Now I understood that when there is 'sensor: !include sensor.yaml" the prefiix here should dictate not to have in sensoer.yaml file. Thanks for the clarification

It is confusing to use sensor: and sensors:. My understanding now is sensor: is coming from configuration.yaml. So it need not be placed in the new file.

I want to clarify these.

  1. why you put sensors: for template but not for tcp type?
  2. If I wanted to have platform tcp and also template to parse response with templates how do I do that? Is it possible to use both tcp and template platforms for one of the sensor?

Thanks for help. I am getting better but still trying to understand.

Likely it’s because the tcp platform does not allow you to configure multiple sensors under a single instantiation, and the template platform does… So template requires sensors and says so in the docs:

image

Can you clarify what you are asking?

But isn’t it easier to put template sensors under “template”?
https://www.home-assistant.io/integrations/template/
That way you don’t have to restart HA each time you change sensor config, but only restart templates. It’s also new, advised method.
I think it’s also explained HERE

Appreciate for the explanation. I am getting better grasping the concept. So, this is individual integration that dictates the yaml definitions.

Looks like platform template makes the value for that sensor available as a result of executing code of value_template.

For tcp platform, it seems ‘value’ has result but supports value_template attribute also.

If there is need for a platform let us say ‘xyz’ need to be defined in yaml file but that also needs template platform, is it possible? Kind of two platforms need to be inherited for given sensor etc.

But it makes sense that how the integration developed and exposed as platform decides what it can do.

I will read more the link on template and also other platforms.

sorry. what do you mean putting template sensor under 'template? Rightnow, I have !include in configruation file and sensors in sensor.yaml. But I do have tcp type of sensors defined in the same file. I have to restart HA to make changes.

It definitely helps on how to organize to reduce restarts of complete system instead of realoading configuration yaml.

There are two formats one can use for template sensors. You are using the older one. Since that format uses the top-level key sensor, it can be use in sensors.yaml with your other sensor platforms like tcp.

The preferred format is the newer format that Pavel linked above, but you cannot use it in sensors.yaml because it requires template to be the top-level key. Basic state-based template sensors like the number_lights_on sensor from the original post can also be set up in the frontend from the Helpers menu.

In the TCP sensor configuration value_template is an optional configuration variable. When used, the state of the sensor will be whatever the template returns instead of the string/object returned directly from the TCP call. This allows you to modify/manipulate the data returned by the TCP call without creating a separate sensor.

Again, I’m not sure I understand exactly what you are asking here… Home Assistant does not have any sensors that require multiple platforms.

If you want to modify or manipulate the values that sensor.xyz returns, you can set up a Template sensor that is based off the state or any attribute of sensor.xyz.

1 Like