Fan Template - value_template confusion

I realized there was a fan template that I hadn’t been using. Instead I had my fans setup as switches. So I took this:

  switch:
  - platform: template
    switches:  
      living_room_fan:
        friendly_name: "Living Room - Fan"
        value_template: "{{ is_state('sensor.living_room_fan_status', 'True') }}"
        turn_on:
          - service: rest_command.engen_api
            data:
              node: 17
              value: 1
              path: '1/switch_binary/value'
          - delay: 0.5
          - service: homeassistant.update_entity
            entity_id: sensor.living_room_fan_status
        turn_off:
          - service: rest_command.engen_api
            data:
              node: 17
              value: 0
              path: '1/switch_binary/value'
          - delay: 0.5
          - service: homeassistant.update_entity
            entity_id: sensor.living_room_fan_status

And turned it into this:

fan:
  - platform: template
    fans:
      living_room_fan:
        friendly_name: "Living Room - Fan"
        value_template: "{{ is_state('sensor.living_room_fan_status', 'True') }}"
        turn_on:
          - service: rest_command.engen_api
            data:
              node: 17
              value: 1
              path: '1/switch_binary/value'
          - delay: 0.5
          - service: homeassistant.update_entity
            entity_id: sensor.living_room_fan_status
        turn_off:
          - service: rest_command.engen_api
            data:
              node: 17
              value: 0
              path: '1/switch_binary/value'
          - delay: 0.5
          - service: homeassistant.update_entity
            entity_id: sensor.living_room_fan_status

The problem that I’m having is that the value_template isn’t updating appropriately. If I change it to this:
value_template: "{{ states('switch.living_room_fan') }}"

It works, but I don’t really want this to remain as a switch. This works for my lights, so I’m not sure what the fan element is expecting. These are all simple fan controls. All they do is turn on and off. I just wanted it to be a ‘fan’ element for the GUIs. Thanks!

You are creating a Template Fan called fan.living_room_fan but its value_template refers to sensor.bedroom_fan_status. Shouldn’t that be sensor.living_room_fan_status?

Yes, sorry, I pasted the wrong block in the post. I’ll edited the post with the right section from the config file.

You’re using sensor.living_room_fan_status as a status indicator. Given that this fan can only be turned on and off, why didn’t you choose to create a binary_sensor? Then the value_template would be:

        value_template: "{{ is_state('binary_sensor.living_room_fan_status', 'on') }}"

Which leads to another question, what is sensor.living_room_fan_status actually monitoring? How does it know when the fan is on or off?

This:

            service: homeassistant.update_entity
            entity_id: sensor.living_room_fan_status

forces the sensor to update itself … but what is it actually checking?

I guess I did it as a sensor item because that’s what I had for everything else. What’s the difference between:

        value_template: "{{ is_state('sensor.living_room_fan_status', 'True') }}"

and

        value_template: "{{ is_state('binary_sensor.living_room_fan_status', 'on') }}"

Shouldn’t both of those return a true statement? What first one is what I had when I built this as a switch, so I expected the same template to work as a fan. Thanks!

  - name: living_room_fan_status
    platform: rest
    resource: http://192.168.2.12:52125/api/v1/nodes/0:17
    method: GET
    value_template: '{{ value_json.result["1"].switch_binary.value }}'

An example:

{
  "params": {}, 
  "response": "0.00396", 
  "result": {
    "1": {
      "basic": {}, 
      "multi_channel_capability": {
        "cmdclasses": [
          "basic", 
          "switch_binary"
        ], 
        "controlclasses": [], 
        "dynamic": false, 
        "endpoint": 1, 
        "generic_type": "switch_binary", 
        "specific_type": "power_switch_binary"
      }, 
      "switch_binary": {
        "alias": "Fan", 
        "value": false
      }
    }, 
    "2": {
      "basic": {}, 
      "multi_channel_capability": {
        "cmdclasses": [
          "basic", 
          "switch_binary"
        ], 
        "controlclasses": [], 
        "dynamic": false, 
        "endpoint": 2, 
        "generic_type": "switch_binary", 
        "specific_type": "power_switch_binary"
      }, 
      "switch_binary": {
        "alias": "Light", 
        "value": false
      }
    }, 

Whereas a binary_sensor only has two valid states, on an off, a sensor can have all kind of states. It all depends on how you configured it and, especially, what it is monitoring.

In this case, I have no idea what sensor.living_room_fan_status is monitoring. The fan? How?

OK, while I was busy typing, you answered the question. :slight_smile:

Does the fan template require a literal ‘on’ or ‘off’? And I’m supplying true/ false? I guess I thought the value_template section with is_state() actually returned a boolean true/ false with the comparison.

If that’s the case, I guess I can change my sensor to binary_sensor and that should be an easy fix.

Yes. A Template Fan expects its value_template to return on or off.

It does but, in this case, the fan’s value_template want to see on/off not true/false. :man_shrugging:

After switching from sensor to binary_sensor, change the fan’s value_template to:

        value_template: "{{ states('binary_sensor.living_room_fan_status') }}"

Thanks for the help!

I ended up doing this (just to close out the thread):

        value_template: "{{ states('binary_sensor.living_room_fan_status') }}"

I assume you means states, not state. Let me know if that’s wrong. Thanks!

Yep, just a tiny typo. I’ve corrected it.

I’d appreciate it if you could tag my post as ‘Solution’ in order to make it easier for other users (who may be experiencing the same problem to find it. Only you, the author of this topic, can tag a post as ‘Solution’. It will automatically place a link to the solution under your first post.