Conditional Sensors. My coding arch-nemesis

Hi. I’m pretty new to this whole yaml/HA malarky and have watched countless YT vids and searched on here and yet… I am stil stuck, so help me obi wan…

Ok what I’m trying to do. Is there are two sensors sensor.outdoor_temperature and sensor.outdoor_temperature_2 and only one of them will have a float value, while the other will have a value of unavailable.

What I want to do is If sensor.outdoor_temperature = unavailable then set a new custom sensor sensor.njs_outside_temp to sensor.outdoor_temperature_2 else set sensor.njs_outside_temp to sensor.outdoor_temperature

In every other coding language I know C, VB, heck even cobol dead easy. But this got me stumped. So in my sensors.yaml I have gotten the condition statement to parse but am at a total loss on how to assign the values. The closest I have gotten is

  sensors: ```

#custom sensors that work...


```- sensor:
  - name: njs_outside_temp
    unique_id: njs_outside_temp
    state: >
      {% if is_states('sensor.outdoor_temperature', 'unknown') %}
        {{ states('sensor.outdoor_temperature') }}
        {% else %}
        {{ states('sensor.outdoor_temperature_2') }}
      {% endif %} ```
Which gives a "Configuration warnings
Invalid config for 'sensor' at Sensors.yaml, line 70: required key 'platform' not provided"

Such a simple thing to ask/do right.. Yet this is proving my nemesis. Hope you can help

Seems like you are not following the proper formatting, also…when you paste code, please enclose it between 3 backticks ```

Template - Home Assistant (home-assistant.io)

Sorry, this is my first ever post . i just ctrl-C Ctrl V from the editor into here. I’ve done the backticks in edit if that helps :slight_smile:

Assuming that code comes directly from configuration.yaml, you need

sensor:
  - platform: template
    sensors:
      - name: njs_outside_temp
[...]

Beside the above, keep in mind that “unknown” and “unavailable” are 2 different states.

That’s not correct.

they are using the modern format syntax so it needs to be (also notice the change to “is_state” instead of “is_states”):

template:
  - sensor:
      - name: njs_outside_temp
        unique_id: njs_outside_temp
        state: >
          {% if is_state('sensor.outdoor_temperature', 'unknown') %}
            {{ states('sensor.outdoor_temperature') }}
          {% else %}
            {{ states('sensor.outdoor_temperature_2') }}
          {% endif %}

Otherwise I believe it should work.

Well, my assumption is that he doesn’t (due to the error message) but whichever will do.

Getting much closer now. Thank you. Only issue remaining is that the value of njs_outside_temp is output as “state: > 10.78” . where 10.78 is the what it should be.

The platform: template Sensors: part is defined in my sensors.yaml file, which is included from configuration. yaml

Thank in advance guys.

    njs_outside_temp: 
        unique_id: njs_outside_temp
        friendly_name: "Outside Temp"
        value_template: "
        state: >
          {% if is_state('sensor.outdoor_temperature', 'unknown') %}
            {{ states('sensor.outdoor_temperature') }}
          {% else %}
            {{ states('sensor.outdoor_temperature_2') }}
          {% endif %}"

you are still mixing up the legacy template sensor syntax with the modern legacy sensor syntax.

seeing that most of your syntax is the legacy syntax we’ll stick to that.

“state:” isn’t a valid option for the legacy syntax so the template engine is assuming it’s part of the value_template since everything there is enclosed on quotes.

try this:

njs_outside_temp: 
  unique_id: njs_outside_temp
  friendly_name: "Outside Temp"
  value_template: >
    {% if is_state('sensor.outdoor_temperature', 'unknown') %}
      {{ states('sensor.outdoor_temperature') }}
    {% else %}
      {{ states('sensor.outdoor_temperature_2') }}
    {% endif %}