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.

39%20pm

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

24%20pm

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’.

06%20pm


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.

02%20pm

When off, I see just the friendly name.

03%20pm

Thanks!

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 %}
  0
{% endif %}

Not tested, but should work.

Worked perfectly, and makes total sense - thank you!

29%20pm

:grinning: