Trying my first template - read all the docs and videos. Need help with a specific template!

I have a fan that is controlled by the Bond Bridge integration. This ONLY gives me the ability to toggle the fan by sending the “on” command. I have also recently plugged it into an energy monitoring plug and have determined that for the most part anything above 2w is “on”.

First I tried creating a “status” helper that would be “on” if the current was above 2 and “off” it is was below. Then I created a toggle helper switch that would take an “off” command and then compare it to the status helper. If it was already off, then it wouldn’t do anything, but if it was on, then the toggle command would be sent. (and vice versa, of course). It actually worked. Problem is, this toggle helper switch doesn’t show up as a switch that I control through Alexa. SO - I have been trying to figure out templates, thinking this was a first good scenario. I’ve read all the docs, and I just can’t seem to figure out how to do it.

switch:
  - platform: template
    switches:
      bedside_fan:
        friendly_name: "Bedside Fan Switch"
        unique_id: "bedside_fan_switch"
        value_template: "{{ is_state('input_boolean.bedside_fan_status', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: fan.bedside_fan
        turn_off:
          service: switch.turn_on
          data:
            entity_id: fan.bedside_fan'

Here is the automation that currently successfully creates the helper toggle that knows whether the fan is on or off:

'alias: Bedside Fan Status Control
description: Controls the Bedside Fan helper status based on energy consumption
trigger:
  - platform: state
    entity_id:
      - sensor.bedside_fan_energy_monitor_current_consumption
condition: []
action:
  - if:
      - type: is_power
        condition: device
        device_id: 86b0a18c58a881fd13633541fa4d1883
        entity_id: sensor.bedside_fan_energy_monitor_current_consumption
        domain: sensor
        above: 2
    then:
      - service: input_boolean.turn_on
        data: {}
        target:
          entity_id: input_boolean.bedside_fan_status
  - if:
      - type: is_power
        condition: device
        device_id: 86b0a18c58a881fd13633541fa4d1883
        entity_id: sensor.bedside_fan_energy_monitor_current_consumption
        domain: sensor
        below: 2
    then:
      - service: input_boolean.turn_off
        data: {}
        target:
          entity_id: input_boolean.bedside_fan_status
mode: single

The fan.bedside_fan only has an “on” command and that toggles the fan. Ultimately I want this switch template to function the way my toggle helper did. Sync with the status helper that is always monitoring the voltage, so that when a command is sent, it will react according to the status (as opposed to, say, thinking it’s turning off the fan when it’s already off, thus turning it on).

I know this is probably a “roll your eyes” kind of question. it’s always my first attempt that I feel stupid, but I think once I wrap my head around this one I’ll be able to continue to learn more. Could someone help me build this first template? I would be EVER so grateful! Ultimately, I want to have created something that I can control from Alexa (some kind of switch that Alexa will recognize as an actual switch, which the toggle helpers do not seem to!?)

Thank you!

So, I think you need to do a couple of things.

  1. Create a template binary_sensor that determines if it is currently on or of The template would be something like this. You can test it in developer tools / templates. With this you can get rid of the input_boolean. Name it bedside_fan_running
{{ states(‘sensor.bedside_fan_energy_monitor_current_consumption’)|int(0) < 2 }}

Use this binary_sensor as the value_template in the fan.
{{ states(‘binary_sensor.bedside_fan_running’) }}

  1. Create a script that toggles the fan. You can reuse sections of the automation you have and use the binary sensor created in step 1. For the turn on and turn off commands call the script.
  1. You cannot use a switch.turn_on service on a fan entity.

  2. You could have used a template binary sensor as the basis for your template switch’s value_template, but using a boolean helper is fine. However, triggering off an open state is overkill when you are only interested in the transition around a single number

alias: Bedside Fan Status Control
description: Controls the Bedside Fan helper status based on energy consumption
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.bedside_fan_energy_monitor_current_consumption
    above: 2
    id: 'on'
  - platform: numeric_state
    entity_id:
      - sensor.bedside_fan_energy_monitor_current_consumption
    below: 2
    id: 'off'
condition: []
action:
  - service: input_boolean.turn_{{ trigger.id }}
    data: {}
    target:
      entity_id: input_boolean.bedside_fan_status
mode: single

Using this approach also opens up the possibility to use a for variable in your triggers so that you avoid “bouncing” the boolean back-and-forth if the value hovers around 2.

You can use fan.turn_on

Oops. THAT’s just embarassing! I keep forgetting that switch/fan/light entities are not really interchangeable. I’ll start with that!