home assistant - Putting sensors in separate YAML file leads to errors - Stack Overflow

时间: 2025-01-06 admin 业界

I recently moved my sensors from configuration.yaml to a separate sensors.yaml because my config was getting crowded. I import sensors.yaml into my configuration.yaml.

Here's an example of my old sensor configuration (converting m³ to liters):

- platform: template
  sensors:
    wasserverbrauch_haus_tag_in_liter:
      friendly_name: "Wasserverbrauch Haus Tag in Liter"
      unit_of_measurement: 'L'
      value_template: "{{ states('sensor.wasserverbrauch_haus_tag') | float * 1000 | round(0) }}"
    
    wasserverbrauch_haus_monat_in_liter:
      friendly_name: "Wasserverbrauch Haus Monat in Liter"
      unit_of_measurement: 'L'
      value_template: "{{ states('sensor.wasserverbrauch_haus_monat') | float * 1000 | round(0) }}"
    # Similar definitions for other sensors...

After establishing my sensors.yaml I received the error that my entities (water) don't have a state class anymore. So I added device_class and state_class accordingly:

    device_class: water
    state_class: measurement

However, that led to another error saying my config was invalid. I was advised to change the structure like this:

template:
  - sensor:
      - name: "Wasserverbrauch Haus Tag in Liter"
        unit_of_measurement: 'L'
        device_class: water
        state_class: measurement
        state: "{{ states('sensor.wasserverbrauch_haus_tag') | float * 1000 | round(0) }}"

But I still receive an error, likely due to the template structure.

How can I properly configure my water sensors and retain the historical data?

I recently moved my sensors from configuration.yaml to a separate sensors.yaml because my config was getting crowded. I import sensors.yaml into my configuration.yaml.

Here's an example of my old sensor configuration (converting m³ to liters):

- platform: template
  sensors:
    wasserverbrauch_haus_tag_in_liter:
      friendly_name: "Wasserverbrauch Haus Tag in Liter"
      unit_of_measurement: 'L'
      value_template: "{{ states('sensor.wasserverbrauch_haus_tag') | float * 1000 | round(0) }}"
    
    wasserverbrauch_haus_monat_in_liter:
      friendly_name: "Wasserverbrauch Haus Monat in Liter"
      unit_of_measurement: 'L'
      value_template: "{{ states('sensor.wasserverbrauch_haus_monat') | float * 1000 | round(0) }}"
    # Similar definitions for other sensors...

After establishing my sensors.yaml I received the error that my entities (water) don't have a state class anymore. So I added device_class and state_class accordingly:

    device_class: water
    state_class: measurement

However, that led to another error saying my config was invalid. I was advised to change the structure like this:

template:
  - sensor:
      - name: "Wasserverbrauch Haus Tag in Liter"
        unit_of_measurement: 'L'
        device_class: water
        state_class: measurement
        state: "{{ states('sensor.wasserverbrauch_haus_tag') | float * 1000 | round(0) }}"

But I still receive an error, likely due to the template structure.

How can I properly configure my water sensors and retain the historical data?

Share Improve this question edited 20 hours ago jonrsharpe 122k30 gold badges263 silver badges469 bronze badges asked 20 hours ago JulianJulian 1051 gold badge4 silver badges8 bronze badges 5
  • How do you import sensors.yaml into configuration.yaml? Like so: sensor: !include path/to/sensor.yaml? – coreuter Commented 5 hours ago
  • # Sensoren auslagern sensor: !include sensors.yaml – Julian Commented 5 hours ago
  • LGTM. Your old configuration looks good to me, too. What version of HA are you using, have you checked that the indentation is correct, and have you restarted HA or just reloaded the config after the changes? You might want to add this information to your question itself. This makes it easier for future readers to get a good overview of the context. – coreuter Commented 5 hours ago
  • First I Had: - platform: template sensors: but this lead to Errors. So ChatGPT recomended: template: - sensor: - name: ..... It feels Like Here is the issue? – Julian Commented 4 hours ago
  • - platform: template sensors: test_sensor: ... is working fine in sensors.yaml for me (just created that sensor). That's why I asked for additional information. – coreuter Commented 4 hours ago
Add a comment  | 

1 Answer 1

Reset to default 0

I think you are mixing the legacy and the modern template sensor configuration.

For the legacy configuration (quote from the docs: "This format still works but is no longer recommended") to work with a separate sensors.yaml, your configuration.yaml should contain the following line

# configuration.yaml
sensor: !include path/to/sensors.yaml # adjust path as needed

and sensors.yaml should contain the template sensor configured like this

- platform: template
  sensors:
    test_sensor:
      friendly_name: "Test Sensor in sensors.yaml"
      value_template: "{{ True }}" # required
      # ...

For the modern template sensor configuration to work, your configuration.yaml should look like this

template: !include path/to/templates.yaml # adjust path as needed

and templates.yaml should follow the following format

- sensor:
    - name: "Test Sensor in templates.yaml" # NOTE 'name' instead of 'friendly_name'
      state: "{{ True }}" # required # NOTE 'state' instead of 'value_template'

Additional sensor variables for both configurations are described in the documentation linked above.

Notes:

  • The filename used in configuration.yaml does not matter. What's important is that the key (sensor: !include ..., template: !include ..., ...) matches the format/structure of the content included in that file.

Last tested on Home Assistant 2024.12

最新文章