Template use state 2 times math

Hi folks,
I’m really new in HA and got some troubles with the template stuff…
I’ve built a weather station using a esp and some sensors, one of the sensors is a anemometer from which i just count the rotations and send it with mqtt to my HA which is hosted on a rpi3.

I dont thought it would be some trouble to convert the rps to km/h or m/s, but i cant get it run…

the formular is 1.761/(1+rps)+3.013*rps

if i try it with the template tool everything works fine:

{{ (1.761 / (1+ states('sensor.windmast_windgeschwindigkeit')|float)
+ 3.013 * states('sensor.windmast_windgeschwindigkeit')|float) |round(1)}}

but if i edit the sensor in the configuration.yaml the state of the sensor goes up and up and up, after minutes its a few million km/h :smile:

 - platform: mqtt
    name: "Windmast_Windgeschwindigkeit"
    unit_of_measurement: "km/h"
    state_topic: "NODE_4/wind/Count"
    value_template: "{{ (1.761 / (1+ states('sensor.windmast_windgeschwindigkeit')|float) + 3.013 * states('sensor.windmast_windgeschwindigkeit')|float) |round(1)}}"

i think the template increase itselfs everytime. Is there a way to declare a variable in the template or someone of you got a plan how i can handle that?

Thanks alot

the value coming from MQTT is value or value_json, so that’s what you should be using in place of states(‘sensor.windmast_windgeschwindigkeit’).

  - platform: mqtt
    name: "Windmast_Windgeschwindigkeit"
    unit_of_measurement: "km/h"
    state_topic: "NODE_4/wind/Count"
    value_template: "{{ (1.761 / (1+ value|float) + 3.013 * value|float) |round(1)}}"
1 Like

holy … so fast and so simple…
big thanks @petro

Can someone maybe tell me how i could filter out all values below a number?

I try to set the sensor output to 0 if the incoming mqqt is below 2 or so…

thank you again

Try somethine like that:

- platform: mqtt
    name: "Windmast_Windgeschwindigkeit"
    unit_of_measurement: "km/h"
    state_topic: "NODE_4/wind/Count"
	value_template: >-
		{% if value|float < 2}
			0
		{% else %}
			{{ (1.761 / (1+ value|float) + 3.013 * value|float) |round(1)}}
		{% endif %}   

Or in one line:

- platform: mqtt
    name: "Windmast_Windgeschwindigkeit"
    unit_of_measurement: "km/h"
    state_topic: "NODE_4/wind/Count"
	value_template: "{{ 0 if value|float < 2 else (1.761 / (1+ value|float) + 3.013 * value|float) |round(1) }}"
1 Like

thx for the fast reply, i’ve tried both of yours but get the same error:

Error loading /config/configuration.yaml: while scanning for the next token
found character '\t' that cannot start any token
in "/config/configuration.yaml", line 79, column 1

this is my complete sensor

  - platform: mqtt
    name: "Windmast_Windgeschwindigkeit"
    unit_of_measurement: "km/h"
    state_topic: "NODE_4/wind/Count"
	value_template: >-
		{% if value|float < 2}
			0
		{% else %}
			{{ (1.761 / (1+ value|float) + 3.013 * value|float) |round(1)}}
		{% endif %}   

\t is a tab character. Use spaces in your indentation. [docs, 6th paragraph down]

Also, you’re missing a % at the end of your if line:

		{% if value|float < 2}

should be

        {% if value|float < 2 %}
1 Like

shame on me… thx a lot mate