aloha,
I really feel stupid but I do not find any meaningful documentation how to work with lists / maps:
have a simple name,value map (tv channels)
eg
channels:
name: channel1
id: 22
name: channels
id:55
…
so I think I put the definition of this list close in the according HA script
then it will need to handle the incoming channel name as parameter
and use this parameter as lookup criteria against the list
coming from Java devolpment background I expected that as basic …
but being quite new in HA, yaml world it is somehow tough
any advice ?
thänx in advance
tom_l
March 31, 2024, 10:14am
2
You don’t give an example of where you want to use this so this answer will be very generic:
value_template: >
{% set mapper = { 22 : 'channel1', 55 : 'channels' } %}
{% set x = states('sensor.id')|int(0) %}
{{ mapper[x] if x in mapper else 'Failed' }}
EDIT: there was some talk of input_select helpers supporting values and names but nothing has come of it yet.
petro
(Petro)
March 31, 2024, 10:39am
3
to expand on toms comment
script assuming you’re using play_media…
change_channel:
fields:
channel:
variables:
channels:
22: channel1
53: channels
selected: >
{{ channels.get(channel) }}
sequence:
# check for valid channel, if not, stop script.
- condition: template
value_template: "{{ selected is not none }}"
# we got valid channel, now change to it.
- service: media_player.play_media
target:
entity_id: media_player.whatever_yours_is
data:
... (other data here)...
media_content_id: "{{ selected }}"
Then to call it where the automation waits for the service to execute…
- service: script.change_channel
data:
channel: 22
or call it without waiting
- service: script.turn_on
target:
entity_id: script.change_channel
data:
variables:
channel: 22
1 Like
thaaanx for the quick response - will try this as soon I am back in vienna …
so if I have the map as defined in our examples (‘channels’) - I can simply call channel.get(key) to get the associated value
cool
so,
back and tried a test code in a script as follows…
alias: tvboxByChannel
fields:
channel:
selector:
text: null
name: channel
variables:
channels: >-
orf1:11 orf2:12 orf3:22 3sat:48 arte:47 atv:13 servustv:15 puls4:14 ard:23
zdf:16 pro7:17 sat1:21 rtl:18 vox:20 w24:19 okto:53 kuriert:51 laendletv:131
comedycentral:44 syfy:205 tele5:46 automotorsport:110 puls4xx:27 oe24:28
ntv:29 phoenix:56 zdfinfo:57 doku:38 n24doku:39 spiegelgeschichte:219 one:43
zdfneo:42 orfv:127 kabel1:24 alpha:59 br:49 wdr:61 mdr:62 swr:63 ndr:64
hr:65 srfinfo:67 nitro:33 7max:36 rtl2:25 sixx:34 dmax:37 atv2:36 krone:30
rtlup:35
sequence:
service: notify.persistent_notification
data:
message: hallo {{channel}} looking up id => {{channels.get(channel)}}
…
i get following error on running the script
UndefinedError: ‘homeassistant.util.yaml.objects.NodeStrClass object’ has no attribute ‘get’
what is going on here ? hmm …
123
(Taras)
March 31, 2024, 1:37pm
6
Based on the error message, Home Assistant thinks the channel
variable contains a string . It implies you didn’t successfully define its value as a dictionary . (You used a line continuation character >-
to indicate the start of a template yet that’s not needed because it’s not a template)
It’s difficult to tell what you have created because your example is unformatted. Compare the appearance of your example to petro’s (which is formatted correctly). Here’s a guide for formatting YAML:
Spaces at the beginning of the line are critical in yaml, and people trying to help you may want to copy your code so that they can try it out themselves, so please format it correctly when you are posting.
Use the preformatted text tool (</> in the edit post toolbar). If it is not immediately visible, it will be in the cogwheel menu.
[Code_format]
Alternatively you can use three backticks (the key is on the far left, 2nd row on en keyboards) on their own line above and below the code. That i…
petro
(Petro)
March 31, 2024, 2:25pm
7
I can’t tell because your code isn’t formatted.
ok … will try and learn (coming from a more strongly typed coding world - this is quite a different planet here where every single space makes a difference
but thänx for pointing me to a direction … will try and feedback
c
petro
(Petro)
March 31, 2024, 3:04pm
9
each key value pair should be on a separate line at the same indentation, like I have in my example. I believe you put them all in 1 line