Creating a Template for a Mains Plug where 'state_attr' for 'current_power_w' can be 'None'

Hiya. I’m hoping someone could please point me in the right direction.
… Sorry if its as easy as I’m hoping it is! :rofl::crossed_fingers:t2:

I user a template for a mains plug that supports power monitoring.
The ‘state_attr’ for ‘current_power_w’ gives me the power usage when above 0 W.


However, when the switch is off, instead of showing 0, it displays ‘None’.


I felt that this was a great opportunity to use a template.

I had a go, but sadly it still shows ‘None’ at times when I was really hoping for ‘0 W’.


If anyone could point me towards whatever I’m missing, I’d really appreciate it please. :pray:t2:
Hope that’s clear - thanks in advance!

Please show the attributes of switch.charging_station when it is off.

Hiya, thanks for your help.

When on, I see the friendly name, and the power usage.


When off, I see just the friendly name.



So you need to test if the attribute is available.

{% if states.switch.charging_station.attributes.current_power_w %}
  {{ state_attr('switch.charging_station'. 'current_power_w') }}
{% else %}
{% endif %}

Not tested, but should work.

Worked perfectly, and makes total sense - thank you!