I want to understand how to use multiple inputs in Blueprints.
In my example, I want to calculate the average value of the temperatures of several weather services and assign this average value to an input_number helper. This works perfectly in my automation:
alias: Durchschnittliche Außentemperatur berechnen
description: ""
trigger:
- platform: state
entity_id:
- weather.51_303864650772276_9_45846140384674
- weather.openweathermap
- weather.forecast_home
attribute: temperature
condition: []
action:
- service: input_number.set_value
target:
entity_id: input_number.durchschnittliche_aussentemperatur
data:
value: >-
{% set averageWeather = namespace(temperature = 0.0 | float, counter=0 |
int) %} {% set annualMeanTemperature = 9.1 | float %}
{% if states('weather.51_303864650772276_9_45846140384674') !=
'unavailable' %} {% set averageWeather.temperature =
averageWeather.temperature+state_attr("weather.51_303864650772276_9_45846140384674",
"temperature") | float %} {% set averageWeather.counter =
averageWeather.counter+1 %} {% endif %}
{% if states('weather.openweathermap') != 'unavailable' %} {% set
averageWeather.temperature =
averageWeather.temperature+state_attr("weather.openweathermap",
"temperature") | float %} {% set averageWeather.counter =
averageWeather.counter+1 %} {% endif %}
{% if states('weather.forecast_home') != 'unavailable' %} {% set
averageWeather.temperature =
averageWeather.temperature+state_attr("weather.forecast_home",
"temperature") | float %} {% set averageWeather.counter =
averageWeather.counter+1 %} {% endif %}
{% if averageWeather.counter != 0 %}
{{averageWeather.temperature/averageWeather.counter}} {% else %} {# No
weather service is available, we use annual mean temperature instead #}
{{annualMeanTemperature}} {% endif %}
mode: single
But if I want to create a blueprint so that I can later use this automation with any number of weather services at any time, I fail because I don’t know the syntax how to use the multiple inputs. I’ve been programming for 40 years but I just don’t understand this mix of Jinja2 and YAML. I have searched the internet for days now but have not found any examples from which I can derive it for my blueprint.
I have found examples like this but I am too stupid for that. I don’t even understand what is going on in this single line of code. That looks like abbreviated voodoo to me:
entity_id: '{{ expand(switches) | selectattr("entity_id", "!=", trigger.entity_id) | map(attribute="entity_id") | list }}'
I’m used to normal arrays, why isn’t there just something like entity[i].attribute?
Anyway this is my naive blueprint. Maybe someone can take me by the hand and explain why the stuff inside the for-loop is not working as one would expect:
blueprint:
name: Average Temperatures of different Weather Services
description: |
## Average Temperatures of different Weather Services v1.0.0
Select multiple weather services to average their reported temperates. The value of an input_number helper will be set to the calculated average temperature. The automation is triggered when any of the reported temperatures changed
### Requirements
* You need to set up an input_number helper first.
domain: automation
homeassistant:
min_version: 2022.5.0
input:
weather_services:
name: Weather services
selector:
entity:
domain:
- weather
multiple: true
avg_temperature_helper:
name: Average Temperature input_number helper
selector:
entity:
domain:
- input_number
multiple: false
mode: single
variables:
weather_services: !input 'weather_services'
avg_temperature_helper: !input 'avg_temperature_helper'
trigger:
- platform: state
entity_id: !input 'weather_services'
attribute: temperature
condition: []
action:
- service: input_number.set_value
target:
entity_id: !input 'avg_temperature_helper'
data:
value: '
{% set averageWeather = namespace(temperature = 0.0 | float, counter=0 | int)%}
{% set annualMeanTemperature = 9.1 | float %}
{% for weather in expand("weather_services") %}
{% if states(weather.entity_id) != "unavailable"%}
{% set averageWeather.temperature = averageWeather.temperature+state_attr(weather.entity_id, "temperature") | float %}
{% set averageWeather.counter = averageWeather.counter+1%}
{% endif %}
{% endfor %}
{% if averageWeather.counter != 0 %}
{{averageWeather.temperature/averageWeather.counter}}
{% else %}
{{annualMeanTemperature}}
{% endif %}
'