MQTT, probaly asked 1000 times

Hello all, iám rather new on HA and having some questions about MQTT sensor readings / formatting
In the template editor it works ok

{{ states('sensor.DB199_OB098') | round(0) }} 
{{  (states('sensor.DB199_OB097')|float * 1 / 1000) | round(3)}}

But when i put this in my configuration.yaml .is not diplaying getting any data, also no error message.

  - platform: mqtt
    state_topic: 'DB199-OB098'
    name: "db199-ob098"
     value_template: '{{ states(sensor.db199_ob098) | round(0) }}'
    unit_of_measurement: 'Watt'
    
  - platform: mqtt
    state_topic: 'DB199-OB097'
    name: "db199-ob097"
    value_template: '{{ ( states(sensor.db199_ob097)|float * 1 / 1000) | round(3)}}'
    unit_of_measurement: 'kWh'

If i remove the value_template the unformatted values are dislayed like +0.3062391E+03 Watt

Your value templates should be:


value_template: '{{ value|float|round(0) }}'

And

value_template: '{{ value|float|round(3) }}'

This takes the received string, converts it to a number then rounds it.

In future please format your code correctly. See point 11 here: How to help us help you - or How to ask a good question

2 Likes

Ahh, it works. thx

In configuration.yaml
for the momentum power gives 123.4 Watt
value_template: '{{ (value|float) | round(1) }}

for the counter gives the value 1234.123 kWh
value_template: '{{ (value| float) / 1000 }}

db199-098

1 Like