Nope. States are strings. The + will concatenate the strings.
It is correct to cast each value. The problem is that it’s the integer 3 that gets rounded and not the whole result, due to the order of evaluation. The solution is to add brackets.
Did you find a solution? I am looking for a generic approach to get temp from sensors in xx.yy format but xx.y is sufficient. And I also don’t want to create another template sensor…
I’ll start by saying I’m new to the template topic, and have been playing with Home Assistant for a few months now with good success otherwise.
Can someone please help with this MQTT.yaml entry?
The default output is showing up as having 6 decimal places (i.e. 2.500000 C) and I’m trying to round it off to 1 decimal place, (i.e. 2.5 C)
The below code is showing me 0 C (when the temperature is really 2.5 C
Thanks in advance,
# MQTT Sensors
sensor:
- name: "Backyard Temperature (433Mhz Probe)"
state_topic: "rtl_433/13184/temperature_C"
qos: 0
state_class: measurement
device_class: temperature
unit_of_measurement: "°C"
value_template: '{% set state = states("sensor.backyard_temperature_433mhz_probe") %}{{ state | round(1) if is_number(state)}}'
States are strings — even when they look like numbers (attributes retain their types). You need to make it a number first and you must specify a default in case it’s unavailable. I’m pretty sure your logs would’ve given some kind of warning or error.
This is an MQTT sensor, not a template sensor. You can’t look up its own state. The value_template allows you to convert whatever is in the MQTT topic to the format of your choosing.
I assume the topic contains e.g. 2.500000 — that is the value, which you then need to turn into a float and round it.
If that still doesn’t work, remove the value_template line and post a screenshot of the sensor state so we can see what’s actually coming from the sensor via the topic.
Hey folks, I also have an issue with rounding my aqara sensor values. In deconz i receive zero decimals but in home assistant i suddenly receive two decimals (this started a while ago - before ther was only one decimal.)
it´s not an issue of the card (it´s a button-card), I think.
Even in the states-list the sensor shows two decimals, even though the appearence-setting says one.