MyCool
(mike)
December 29, 2022, 5:04am
1
Need Help adding this service to a ( %if or elif ) string
##### I want to Have this service below run as part of my next string when input_select random is chosen
- service: input_select.select_option
target:
entity_id: input_select.mp3_driveway_alarm_list
data:
option: >
{{ state_attr('input_select.mp3_driveway_alarm_list', 'options')
| reject('in', [states('input_select.mp3_driveway_alarm_list'), 'My option value'])
| list | random }}
- service: media_player.volume_set
data_template:
entity_id: >
{% if is_state("input_select.speaker_select", "ALL Speakers") %}
media_player.all_speakers
{%-elif is_state("input_select.speaker_select", "All Bed Rooms") %}
media_player.all_bedrooms_and_office
{%-elif is_state("input_select.speaker_select", "Master,Lounge,Office") %}
media_player.all_speakers_but_dakota
{%-elif is_state("input_select.speaker_select", "Lounge,Office") %}
media_player.office_and_lounge
{%-elif is_state("input_select.speaker_select", "Master Bed Speaker") %}
media_player.bedroom_speaker
{%-elif is_state("input_select.speaker_select", "Lounge Speaker") %}
media_player.family_room_speaker
{%-elif is_state("input_select.speaker_select", "Office Speaker") %}
media_player.googlehome8575
{%-elif is_state("input_select.speaker_select", "Dakotas Speaker") %}
media_player.dakota_s_bedroom_speaker
{% else %}
media_player.office_and_lounge
{% endif %}
volume_level: "{{states('input_select.driveway_volume')}}"
- service: media_player.play_media
data_template:
entity_id: >
{% if is_state("input_select.speaker_select", "ALL Speakers") %}
media_player.all_speakers
{%-elif is_state("input_select.speaker_select", "All Bed Rooms") %}
media_player.all_bedrooms_and_office
{%-elif is_state("input_select.speaker_select", "Master,Lounge,Office") %}
media_player.all_speakers_but_dakota
{%-elif is_state("input_select.speaker_select", "Lounge,Office") %}
media_player.office_and_lounge
{%-elif is_state("input_select.speaker_select", "Master Bed Speaker") %}
media_player.bedroom_speaker
{%-elif is_state("input_select.speaker_select", "Lounge Speaker") %}
media_player.family_room_speaker
{%-elif is_state("input_select.speaker_select", "Office Speaker") %}
media_player.googlehome8575
{%-elif is_state("input_select.speaker_select", "Dakotas Speaker") %}
media_player.dakota_s_bedroom_speaker
{% else %}
media_player.office_and_lounge
{% endif %}
media_content_type: 'music'
media_content_id: >
{% if is_state("input_select.mp3_driveway_alarm_list", "Yoda-BLR-Hmm-Ha") %}
http://192.168.1/local/Yoda-BLR-Hmm-Ha.mp3
{%-elif is_state("input_select.mp3_driveway_alarm_list", "Metalica One") %}
http://192.168.1./local/metalica_one_intro.mp3
{%-elif is_state("input_select.mp3_driveway_alarm_list", "MessageAlert") %}
http://192.168.1./local/MessageAlert.mp3
{%-elif is_state("input_select.mp3_driveway_alarm_list", "pinksoldier") %}
http://192.168.1/local/pinksoldier.mp3
{%-elif is_state("input_select.mp3_driveway_alarm_list", "carlpoppa2") %}
http://192.168.1/local/carlpoppa2.mp3
{%-elif is_state("input_select.mp3_driveway_alarm_list", "Trump Driveway") %}
http://192.168.1/local/Trump Driveway.mp3
{%-elif is_state("input_select.mp3_driveway_alarm_list", "Driveway Alert") %}
http://192.168.1/local/driveway_movement.mp3
{%-elif is_state("input_select.mp3_driveway_alarm_list", "Driveway Alert2") %}
http://192.168.1./local/driveway_single.mp3
{%-elif is_state("input_select.mp3_driveway_alarm_list", "Squid Games") %}
http://192.168.1./local/squid_game.mp3
**{%-elif is_state("input_select.mp3_driveway_alarm_list", "Random") %}**
** ########### Add Random input select code here ################**
{% else %}
http://192.168.1./local/Trump Driveway.mp3
{% endif %}
- service: camera.snapshot
data:
entity_id: camera.driveway
filename: /config/www/images/driveway.jpg
- delay:
seconds: 2
- service: homeassistant.turn_on
entity_id: script.driveway_notify_alarm
- variables:
p_selector_state: '{{ states("input_select.speaker_select") }}'
selected_player: >
{% set mapper = {
"ALL Speakers": "media_player.all_speakers",
"All Bed Rooms": "media_player.all_bedrooms_and_office",
"Master,Lounge,Office": "media_player.all_speakers_but_dakota",
"Lounge,Office": "media_player.office_and_lounge",
"Master Bed Speaker": "media_player.bedroom_speaker",
"Lounge Speaker": "media_player.family_room_speaker",
"Office Speaker": "media_player.googlehome8575",
"Dakotas Speaker": "media_player.dakota_s_bedroom_speaker"} %}
{% if p_selector_state in mapper.keys() %}
{{ mapper.get(p_selector_state) }}
{% else %}
media_player.office_and_lounge
{% endif %}
content_selector: '{{ states("input_select.mp3_driveway_alarm_list") }}'
selected_content: >
{% set mapper = {
"Yoda-BLR-Hmm-Ha": "http://192.168.1/local/Yoda-BLR-Hmm-Ha.mp3",
"Metalica One": "http://192.168.1./local/metalica_one_intro.mp3",
"MessageAlert": "http://192.168.1./local/MessageAlert.mp3",
"pinksoldier": "http://192.168.1/local/pinksoldier.mp3",
"carlpoppa2": "http://192.168.1/local/carlpoppa2.mp3",
"Trump Driveway": "http://192.168.1/local/Trump Driveway.mp3",
"Driveway Alert": "http://192.168.1/local/driveway_movement.mp3",
"Driveway Alert2": "http://192.168.1./local/driveway_single.mp3",
"Squid Games": "http://192.168.1./local/squid_game.mp3" } %}
{% if content_selector == 'Random' %}
{{ state_attr('input_select.mp3_driveway_alarm_list', 'options')
| reject('in', [ content_selector, 'My option value' ])
| list | random }}
{%- elif content_selector in mapper.keys() %}
{{ mapper.get(content_selector) }}
{% else %}
http://192.168.1./local/Trump Driveway.mp3
{% endif %}
- service: media_player.volume_set
data_template:
entity_id: "{{ selected_player }}"
volume_level: "{{ states('input_select.driveway_volume') }}"
- service: media_player.play_media
data_template:
entity_id: "{{ selected_player }}"
media_content_type: 'music'
media_content_id: "{{ selected_content }}"
- service: camera.snapshot
data:
entity_id: camera.driveway
filename: /config/www/images/driveway.jpg
- delay:
seconds: 2
- service: homeassistant.turn_on
entity_id: script.driveway_notify_alarm
I couldn’t discern from your original post whether you actually need the input select to be set or you just wanted a random selection from the options.
1 Like
MyCool
(mike)
December 29, 2022, 7:43am
3
Wow you must be a absolute master. Thank you so much for this I’m still amazed how quickly you did this…
Anyhow I’m obviously doing something wrong,
I have never seen the - veriables: before and not much info on Google.
Question is where do I add this code youve crafted is it after a
service call
Or other I’m not sure as I’m still learning.
Would you be kind enough to point out the obvious lol
As I get config errors every way I’ve tried so far…
Thank you so so much too
- alias: test1
trigger:
platform: state
entity_id:
- binary_sensor.driveway_beem
to: 'on'
condition:
- condition: template
value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.driveway_linecrossing_mp3.attributes.last_triggered | default(0)) | int > 200)}}'
action:
### probably needs a service:####
- variables:
p_selector_state: '{{state("input_select.speaker_select"}}'
selected_player: >
{% set mapper = {
"ALL Speakers": "media_player.all_speakers",
"All Bed Rooms": "media_player.all_bedrooms_and_office",
"Master,Lounge,Office": "media_player.all_speakers_but_dakota",
"Lounge,Office": "media_player.office_and_lounge",
"Master Bed Speaker": "media_player.bedroom_speaker",
"Lounge Speaker": "media_player.family_room_speaker",
"Office Speaker": "media_player.googlehome8575",
"Dakotas Speaker": "media_player.dakota_s_bedroom_speaker"} %}
{% if p_selector_state in mapper.keys() %}
{{ mapper.get(p_selector_state) }}
{% else %}
media_player.office_and_lounge
{% endif %}
content_selector: '{{ state("input_select.mp3_driveway_alarm_list") }}'
selected_content: >
{% set mapper = {
"Yoda-BLR-Hmm-Ha": "http://192.168.1.44/local/Yoda-BLR-Hmm-Ha.mp3",
"Metalica One": "http://192.168.1.44/local/metalica_one_intro.mp3",
"MessageAlert": "http://192.168.1.44/local/MessageAlert.mp3",
"pinksoldier": "http://192.168.1.44/local/pinksoldier.mp3",
"carlpoppa2": "http://192.168.1.44/local/carlpoppa2.mp3",
"Trump Driveway": "http://192.168.1.44/local/Trump Driveway.mp3",
"Driveway Alert": "http://192.168.1.44/local/driveway_movement.mp3",
"Driveway Alert2": "http://192.168.1.44/local/driveway_single.mp3",
"Squid Games": "http://192.168.1.44/local/squid_game.mp3" } %}
{% if content_selector == 'Random' %}
{{ state_attr('input_select.mp3_driveway_alarm_list', 'options')
| reject('in', [ content_selector, 'My option value' ])
| list | random }}
{%- elif content_selector in mapper.keys() %}
{{ mapper.get(content_selector) }}
{% else %}
http://192.168.1.44/local/Trump Driveway.mp3
{% endif %}
- service: media_player.volume_set
data_template:
entity_id: "{{ selected_player }}"
volume_level: "{{ states('input_select.driveway_volume') }}"
- service: media_player.play_media
data_template:
entity_id: "{{ selected_player }}"
media_content_type: 'music'
media_content_id: "{{ selected_content }}"
- service: camera.snapshot
data:
entity_id: camera.driveway
filename: /config/www/images/driveway.jpg
- delay:
seconds: 2
- service: homeassistant.turn_on
entity_id: script.driveway_notify_alarm
You’re not to blame… looks like I had a couple typos. I was missing an ‘s’ at the end of two states()
functions and had a missing )
. I think I’ve fixed all the typos and applied it to your full test automation below.
I also realized that it isn’t necessary to re-query the Input select helper to pick a random content value since we already have access to all the options in the mapper.
You will need to double check that the spelling of entity ids and selector options matches the actual values in you HA instance. If you still get errors make sure to share the error message to aid with debugging.
Use of variables is covered in the Script Syntax documentation
- alias: test1
trigger:
platform: state
entity_id:
- binary_sensor.driveway_beem
to: 'on'
condition:
- condition: template
value_template: >
{{ (now() -
(state_attr("automation.driveway_linecrossing_mp3", "last_triggered")
| default(as_datetime(0), 1)) ) > timedelta(seconds=200) }}
action:
- variables:
p_selector_state: '{{ states("input_select.speaker_select") }}'
selected_player: >
{% set mapper = {
"ALL Speakers": "media_player.all_speakers",
"All Bed Rooms": "media_player.all_bedrooms_and_office",
"Master,Lounge,Office": "media_player.all_speakers_but_dakota",
"Lounge,Office": "media_player.office_and_lounge",
"Master Bed Speaker": "media_player.bedroom_speaker",
"Lounge Speaker": "media_player.family_room_speaker",
"Office Speaker": "media_player.googlehome8575",
"Dakotas Speaker": "media_player.dakota_s_bedroom_speaker"} %}
{% if p_selector_state in mapper.keys() %}
{{ mapper.get(p_selector_state) }}
{% else %}
media_player.office_and_lounge
{% endif %}
content_selector: '{{ states("input_select.mp3_driveway_alarm_list") }}'
selected_content: >
{% set mapper = {
"Yoda-BLR-Hmm-Ha": "http://192.168.1.44/local/Yoda-BLR-Hmm-Ha.mp3",
"Metalica One": "http://192.168.1.44/local/metalica_one_intro.mp3",
"MessageAlert": "http://192.168.1.44/local/MessageAlert.mp3",
"pinksoldier": "http://192.168.1.44/local/pinksoldier.mp3",
"carlpoppa2": "http://192.168.1.44/local/carlpoppa2.mp3",
"Trump Driveway": "http://192.168.1.44/local/Trump Driveway.mp3",
"Driveway Alert": "http://192.168.1.44/local/driveway_movement.mp3",
"Driveway Alert2": "http://192.168.1.44/local/driveway_single.mp3",
"Squid Games": "http://192.168.1.44/local/squid_game.mp3" } %}
{% if content_selector == 'Random' %}
{{ mapper.values() | list | random }}
{%- elif content_selector in mapper.keys() %}
{{ mapper.get(content_selector) }}
{% else %}
http://192.168.1.44/local/Trump Driveway.mp3
{% endif %}
- service: media_player.volume_set
data_template:
entity_id: "{{ selected_player }}"
volume_level: "{{ states('input_select.driveway_volume') }}"
- service: media_player.play_media
data_template:
entity_id: "{{ selected_player }}"
media_content_type: 'music'
media_content_id: "{{ selected_content }}"
- service: camera.snapshot
data:
entity_id: camera.driveway
filename: /config/www/images/driveway.jpg
- delay:
seconds: 2
- service: homeassistant.turn_on
entity_id: script.driveway_notify_alarm
1 Like
MyCool
(mike)
December 29, 2022, 8:19pm
5
Omg it works and first try, thank you so much real appreciate the help. One last question, I’ve been playing with HASS for a few years on n off slowly learning how yaml works. You obviously know your stuff, is there anyway I can expand my understanding on json and templating my issue is I don’t understand the formating and basic of templates and json. I just look for patterns and copy n past most of the time. I would love to understand it more. Mainly so I can write my own strings.
Once again thank you so so much hope you’ve had a good Christmas
I’ve still got a lot to learn too… I just find templates really useful
There’s no single source that covers all the things you can do with templates in HA, but make sure to check out:
Home Assistant Templating : This is the the most comprehensive source for HA-specific functions
Jinja Docs : These docs are not geared toward new/casual users, but they contain a lot of important information.
Jinja for Ninjas : @skalavala has put together a well organized tutorial and group of examples. It’s been a while since this was updated, so there are better/easier ways to do some of the things shown, but it’s still a good source.
HA also allows many Python methods to work in templates (for example the get()
method in the templates above). I’ve never seen a complete list of which methods work and which don’t… I’ve mostly just stumbled upon them in posts on this forum. My understanding is that the docs gatekeepers consider these to be out of scope for inclusion in Home Assistant’s documentation.
1 Like