Hello,
I’m trying to get the following working:
Say a specific phrase to Google Assistant, which triggers an ifttt webhook with a data template, which then triggers an Automation in Home Assistant, which switches the input source of the tv based on the said-phrase (to google assistant)
The Webhook to ifttt is up and running and I’m receiving the corresponding message. So I’m gonna skip everything up to this part.
The received webhook looks like this (source is the part that I want to process):
{
"event_type": "ifttt_webhook_received",
"data": {
"action": "call_service",
"service": "media_player.select_source",
"type": "source_change",
"entity_id": "media_player.lg_webos_smart_tv",
"source": " to Spotify",
"webhook_id": XXXXXXXXXXXXXXXXXXX"
},
"origin": "LOCAL",
"time_fired": "2020-12-09T14:50:16.014181+00:00",
"context": {
"id": "XXXXXXXXXXXXX",
"parent_id": null,
"user_id": null
}
}
The code which should extract the source and matches it against the sourcelist entry of the media_player device looks like so:
{% set namespace = namespace(source=[]) -%}
{% for word in trigger.event.data.source.split(' ') -%}
{% for source in state_attr(trigger.event.data.entity_id, 'source_list') -%}
{% for source_word in source.split(' ') -%}
{% if (source_word|capitalize).startswith(word|capitalize) -%}
{% set namespace.source = namespace.source + [source] -%}
{%endif-%}{% endfor -%}{% endfor -%}{% endfor -%}
{{namespace.source[0]}}
The idea is that the automation fetches the list of available sources. Loops through them in search for a matching word (so I’m able to say Prime or Prime Video instead of the full “Amazon Prime Video”-Source). If it finds a match it adds it to the namespace.source
At the end it should only report the first one of the findings.
If I paste this code into the developer jinja editor, it works just as i want to (I just need to replace the trigger.event.data
stuff with an example like “to Prime
”.
The code of the full automation looks like this:
alias: IFTTT - Google Assistant - TV Input Source
description: ''
trigger:
- event_data:
action: call_service
event_type: ifttt_webhook_received
platform: event
condition:
- condition: template
value_template: '{{ trigger.event.data.service == "media_player.select_source"}}'
action:
- service: '{{ trigger.event.data.service }}'
data_template:
entity_id: '{{ trigger.event.data.entity_id }}'
source: >-
"{% set namespace = namespace(source=[]) -%}{% for word in
trigger.event.data.source.split(' ') -%}{% for source in
state_attr(trigger.event.data.entity_id, 'source_list') -%}{% for
source_word in source.split(' ') -%}{% if
(source_word|capitalize).startswith(word|capitalize) -%}{% set
namespace.source = namespace.source + [source] -%}{%endif-%}{% endfor
-%}{% endfor -%}{% endfor -%}{{namespace.source[0]}}"
mode: single
In the Home-Assistant Log I get the following messages no matter what source I choose:
Source 'Amazon Prime Video' not found for LG webOS Smart TV
Amazon Prime Video is the first source of the media_player source attribute.
Hopefully someone can help me.
Thanks in advance
Timo