thomasloven / hass-lovelace_gen

🔹 Improve the lovelace yaml parser for Home Assistant
MIT License
218 stars 22 forks source link

Using a variable in a raw section #55

Open rjgrandy opened 10 months ago

rjgrandy commented 10 months ago

I want to populate a template card (mushroom template) using lovelace_gen. So I need to escape the parts that I want jinja to pass through with {% raw %}, however I want to use a variable from lovelace_gen (in the below case {{entity}}. Is below an acceptable syntax? I doesn't seem to like it.

{% raw %}
icon: >-
  {% if is_state({% endraw %}{{entity}}{% raw %},'on') %}
    mdi:lightbulb-on
  {% raw %}{% elif is_state({% endraw %}{{entity}}{% raw %},'off') %}
    mdi:lightbulb-off
  {% else %}
    mdi:lightbulb-alert
  {% endif%}
{% endraw %}
steefzz commented 10 months ago

Hi there,

Not sure if this is what you mean but i think i'm kinda doing the same

This is my room_card.yaml function

# lovelace_gen
type: custom:mushroom-template-card
entity: {{ room_state_group }}
primary: {{ room|capitalize }}
secondary: |-
  {%raw%}
  {% if is_state('{%endraw%}{{ room_state_group }}{%raw%}', 'on') %}
    Aan
  {% else -%}
    Uit
  {%- endif -%}
  {%endraw%}
icon: {{ icon }}
icon_color: |-
  {%raw%}
  {% if is_state('{%endraw%}{{ room_state_group }}{%raw%}', 'on') %}
    yellow
  {%- endif -%}
  {%endraw%}
tap_action:
  action: navigate
  navigation_path: {{ room }}
layout: horizontal
hold_action:
  action: toggle
double_tap_action:
  action: none
fill_container: true

In my view i'm using this to "call the function":

  - type: horizontal-stack
    cards:
      - !include 
        - ../../functions/room_card.yaml
        - room: achtertuin
          room_state_group: light.achtertuin_light_group
          icon: mdi:palm-tree
      - !include 
        - ../../functions/room_card.yaml
        - room: schuur
          room_state_group: light.schuur_licht
          icon: mdi:barn

Which is resulting in (the bottom two buttons): image

For the entity i'm using a different name than "Entity". Don't know or it might be an issue but in my case I often use groups to show the state.

Hope this helps you?