Need some help building a template switch for a ceiling fan please

I have a ceiling fan that I’ve managed to control by learning the 433 codes into a Broadlink RM Pro so now I can turn it on and off which is great but of course, there is no feedback.

What I’m intending to do is supply power to the fan through a Shelly PM1 so I can monitor the power used and determine whether the fan is on or off. I can then build a template switch but I need help with the templating script for the sensor. The intention is that if the power is <1 it’s assumed off and >1 it’s assumed on.

switch:
  - platform: template
    switches:
      ceiling_fan_2:
        value_template: "{{what to put in here?}}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.master_bedroom_fan_2
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.master_bedroom_fan_2

The sensor I currently have for one of my Shelly’s that reports power is this:

sensor:
  - platform: mqtt
    name: "Family Room Shelly Power"
    state_topic: "shellies/shellyswitch-9F6008/relay/power"
1 Like
switch:
  - platform: template
    switches:
      ceiling_fan_2:
        value_template: "{{ states('sensor.family_room_shelly_power') | float > 666 }}" 
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.master_bedroom_fan_2
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.master_bedroom_fan_2

Change the 666 value to a value smaller than when the fan is on. e.g if the fan draws 100W when on a good value might be 75. The closer to the ‘on’ power the faster the response but also the greater the likelihood of a false reading.

Thank you so much! You template ninjas never fail to amaze me. :grin: I’ll try that in the morning and see how I get on.

Worked an absolute treat. Thank you! Added this to the bottom.

        icon_template: >
            {%- if is_state('switch.family_room_fan', 'on') -%}mdi:fan{% else %}mdi:fan-off{%- endif -%}
1 Like

Also, since it’s a fan, you might want to try a template fan instead?

1 Like

I don’t actually have any fans so did not realise this was an option. Learn something new every day.

Now you’ve created a problem! :grin: Like @tom_l, I did not know you could do that either. Let the fun begin. I found a thread here so I’ll ask @subzero79 for some feedback.

I mean, it’s not really that big of a difference. But, if your fan has different speeds and you’re able to detect those with power consumption, you’ll be able to know if your fan is on high, medium, low, or off. Which is nice.

The UI for a fan looks almost the same as a switch. In the main list, it’s just a toggle like a switch. When you go to the more-info panel, you’ll also get a dropdown to set the speed.

The other advantage to using a “fan” instead of a “switch” is Google/Alexa integration will know automatically that it’s a fan as opposed to you having to override the switch to a fan in the Google/Alexa config.

So… if you don’t use Google/Alexa or don’t have multiple speeds on your fan, there’s basically no payoff to switch it to a fan.template.

There is an example on the docs

I posted the set speed script sort of example on the other thread.
As for the current state I use nodered to process the values and push them to the input_number, but you can do the same I guess with a template.

Thanks, appreciate that. Found a detailed post which is quite relevant to my config here.

Sadly, I’ve just discovered that the code below does not work when you define the template as a fan but does when you define it as a switch.

value_template: "{{ states('sensor.bedroom_fan_shelly_power') | float > 3 }}"

The value is reported OK but fan switch does not change state.

Now looking to define something like {% if is_state to automatically turn an input boolean or a sensor on and off but can’t quite figure the syntax.

I’ve got this so far which reports OK in the template editor but comes up with a syntax error when I try and create a template sensor. Any clues?

This works:

'{% if states('sensor.bedroom_fan_shelly_power') | float > 3 %}On{% else %}Off{% endif %}'

This does not. Comes up with “can not read a block mapping entry; a multiline key may not be an implicit key” and searching the forum for that exact phrase yields not results.

sensor:
  - platform: template
    sensors:
      master_fan_status:
        value_template: '{% if states('sensor.bedroom_fan_shelly_power') | float > 3 %}On{% else %}Off{% endif %}'
        friendly_name: 'Master Fan State'

Sorted!

sensor:
  - platform: template
    sensors:
      master_fan_status:
        friendly_name: "Master Fan Status"
        value_template: "{% if states('sensor.bedroom_fan_shelly_power') | float > 3 %}On{% else %}Off{% endif %}"

Seems the syntax has changed some? Odd though as I have another sensor with the ‘old’ way and it works fine and does not report any errors. Go figure.

  - platform: template
    sensors:
      washing_machine_status:
        value_template: '{% if is_state("input_boolean.washing_active", "off") %}Stopped{% else %}Washing{% endif %}'
        friendly_name: 'Washing Machine Status'
1 Like

Buggar, still not working as intended. :roll_eyes:

Fan turns on and power use increases turning on my template sensor but the template fan does not update its state. Anyone know why?

value_template: "{{ states('sensor.master_fan_status') }}"

com-video-to-gif

Cracked it for those looking to use a Shelly1 PM for feedback in regards to on/off status on any device.

My problem was that the value_template for the fan requires either on or off, not true or false or any other value. The following code works a treat.

value_template: "{% if states('sensor.bedroom_fan_shelly_power') | float > 3 %}on{% else %}off{% endif %}"
1 Like