Add function in template

Hello,

is there any way to add a function in a template which can be reused in this one automation template?

Background:
I want to design a weather forecast automation template for OpenEPaperLink. For this, I need to replace the weather status with the appropriate icon - but I do not want to repeat the “if-else” everywhere.

Any suggestion?

Thanks for help :slight_smile:

Hi, yes, see the reusing template section of this page

1 Like

Hi @rekenaar ,
thanks a lot for the very quick reply!
My hope was that I can inline the function in the automation. Any chances for this?

Beside using custom macros like @rekenaar suggested, the only other thing I can think of off-hand would be to use a repeat, but it would make it easier for us to offer realistic options if you share the automation’s configuration.

1 Like

If it is a automation, you can create a variable in the actions, and then re-use it.

I agree with @Didgeridrew that more information would make it easier to assist.

1 Like

So this is an example where I now reuse the logic to get the correct icon to display regarding to the weather condition. Since I have counted 15 different conditions, this will grow by a lot. Any chance to make this easier?

Thanks again for your help :slight_smile:

alias: EPaper Tag - Current Weather Data 2
description: ""
triggers:
  - minutes: /20
    trigger: time_pattern
actions:
  - data:
      dry-run: true
      background: white
      rotate: 0

      payload:
        - type: icon
          value: |
            {% if states.weather.checkpoint.state == 'clear-night' %}
              mdi:weather-night
            {% elif states.weather.checkpoint.state == 'sunny' %}
              mdi:weather-sunny
            {% else %}
              mdi:emoticon-happy
            {% endif %}
          x: 10
          "y": 5
          size: 35
          color: black
        - type: icon
          value: |
            {% if state_attr('sensor.weather_forecast_daily_2', 'forecast')[1].condition == 'clear-night' %}
              mdi:weather-night
            {% elif state_attr('sensor.weather_forecast_daily_2', 'forecast')[1].condition == 'sunny' %}
              mdi:weather-sunny
            {% else %}
              mdi:emoticon-happy
            {% endif %}
          x: 50
          "y": 5
          size: 35
          color: black
        - type: icon
          value: |
            {% if state_attr('sensor.weather_forecast_daily_2', 'forecast')[2].condition == 'clear-night' %}
              mdi:weather-night
            {% elif state_attr('sensor.weather_forecast_daily_2', 'forecast')[2].condition == 'sunny' %}
              mdi:weather-sunny
            {% else %}
              mdi:emoticon-happy
            {% endif %}
          x: 90
          "y": 5
          size: 35
          color: black
    action: open_epaper_link.drawcustom
    target:
      device_id: da5a69b43d351ec181abb2e3fb14d630
mode: restart

You can set up a mapping as a script variable, then use the get method to find the corresponding icon ID string for the condition you are returning from the state objects of your weather and sensor entities.

alias: EPaper Tag - Current Weather Data 2
description: ""
triggers:
  - minutes: /20
    trigger: time_pattern
variables:
  icons:
    clear-night: 'mdi:weather-night'
    sunny: 'mdi:weather-sunny'
actions:
  - data:
      dry-run: true
      background: white
      rotate: 0
      payload:
        - type: icon
          value: |
            {% set cond = states('weather.checkpoint') %}
            {{ icons.get(cond, 'mdi:emoticon-happy') }}
          x: 10
          "y": 5
          size: 35
          color: black
        - type: icon
          value: |
            {% set cond = state_attr('sensor.weather_forecast_daily_2', 'forecast')[1].condition %}
            {{ icons.get(cond, 'mdi:emoticon-happy') }}
          x: 50
          "y": 5
          size: 35
          color: black
        - type: icon
          value: |
            {% set cond = state_attr('sensor.weather_forecast_daily_2', 'forecast')[2].condition %}
            {{ icons.get(cond, 'mdi:emoticon-happy') }}
          x: 90
          "y": 5
          size: 35
          color: black
    action: open_epaper_link.drawcustom
    target:
      device_id: da5a69b43d351ec181abb2e3fb14d630
mode: restart

@Didgeridrew Thanks a lot! The Home Assistant community is awesome :tada:

I did also add a fallback so I have a single responsibility for when there is an unknown state.

variables:
  icons:
    clear-night: mdi:weather-night
    sunny: mdi:weather-sunny
    cloudy: mdi:weather-cloudy
    exceptional: mdi:alert
    fog: mdi:weather-fog
    hail: mdi:weather-hail
    lightning: mdi:weather-lightning
    lightning-rainy: mdi:weather-lightning-rainy
    partlycloudy: mdi:weather-partly-cloudy
    pouring: mdi:weather-pouring
    rainy: mdi:weather-rainy
    snowy: mdi:weather-snowy
    snowy-rainy: mdi:weather-snowy-rainy
    windy: mdi:weather-windy
    windy-variant: mdi:weather-windy-variant
    fallback: mdi:emoticon-happy
{{ icons.get(cond, icons.fallback) }}