Templating With Entity Attributes

I’m trying to make a row of buttons for ceiling fan control. I just want “off, low, medium, medium-high, high” buttons in a row, and one button at a time lights up to reflect current speed. I think I’ll be able to make the buttons reflect the state, but I’m having issues getting the template to work.
As of right now, with the following code, “off” works correctly, but if any other fan speed is selected, the sensor reports “low”. I must be missing something simple.

      living_fan_off:
        friendly_name: Living Room Fan Status
        value_template: >-
          {% if is_state('fan.living_room_ceiling_fan', 'off') %}
            off
          {% elif state_attr('fan.living_room_ceiling_fan', 'speed'), 'low' %}
            low
          {% elif state_attr('fan.living_room_ceiling_fan', 'speed'), 'medium' %}
            medium
          {% elif state_attr('fan.living_room_ceiling_fan', 'speed'), 'medium-high' %}
            medium-high
          {% elif state_attr('fan.living_room_ceiling_fan', 'speed'), 'high' %}
            high
          {% else %}
            failed
          {% endif %}

Maybe there’s an easier way to do this that I’m missing, but any help would be appreciated

          {% if is_state('fan.living_room_ceiling_fan', 'off') %}
            off
          {% elif state_attr('fan.living_room_ceiling_fan', 'speed', 'low') %}
            low
          {% elif state_attr('fan.living_room_ceiling_fan', 'speed', 'medium') %}
            medium
          {% elif state_attr('fan.living_room_ceiling_fan', 'speed', 'medium-high') %}
            medium-high
          {% elif state_attr('fan.living_room_ceiling_fan', 'speed', 'high') %}
            high
          {% else %}
            failed
          {% endif %}

Syntax was off. Try this.

Thanks for the suggestion. I just tried that and it made it only report “off”. When it do it with the close parenthesis after ‘speed’ it will report “off” when its off or “low” when its any other speed.

Basically I just have a horiz-stack of 5 buttons to set the fan speed and I want the states to be reflected. I have each card set up as below, and Off and Low are working fine. Is there a way I can just pull the state_attribute from the fan entity itself and use that in the state section of the card?

  - entity: sensor.living_fan_speed
    hold_action:
      action: more-info
    icon: 'mdi:speedometer-slow'
    name: Low
    show_icon: true
    show_name: true
    state:
      - color: 'rgb(99,137,248)'
        value: low
    style: |
      ha-card {
        border-radius: 15px;
      }
    styles:
      card:
        - height: 90px
    tap_action:
      action: call-service
      service: fan.set_speed
      service_data:
        entity_id: fan.living_room_ceiling_fan
        speed: low
    type: 'custom:button-card'

what does

“{{ state_attr(‘fan.living_room_ceiling_fan’, ‘speed’) }}”

report in states tab?

you can also use the fan entity row custom component: https://github.com/finity69x2/fan-control-entity-row

have you also looked at https://www.home-assistant.io/integrations/fan.template/ ?

My fan entity now shows all of this:

I saw the fan entity row as well, but I wanted my button design to match with the rest of the UI

That Fan Template is really cool, and I’m gonna save that form my remote controlled bedroom fan for sure, so thank you for that.

oh I just realized you were trying to set it up for ‘off’ right? if not

its either off,. or the next condition so:

    living_fan_off:
      friendly_name: Living Room Fan Status
      value_template: >-
        {% if is_state('fan.living', 'off') %}
          off
        {% else %}
         {{ state_attr('fan.living', 'speed') }}
        {% endif %}

maybe this is what you want?

cards:
  - entity: sensor.livingroom_fanspeed
    hold_action:
      action: more-info
    icon: 'mdi:speedometer-slow'
    name: Low
    show_icon: true
    show_name: true
    state:
      - color: 'rgb(99,137,248)'
        value: low
    styles:
      card:
        - height: 90px
    tap_action:
      action: call-service
      service: fan.set_speed
      service_data:
        entity_id: fan.living
        speed: low
    type: 'custom:button-card'
  - type: horizontal-stack
    cards:
      - entity: sensor.livingroom_fanspeed
        hold_action:
          action: more-info
        icon: 'mdi:speedometer-slow'
        name: Med
        show_icon: true
        show_name: true
        state:
          - color: 'rgb(99,137,248)'
            value: medium
        style: |
          ha-card {
            border-radius: 15px;
          }
        styles:
          card:
            - height: 90px
        tap_action:
          action: call-service
          service: fan.set_speed
          service_data:
            entity_id: fan.living
            speed: medium
        type: 'custom:button-card'
  - type: horizontal-stack
    cards:
      - entity: sensor.livingroom_fanspeed
        hold_action:
          action: more-info
        icon: 'mdi:speedometer-slow'
        name: High
        show_icon: true
        show_name: true
        state:
          - color: 'rgb(99,137,248)'
            value: high
        style: |
          ha-card {
            border-radius: 15px;
          }
        styles:
          card:
            - height: 90px
        tap_action:
          action: call-service
          service: fan.set_speed
          service_data:
            entity_id: fan.living
            speed: high
        type: 'custom:button-card'
  - type: horizontal-stack
type: horizontal-stack

sensor.yaml

    livingroom_fanspeed:
      value_template: "{{ state_attr('fan.living', 'speed') }}"

maybethis

Oh my god you nailed it. That’s 100% what I was trying to do. I just made it much more difficult for myself. Thank you so much, it all works great now

I was trying to work from this and it no matter what I did I just couldn’t get the right outputs

sensor:
  - platform: template
    sensors:
      kettle:
        friendly_name: "Kettle"
        value_template: >-
          {% if is_state('switch.kettle', 'off') %}
            off
          {% elif state_attr('switch.kettle', 'kwh')|float < 1000 %}
            standby
          {% elif is_state('switch.kettle', 'on') %}
            on
          {% else %}
            failed
          {% endif %}
1 Like

Great. glad its working for you.

Ive been playing with the fan things for about a week or so now so I had alot of random coding happening, lol.

I’m just glad you realized what I was trying to do. I definitely wasn’t explaining it correctly at all, or going at it the right way
image

1 Like