New To Home Assistant - Need help with Sensor template

Hello Everyone,

I managed to set up some basic stuff see screenshot.( https://i.imgur.com/8dlMNRG.png ) but at first i am having issues with a smart plug with power meter.
When the plug is in ON state, the power meter works fine. But when it’s off , it’s stuck with the last info.
It’s the Boiler 1 and Boiler 2

At the moment the template is :

  - sensor:
      - name: "Boiler 1 Power Usage"
        unit_of_measurement: "W"
        state_class: 'measurement'
        device_class: 'power'
        state: "{{ states('sensor.boiler_1_power') | int / 10 | round(1) }}"

But i would like to achieve something like . If state OFF then to return value 0 . If on then the value of :
{{ states(‘sensor.boiler_1_power’) | int / 10 | round(1) }}

Is it possible ?

Thanks,

This one should work:

  - sensor:
      - name: "Boiler 1 Power Usage"
        unit_of_measurement: "W"
        state_class: 'measurement'
        device_class: 'power'
        state: "{{ 0 if states('sensor.boiler_1_power') == 'off' else states('sensor.boiler_1_power') | int / 10 | round(1) }}"
1 Like

This should work. If the state is text (off, unknown, unavailable etc) it will set the default to 0 and compute.

template:
   - sensor:
        - name: "Boiler 1 Power Usage"
          unit_of_measurement: "W"
          state_class: 'measurement'
          device_class: 'power'
          state: {{ (states('sensor.boiler_1_power') | float(0) / 10) | round(1) }}
2 Likes

Thank you Guys,

It solved the problem.

have a nice day.