Calculation 2bytefloat type sensor value

Hello,

I am new pretty new in the advanced configuration of home assistant and I am stuck for the moment.

Most of my devices are (or will be) connected at KNX. I am currently integrating my building ventilation system. Here I am struggling with calculating a value of a sensor.

It is regarding the airflow. I am importing the value through KNX. The type is 2 byte float.

knx:
  sensor:
    #Ventilatie: Debiet
    - name: "Debiet (l/u)"
      state_address: "6/2/0"
      type: 2byte_float

This gives the airflow in litres/hour, which is not quite readable value. Now I want to divide this value by 1000, to show mÂł/h. I tried already the solutions in this tread, where they say to use templates, but I do not get it working.

If I use the float-filter, it returns 0, so I am assuming that my sensor.debiet_l_u is not a number because in this case the float-filter returns always 0. That is also why I think it is related to the “2 byte float” type.

Can someone help me with some advice how I can do a calculation with this number?

Thank you.

Hi :wave:!
Please post what you have tried so far.
Every sensors state is a string, that’s why you need to float-filter it first before dividing etc.

Hi Farmio,

I tried already several things, but this comes the closest to the tread about the templates:

sensor:
  - platform: template
    sensors:
      ventilatie_debiet_m3h:
        value_template: "{{ ((states.sensor.debiet_l_u | float) / 1000) | round(0) }}"

The result I get:
Ventilation

As you can see, this result in 0.

Have a look at the current documentation: Template - Home Assistant
It’s states('entity_id') instead of states.entity_id and you are using legacy template sensor format - the Schema nowadays is a little different.

Try this.

template:
   - sensor:
      - name: ventilatie_debiet_m3h:
        state: {{ (states('sensor.debiet_l_u') / 1000)) | round(0) }}

Hello,

I had to search still a little bit and got it working like this:

sensor:
  - platform: template
    sensors:
      ventilatie_debiet_m3h:
        value_template: "{{ ((states('sensor.debiet_l_u') | float) / 1000) | round(0) }}"

It seems the correct documentation for this is Home Assistant - Templating

The Template configuration, like the example of AllHailJ, did not work for me. If I used this, I also could not find the sensor back to use in my dashboard. Next to this, there is expected a string after “state:”.

Thank you all for the help.

The correct new-format syntax would be:

template:
   - sensor:
      - name: "Debiet (m3/h)"
        unit_of_measurement: "mÂł/h"
        state: "{{ ((states('sensor.debiet_l_u') | float) / 1000) | round(0) }}"
1 Like

Apparently, I did something wrong the first time when I tried the new-format syntax. Your code is working also correctly. Because this is the new syntax, I will use this one.

Many thanks!!

1 Like