Rounding of sensor values

To answer my interrogation, I made a test and I was actually right. It is NOT equivalent rounding the value before and after. So rounding the value before all computations will make the final value different from the theoretical value

1 Like

Hi. Can someone guide me in the right direction on how to me to round a value obtained via a webhook through Node Red and mqtt?

nodered2

In configuration.yaml i have this.

# Brewfather Integration
sensor:
  - platform: mqtt
    name: "iSpindel Batterylevel"
    state_topic: "brew/ferment/tilt/battery"
    unit_of_measurement: "Volt"

I use the calculator node to round the figures on my smart meter readings -

image

Thank you for response. Can i put the calculator node between the battery and mqtt nodes? I am 100% noob on Node Red, so i am sorry if it is a stupid question :slight_smile:

Sure. Try it and see :smiley:

Hi there everyone, I’ve created a few sensors in HA from some Power Monitors attached to a washing machine and dryer.

It’s working great however I want to scale it by 0.1 so I can get a valid wattage read.

Is there a simple way to do this?

        friendly_name: Washing Machine Power
        unit_of_measurement: 'W'
        value_template: '{{ states.switch.washing_machine_power_meter.attributes.current }}'
      dryerpower:
        friendly_name: Clothes Dryer Power
        unit_of_measurement: 'W'
        value_template: '{{ states.switch.gpo_power_meter.attributes.current }}'

I’m not sure what you mean by this.
Do you want the value to 1 decimal place

value_template: '{{ states.switch.gpo_power_meter.attributes.current | round(1) }}'

Or do you want 10% of the value

value_template: '{{ states.switch.gpo_power_meter.attributes.current * 0.1 }}'

I meant 10% of the value, thank you so much. :slight_smile:

1 Like

I’m trying to round my climate temp to 0.5. So 22.3 becomes 22.5, etc
I thought this would work but this gives me back the same original temp.
Would anyone be able to spot the mistake?
I double the original and round it by (0), (no decimals), then I divide by 2 and round it to one decimal which will always be .0 or .5

- platform: template
  sensors:
    livingroom_current_temperature_rd:
      friendly_name: "Current Temperature Rounded"
      unit_of_measurement: °C
      value_template: >
        {% set rd = state_attr('climate.living_room', 'current_temperature') | round (1) %}
        {% set dble = rd * 2 | round(0) %}
        {{ dble / 2 | round(1) }}
1 Like

Figured it out

- platform: template
  sensors:
    livingroom_current_temperature_rd:
      friendly_name: "Current Temperature"
      unit_of_measurement: °C
      value_template: >
        {{ (((state_attr('climate.living_room', 'current_temperature') | float) * 2) | round ) / 2 }}

Now that I have my temperature numbers rounded, how could I make 22.0 shown as 22?
So all .0 numbers loose the decimal point but not the .5 numbers.

22.0 → 22
22.5 → 22.5
23.0 → 23
Etc

You can use the modulo operation (%) for that.

{% set foo = 22.5 %}

{{ (foo | round(1)) if (foo % 1) else foo }}

This gave me a null result

        temperature: |
          {% set temp = states('sensor.livingroom_current_temperature_rd') %}
          <span class="sidetemp">
            {{ (temp | rount(1)) if (temp % 1) else temp }}
          </span>

Try ‘round’

That didn’t work either.

{{ (temp | round(1)) if (temp % 1) else temp }}

Tried this too:

{% if (temp % 1) %}
          {{temp | round(1)}}
          {% else %}
          {{temp}}

Gives me back true instead of null. haha

this helped me too, thx ya!

You just rounded 3.6 not the calculation, use brackets.

Here vis a quick way to get hh:mm:ss from a count of seconds by (ab)using the time primitives

The example is UPS runtime remaining.

# UPS runtime in hh:mm:ss
# converts run time to seconds since unix epoc 1970-01-01T00:00Z 
# it uses this hack to get the locale time offset
#  0 | timestamp_custom('%s') | float 
- platform: template
  sensors:
    server_rack_ups_runtime_mins:
      friendly_name: "Server Rack UPS Runtime"
      value_template: >-
        {{ (states.sensor.server_rack_battery_runtime.state | float - (0 | timestamp_custom('%s') | float))    | timestamp_custom('%X') }}

Instead of {{ (float(states.sensor.1.state) + float(states.sensor.2.state) + float(states.sensor.3.state)) / 3 | round(2) }}

use {{ (float ((states.sensor.1.state + states.sensor.2.state + states.sensor.3.state) / 3) | round(2) }}