Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 103689

Templating service call in blueprint for mobile_app notification with multiple:true

$
0
0

Is it possible in a blueprint to to template one or more service calls depending on the input?
What I am trying to achieve is that the user selects multiple mobile_app devices to get them notified.
The service call here for is notify.mobile_app_DEVICE_NAME which is nothing easy to achieve with multiple: true in a blueprint.
I tried passing the service call(s) as a list to serivce:, which did not work. I could use templates to essential render the entire action: of the blueprint, as long as I can pass it as a jinja list with sqaure brackets. I’ve tried diferent versions:

blueprint:
  name: Notify to ventilate
  description: An automation blueprint that informs the user that in a given room the temperature is higher than the outside temperature in order to ventilate the room. Especially useful for hot summer days. The temperature sensor must be assigned an area.
  domain: automation
  input:
    outside_temp:
      name: Outside temperature sensor
      description: Sensor for the outside temperature
      selector:
        entity:
          domain: sensor
          device_class: temperature
    forecast_temp:
      name: Forecast temperature
      description: Sensor for the peak temperature of the current day.
      selector:
        entity:
          domain: sensor
          device_class: temperature
    min_temp:
      name: Minimum temperature
      description: Min. temperature for the day (forecast) to enable notifications.
      selector:
        number:
          min: 18
          max: 40
          unit_of_measurement: "C"
          mode: box
    temp_sensor:
      name: Temperature sensor
      description: A sensor reading temperature.
      selector:
        entity:
          domain: sensor
          device_class: temperature
          multiple: true
    notify_device:
      name: Devices to notify
      description: Devices to be notified via the mobile app
      selector:
        device:
          integration: mobile_app
          #multiple: true
    message_title:
      name: Message title
      description: Title of the notification.
      selector:
        text:
    message_string:
      name: Message
      description: Text to append after the area name.
      selector:
       text:
  source_url: 
variables:
  temp_sensors: !input 'temp_sensor'
  min_temp: !input 'min_temp'
  forecast_temp: !input 'forecast_temp'
  outside_temp: !input 'outside_temp'
  notify_devices: !input 'notify_device'
  title: !input 'message_title'
  message: !input 'message_string'
  notify_service: >-
    {% for device in notify_devices %}
      {%- set data.services = data.services + ["notify.mobile_app_"+device] -%}
    {% endfor %}
    {{data.services}}
trigger:
- platform: numeric_state
  entity_id: !input 'temp_sensor'
  below: !input 'outside_temp'    
condition:
- condition: template
  value_template: >-
    {{forecast_temp >= min_temp }}
action:
- device_id: !input notify_device
  domain: mobile_app
  type: notify
  title: "{{ title | default('') }}"
  message: >-
      {% for state in states.sensor%}
        {% if state.entity_id in temp_sensors %}
          {% if state.state >= outside_temp %}
            {{area_name(state.entity_id)}} {{message}}
          {% endif %}
        {% endif %}
      {% endfor %}
  data:
    target: window
    sticky: true
    persistent: true
- wait_template: >
    {{forecast_temp < min_temp}}
  continue_on_timeout: false
- device_id: !input notify_device
  domain: mobile_app
  type: notify
  title: "{{ title | default('') }}"
  message: "clear_notification"
  data:
    target: window

Version 2:

blueprint:
  name: Notify to ventilate
  description: An automation blueprint that informs the user that in a given room the temperature is higher than the outside temperature in order to ventilate the room. Especially useful for hot summer days. The temperature sensor must be assigned an area.
  domain: automation
  input:
    outside_temp:
      name: Outside temperature sensor
      description: Sensor for the outside temperature
      selector:
        entity:
          domain: sensor
          device_class: temperature
    forecast_temp:
      name: Forecast temperature
      description: Sensor for the peak temperature of the current day.
      selector:
        entity:
          domain: sensor
          device_class: temperature
    min_temp:
      name: Minimum temperature
      description: Min. temperature for the day (forecast) to enable notifications.
      selector:
        number:
          min: 18
          max: 40
          unit_of_measurement: "C"
          mode: box
    temp_sensor:
      name: Temperature sensor
      description: A sensor reading temperature.
      selector:
        entity:
          domain: sensor
          device_class: temperature
          multiple: true
    notify_device:
      name: Devices to notify
      description: Devices to be notified via the mobile app
      selector:
        device:
          integration: mobile_app
          multiple: true
    message_title:
      name: Message title
      description: Title of the notification.
      selector:
        text:
    message_string:
      name: Message
      description: Text to append after the area name.
      selector:
       text:
  source_url:
variables:
  temp_sensors: !input 'temp_sensor'
  min_temp: !input 'min_temp'
  forecast_temp: !input 'forecast_temp'
  outside_temp: !input 'outside_temp'
  notify_devices: !input 'notify_device'
  title: !input 'message_title'
  message: !input 'message_string'
  notify_service: >
    {% for device in notify_devices %}
    - notify.mobile_app_{{device_attr(device, "name") | replace(" ", "_")}}
    {% endfor %}
trigger:
- platform: numeric_state
  entity_id: !input 'temp_sensor'
  below: !input 'outside_temp'    
condition:
- condition: template
  value_template: >-
    {{forecast_temp >= min_temp }}
action:
- service: >
    {%- set data = namespace(services=[]) -%}
    {% for device in notify_devices %}
      {%- set data.services = data.services + ["notify.mobile_app_"+device_attr(device, "name") | replace(" ", "_")] -%}
    {% endfor %}
    {{data.services}}
  data:
    message: >-
      {% for state in states.sensor%}
        {% if state.entity_id in temp_sensors %}
          {% if state.state >= outside_temp %}
            {{area_name(state.entity_id)}} {{message}}
          {% endif %}
        {% endif %}
      {% endfor %}
    title: "{{ title | default('') }}"
    data: 
      target: window
      sticky: true
      persistent: true
- wait_template: >
    {{forecast_temp < min_temp}}
  continue_on_timeout: false
- service: >
    {%- set data = namespace(services=[]) -%}
    {% for device in notify_devices %}
      {%- set data.services = data.services + ["notify.mobile_app_"+device_attr(device, "name") | replace(" ", "_")] -%}
    {% endfor %}
    {{data.services}}
  data:
    message: "clear_notification"
    title: "{{ title | default('') }}"
    data: 
      target: window
mode: restart
max_exceeded: silent

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 103689

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>