Sensor template from current consumption

I’m trying to create a sensor template from an attribute on a TP-Link HS-110 and I’m stuck.

My goal is to create a sensor that is either “on” if the current consumption is above 1 watt or “off” if it’s below 1 watt.

I’ve found this code that removes the “W” from the current consumption attribute, but struggling to make the rest.
Any help would be much appreciated :slight_smile:

value_template: '{{ states.switch.tplink1.attributes["Current consumption"] | replace(" W", "") | float }}'

Hi @ladefoged81, the multiline example in the Template Sensor docs should give you a hint.
Someting like (not tested):

value_template: >-
            {%- if states.switch.tplink1.attributes["Current consumption"] | replace(" W", "") | float > 1 %}
                on
            {% else %}
                off
            {%- endif %}

You can play with it in Developer Tools/Templates.

1 Like

I’ve tried something similar without luck, but this works like a charm. Thanks.