Is it possible to include inside sensor.yaml another .yaml file with a sensor?

Hello everyone, I have a question, I have a sensor that are several food items that I use to control the price, this occupies many lines. Is it possible within sensor.yaml relate to another yaml file containing only that code? Type as we do in configuration.yaml that we point to the sensors, groups…

you should be able to use an !include there just as you do anywhere else as long as you make sure the syntax/indentation is maintained.

When I do so, I get the following message:

Invalid config for [sensor]: required key not provided @ data[‘platform’]. Got None. (See /config/configuration.yaml, line 13).

Could you tell me how you would put it, I put the code inside sensor.yaml:

  - sensor: !include alimentos.yaml

  - platform: temperature_feels_like
    source: weather.aemet_daily

  - platform: template
    sensors:
      horas_totales_de_limpieza:
        friendly_name: "Horas Totales de Limpieza"
        unit_of_measurement: 'h'
        value_template: >-
          {% set s = states('sensor.roborock_s6_pure_total_duration')|int %}
          {{ '%02d:%02d' % ((s/3600)|int, (s%3600)/60) }}{## Imitate available variables: ##}

  - platform: template
    sensors:
      duracion_ultima_limpieza:
        friendly_name: "Duración Última Limpieza"
        unit_of_measurement: 'h'
        value_template: >-
          {% set s = states('sensor.roborock_s6_pure_last_clean_duration')|int %}
          {{ '%02d:%02d' % ((s/3600)|int, (s%3600)/60) }}{## Imitate available variables: ##}

try this:

  - !include alimentos.yaml

  - platform: temperature_feels_like
    source: weather.aemet_daily

  - platform: template
    sensors:
      horas_totales_de_limpieza:
        friendly_name: "Horas Totales de Limpieza"
        unit_of_measurement: 'h'
        value_template: >-
          {% set s = states('sensor.roborock_s6_pure_total_duration')|int %}
          {{ '%02d:%02d' % ((s/3600)|int, (s%3600)/60) }}{## Imitate available variables: ##}

  - platform: template
    sensors:
      duracion_ultima_limpieza:
        friendly_name: "Duración Última Limpieza"
        unit_of_measurement: 'h'
        value_template: >-
          {% set s = states('sensor.roborock_s6_pure_last_clean_duration')|int %}
          {{ '%02d:%02d' % ((s/3600)|int, (s%3600)/60) }}{## Imitate available variables: ##}

then it depends on what is included in the alimentos.yaml file (that you didn’t post) so depending on what’s in there you could still get errors.

Instead of trying to include one file of sensors inside another file of sensors, change how your sensor files are stored.

  1. Create a sensors sub-directory (i.e. config/sensors).
  2. Put as many files (containing sensor configurations) as you want in the sensors sub-directory.
  3. In configuration.yaml, modify the sensor: key like this:
sensor: !include_dir_merge_list sensors/
  1. Restart Home Assistant.

On startup, Home Assistant will combine the contents of all files in the sensors sub-directory and assign it to the sensor: key.

4 Likes

As far as I can see what you sent me doesn’t work @finity but the issue with what you comment @123 which I have done before, is that you have to have one file per sensor.

Isn’t it possible to have several sensors like @finity says but it works?

Thank you. Regards.

Do what @123 says, or you’ll just confuse yourself . I have this in configuration.yaml:

sensor: !include_dir_merge_list sensors

and the ./sensors folder contains multiple files looking like this:

- platform: time_date
  display_options:
    - 'time'
    - 'date'

- platform: rest
  name: "Drive camera"
  (etc)

You can have multiple sensors per file (as per my example above). You can even create subfolders for your files under the sensors folder.

The likely reason @finity’s suggestion didn’t work for you was because of the leading hyphen:

sensors: !include sensors.yaml
---- then
  - !include alimentos.yaml
---- then
  - platform: whatever

all combines together to give:

sensors: - - platform: whatever

With that sort of strategy, you have to be really careful with what’s included where. With the !include_dir_merge_list strategy, everything is much simpler.

1 Like

False.

You can have as many files containing as many sensor configurations as you desire.

1 Like

Perfect, thank you very much for your patience and help.

It is much appreciated. Have a nice evening!

Regards

You’re welcome!

Any reason why you ultimately didn’t select it as the solution?

Excuse me, I was wrong to select it 🤦