Sensor Template with range numbers not updating correctly

So I have a Qubino Weather Station that I’ve been playing with, and I’m currently working to create a template sensor to reflect the direction of the wind. I have an entity_id called sensor.qubino_zmnhzdx_weather_station_direction_44_7_2, which outputs a number value between 0 and 360, depending on the direction (i.e. 0/360 = North, 90 = East, 180 = South, 270 = West).

So here is the template I have created, currently:

    weather_wind_direction:
      friendly_name: 'Wind Direction'
      value_template: >-
        {% if states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int >= 0 and states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int <= 22.4 %}
            North
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int >= 22.5 and states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int <= 67.4 %}
            North-East
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int >= 67.5 and states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int <= 112.4 %}
            East
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int >= 112.5 and states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int <= 157.4 %}
            South-East
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int >= 157.5 and states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int <= 202.4 %}
            South
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int >= 202.5 and states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int <= 247.4 %}
            South-West
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int >= 247.5 and states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int <= 292.4 %}
            West
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int >= 292.5 and states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int <= 327.4 %}
            North-West
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int >= 327.5 and states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int <= 360 %}
            North
        {% else %}
            FAILED
        {% endif %}

However, the issue I’m running into, is that no mater what number sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 has, this sensor.weather_wind_direction is showing North, which isn’t correct. I.E., the screenshot above shows 157.5 as the state, which should translate to South, but sensor.weather_wind_direction is showing North.

Is there something wrong with my template? I’m not sure where else to go from here.

Thanks!

I wonder if you might benefit from some parentheses to group statements. Have you tested this in the template dev tool? That’s an easy way to test live.

{% if (states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int) >= 0 and (states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int) <= 22.4 %}

or even

{% if ( (states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int) >= 0 ) and ( (states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int) <= 22.4 ) %}

Thanks for the response. I have tried the first option you suggested, and got the same result. I just now tried the second option, and same behavior.

Yes, I have been using the template dev tool for testing. As a test, I moved the North-East if to the top, and the result is still giving me the same North. For whatever reason, the template is always matching to:

        {% if states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int >= 0 and states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int <= 22.4 %}
            North

Regardless of where it’s positioned in the template (so it’s not just matching to the top if). The actual value is 132.5, but it’s matching to >= 0 and <= 22.4. How is that possible? Is the actual value (i.e., 132.5) not being properly represented by an integer in the state, and instead a string? Is it not following the and <= x?

Something interesting I just came across (and I don’t understand why, just yet)…

        {% if states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int > 0 %}
            WORKING
        {% else %}
            FAILED
        {% endif %}

Outputs FAILED. But…

        {% if states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2 | int >= 0 %}
            WORKING
        {% else %}
            FAILED
        {% endif %}

Outputs WORKING. The only difference is > vs >=. This makes me thing that it’s detecting the state as 0, instead of the actual value that’s in there.

So found something interesting. According to:
http://jinja.pocoo.org/docs/dev/templates/#list-of-builtin-filters

For the int filter:

Convert the value into an integer. If the conversion doesn’t work it will return 0. 

So I think that’s what is happening. It’s failing to convert the value to an integer, so it’s returning 0 which matches the North if statement. The float filter gives me the same result.

What is the output of just {{states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2}}

I think you need .state at the end after the numbers

By the way, since it checks the conditions in order after the first one you just need to check if it is less than 64.7 you do not need to re confirm that it is greater than 22.5 since that would have been caught in the condition above it. Then do this for every condition below it as well. Bit more efficient and cleaner looking.

1 Like

@stunts1337
You are a genius. I KNEW it was going to be something simple that I was over looking!

{{states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2}}

Outputs:

<state sensor.qubino_zmnhzdx_weather_station_direction_44_7_2=45.0; node_id=44, friendly_name=Qubino ZMNHZDx Weather Station Direction, battery_level=100 @ 2017-03-03T11:53:49.703312-05:00>

While…

{{states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state}}

Outputs:

45.0

So that is exactly what I was missing!

Thanks for the advise about ditching the other part of the statement, that makes a lot more sense but I wasn’t sure if it processed in order like that. I assumed it did, but wanted to play it safe. I will get that updated, test, confirm, and share here shortly!

1 Like

Cool glad that works, make sure you convert to float instead of int btw

2 Likes

You know what, that would explain the behavior I am currently seeing. Thanks for pointing that out!

Here is my ending template!

    weather_wind_direction:
      friendly_name: 'Wind Direction'
      value_template: >-
        {% if states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state | float < 22.5 %}
            North
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state | float < 67.5 %}
            North-East
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state | float < 112.5 %}
            East
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state | float < 157.5 %}
            South-East
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state | float < 202.5 %}
            South
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state | float < 247.5 %}
            South-West
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state | float < 292.5 %}
            West
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state | float < 327.5 %}
            North-West
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state | float < 360 %}
            North
        {% else %}
            FAILED
        {% endif %}
      icon_template: >-
        {% if states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state | float < 22.5 %}
            mdi:arrow-up
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state | float < 67.5 %}
            mdi:arrow-top-right
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state | float < 112.5 %}
            mdi:arrow-right
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state | float < 157.5 %}
            mdi:arrow-bottom-right
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state | float < 202.5 %}
            mdi:arrow-down
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state | float < 247.5 %}
            mdi:arrow-bottom-left
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state | float < 292.5 %}
            mdi:arrow-left
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state | float < 327.5 %}
            mdi:arrow-top-left
        {% elif states.sensor.qubino_zmnhzdx_weather_station_direction_44_7_2.state | float <= 360 %}
            mdi:arrow-up
        {% else %}
            mdi:duck
        {% endif %}

Seems to be working exactly as I had expected, and the template is half the size of before.

Thanks for the help everyone!

EDIT: Added an icon_template to this sensor. :wink:

2 Likes

I am new to HA and setting up my system. I have a sun blind at my terrace and I would like to control it with HA over z-wave.
What is your experience with Qubino Weather Station, do you recommend it to buy? The most important for me is the wind sensor. It should be reliable to protect the sun blind being destroyed by wind. Thanks.