Struggle with templating a sensor based on anothers output

So like everyone i have the weather.installation sensor but its output of words is not helpful to me. I basically want a number variant of this sensor with sunny being 0 partly cloudy being 1 and everything else being 2. This would then allow me to have binary sensors as triggers from the simple number output.

Here is my simplifying sensor:

- sensor:
  - name: "Cloud O Meter"
        unique_id: cloud_o_meter
        state: >-
          {% if is_state(weather.installation, 'Sunny') %}
            0
          {% elif is_state(weather.installation, 'Partly Cloudy') %}
            1
          {% else  %}
            2
          {% endif %}

Here is my a binary sensor using the simplified sensor:

- binary_sensor:
      - name: "Cloudy Weather"
        unique_id: cloudy_weather
        state: >-
          {% if states.cloud_o_meter.state > 0 %}
            off
          {% else %}
            on
          {% endif %}

What am i missing? I’m sure its something stupid like yaml formatting.

You missed the single quotes around weather.installation in is_state().

However I don’t see why the number sensor is necessary. Just go straight to the binary sensor.

- binary_sensor:
    - name: "Cloudy Weather"
      unique_id: cloudy_weather
      state: "{{ not is_state('weather.installation', 'Sunny') }}

The reason for the extra delineation is sunny isn’t the only state i am using.
However, this did sort it which is wonderful.
Here is the final file:

- sensor:
      - name: "Cloud O Meter"
        unique_id: cloud_o_meter
        state: >-
          {% if is_state('weather.installation', 'Sunny') %}
            0
          {% elif is_state('weather.installation', 'Partly CLoudy') %}
            1
          {% else  %}
            2
          {% endif %}
          
    - binary_sensor:
      - name: "Cloudy Weather"
        unique_id: cloudy_weather
        state: >-
          {% if states('sensor.cloud_o_meter') | int > 0 %}
            off
          {% else %}
            on
          {% endif %}
      - name: "Dark Weather"
        unique_id: dark_weather
        state: >-
          {% if states('sensor.cloud_o_meter') | int > 1 %}
            off
          {% else %}
            on
          {% endif %}

Ok, but you can still simply your binary sensors. The state templates just have to return true or false for binary sensors (though the booleans on and off do work as well, as you found). These templates return true or false, that the binary sensor state template can understand:

    - binary_sensor:
      - name: "Cloudy Weather"
        unique_id: cloudy_weather
        state: >
          {{ states('sensor.cloud_o_meter') | int > 0 }}
      - name: "Dark Weather"
        unique_id: dark_weather
        state: >
          {{ states('sensor.cloud_o_meter') | int > 1 }}

But you need to reverse the comparisons get the output OP wanted in the original.

1 Like

Good catch. I missed that.

Don’t forget to supply int with a default value in case the result of the states function is non-numeric.

I dont think that is likely. There’s the catch all case of “else 2” in the source sensor.