"from nothing" template sensor starts to be unknown

Hi,

I have the following sensors working till yesterday but then it starts to show as “Unknown”.

- sensors:
    peso_rodolfo:
      friendly_name: "Peso Rodolfo"
      value_template: >-
        {% set weight = states('sensor.mi_body_composition_scale_9c1e_mass') | float %}
          {% if weight >= 69 %}
            {{ states("sensor.mi_body_composition_scale_9c1e_mass") }}
          {% else %}
            {{ states("sensor.peso_rodolfo") }}
          {% endif %}
      unique_id: sensor_peso_rodolfo
      unit_of_measurement: "kg"
      icon_template: mdi:weight-kilogram
- sensors:
    impedancia_rodolfo:
      friendly_name: "Impedancia Rodolfo"
      value_template: >-
        {% set weight = states('sensor.mi_body_composition_scale_9c1e_mass') | float %}
          {% if weight >= 69 %}
            {{ states("sensor.mi_body_composition_scale_9c1e_impedance") }}
          {% else %}
            {{ states("sensor.impedancia_rodolfo") }}
          {% endif %}
      unique_id: sensor_impedancia_rodolfo
      unit_of_measurement: "ohm"
      icon_template: mdi:omega

sensor.mi_body* are working fine

They are configured in template.yaml. And in my configuration.yaml has

template: !include templates.yaml

This type of configuration is not allowed anymore?

Do I need to change to

Template - Home Assistant (home-assistant.io) ?

I’ve read that but I’not sure how to set “state” and “state_class”.

will value_template be state?

Your sensors are defined using legacy configuration. Yet you put them under the template: key which is exclusively for modern configuration.

Legacy sensor configuration must be defined under the sensor: key, not the template: key.

Reference

Template integration


If you have this line in your configuration.yaml file:

sensor: !include sensors.yaml

Then you can put this in the sensors.yaml file:

- platform: template 
  sensors:
    peso_rodolfo:
      friendly_name: "Peso Rodolfo"
      value_template: >-
        {% set weight = states('sensor.mi_body_composition_scale_9c1e_mass') | float %}
          {% if weight >= 69 %}
            {{ states("sensor.mi_body_composition_scale_9c1e_mass") }}
          {% else %}
            {{ states("sensor.peso_rodolfo") }}
          {% endif %}
      unique_id: sensor_peso_rodolfo
      unit_of_measurement: "kg"
      icon_template: mdi:weight-kilogram
    impedancia_rodolfo:
      friendly_name: "Impedancia Rodolfo"
      value_template: >-
        {% set weight = states('sensor.mi_body_composition_scale_9c1e_mass') | float %}
          {% if weight >= 69 %}
            {{ states("sensor.mi_body_composition_scale_9c1e_impedance") }}
          {% else %}
            {{ states("sensor.impedancia_rodolfo") }}
          {% endif %}
      unique_id: sensor_impedancia_rodolfo
      unit_of_measurement: "ohm"
      icon_template: mdi:omega

Alternately, you can convert your sensor’s configuration from legacy to modern format and put it in templates.yaml.

  - sensor:
      - name: "Peso Rodolfo"
        state: >-
          {% set weight = states('sensor.mi_body_composition_scale_9c1e_mass') | float %}
          {% if weight >= 69 %}
            {{ states("sensor.mi_body_composition_scale_9c1e_mass") }}
          {% else %}
            {{ this.state }}
          {% endif %}
        unique_id: sensor_peso_rodolfo
        unit_of_measurement: "kg"
        icon: mdi:weight-kilogram
      - name: "Impedancia Rodolfo"
        state: >-
          {% set weight = states('sensor.mi_body_composition_scale_9c1e_mass') | float %}
          {% if weight >= 69 %}
            {{ states("sensor.mi_body_composition_scale_9c1e_impedance") }}
          {% else %}
            {{ this.state }}
          {% endif %}
      unique_id: sensor_impedancia_rodolfo
      unit_of_measurement: "ohm"
      icon: mdi:omega

Thanks! That worked.

I decided to change to modern to avoid future problems.

1 Like