Bitshift & conversion to IEEE-754 float

Hello, I’ve got 2 sensors from modbus device, and I need to merge them info single float32 sensor.

    sensors:
      - name: "CS Vbus1"
        unique_id: cs_vbus1
        slave: 90
        address: 0
        input_type: holding
        data_type: uint16
        precision: 0
        scan_interval: 3
      - name: "CS Vbus2"
        unique_id: cs_vbus2
        slave: 90
        address: 1
        input_type: holding
        data_type: uint16
        precision: 0
        scan_interval: 3

And what I need:
cs_vbus1 = 54526 / 0xD4FE / b1101010011111110
cs_vbus2 = 16807 / 0x41A7 / b0100000110100111

b0100000110011111 b1101110100101111 / 0x41A7D4FE = 20.9790000916

Basically, I need to create float32 variable, assign cs_vsbus1 to it, bitshift left by 16 and logical OR cs_value2. How to create such template sensor? Thanks!

Found that!

  - name: "CS Vbus"
    unique_id: cs_vbus
    slave: 90
    address: 0
    input_type: holding
    data_type: float32
    swap: word
    precision: 5
    scan_interval: 3

Returns valid voltage: 20.97800V

Realise you’ve solved it, but another way:

{% from 'ieee754.jinja' import ieee754_float %}
{{ ieee754_float("41A7D4FE") }}

Returns 20.979000091552734.

Using your existing sensors, assuming they contain decimal values as strings:

{% from 'ieee754.jinja' import ieee754_float %}
{{ ieee754_float('%0x%0x' %
                 (states('sensor.cs_vbus2')|int(0),
                  states('sensor.cs_vbus1')|int(0)) }}
1 Like

Thanks, not needed anymore, now I don’t need any calculations, was just a question of correct configuration of a sensor.

1 Like

Thanks for the tip, the Jinja macro has just saved my day, especially for converting from float 2 hex to get little endian format from user input.

{% from 'ieee754.jinja' import float_ieee754_single %}
{{ float_ieee754_single(FLOAT NUMBER) }}

1 Like