Template Switch - Status from Sensor

Hi, I am trying to set up a switch that only shows that it is on when there is current draw. I am trying to use a template, but the switch is showing as being off even when there is current draw. Here is my code:

switch:
  - platform: template
    switches:
      attic_fan_sense:
        friendly_name: "Attic Fan"
        value_template: "{{ is_state('sensor.attic_fan_current', '! 0') }}"
        icon_template: mdi:fan
        turn_on:
          service: switch.turn_on
          target:
            entity_id: switch.attic_fan
        turn_off:
          service: switch.turn_off
          target:
            entity_id: switch.attic_fan

Try this:

value_template: "{{ states('sensor.attic_fan_current')|int(0) != 0 }}"

The way you have it the template is looking for the literal string β€œ! 0”. You can’t do operations inside the state string.

The way I have it above will register as off from +0.9 to -0.9. If that value range is too large you can do this:

value_template: "{{ states('sensor.attic_fan_current')|float(0) != 0 }}"

This will only show as off when the current is actually 0.

One final method for exactly zero:

value_template: "{{ not is_state('sensor.attic_fan_current', '0.00') }}"

Note however that you must match exactly the number of decimals shown for the sensor in Developer Tools β†’ States when the sensor is zero. All states are strings and the string β€˜0.00’ does not equal the string β€˜0’.

1 Like

Worked perfectly. Ty!

1 Like