I’m having some issues getting the following automation to work properly.
I had everything working just fine when I wasn’t bothering with a device_id
, but since I’d like to use a single automation for multiple devices and multiple tags, I decided to template most of the automation, so it would work with multiple variables. This is where I started getting into problems.
Currently, when I scan an NFC tag, the correct echo device speaks the correct show
and tv
name and then the correct media_player
turns on, but the show doesn’t start.
Does anyone else see anything wrong in this automation that might cause the issues I’m describing?
( session
tells my rest_command which instance of Emby to send the selected show to, and start playing; I have a sensor that populates with the session ID of the Emby instance once it opens. If Emby isn’t open, the sensor has no value; hence the wait_template)
- alias: EMBY - NFC Tag Scanned
trigger:
platform: event
event_type: tag_scanned
variables:
scanners:
a67fd665fe2815113ce31287a4edb141:
sensor_id: 'sensor.embysession_livingroom'
session_id: '{{ states.sensor.embysession_livingroom.state }}'
tts_target: media_player.echo_show
media_player: switch.livingroom_hub_emby_theater
tv: Livingroom TV
turn_on_emby: script.turn_on_emby_livingroom
open_emby: script.open_emby_livingroom
tags:
04-30-8F-01-5D-40-03:
show: 1149104
title: Baby Shark
55-38-CB-2A:
show: '{{ range(614262,614301)| random }}'
title: Dinosaur Train
condition:
- "{{ trigger.event.data.tag_id in tags }}"
- "{{ trigger.event.data.device_id in scanners }}"
action:
- service: notify.alexa_media
data_template:
target: '{{ scanners[trigger.event.data.device_id].tts_target }}'
message: "Playing {{ tags[trigger.event.data.tag_id].title }} on {{ scanners[trigger.event.data.device_id].tv }}"
data:
type: "tts"
- service_template: >
{% if (is_state( scanners[trigger.event.data.device_id].media_player , "off")) %}
{{ scanners[trigger.event.data.device_id].turn_on_emby }}
{% else %}
{{ scanners[trigger.event.data.device_id].open_emby }}
{% endif %}
- wait_template: "{{ not is_state(scanners[trigger.event.data.device_id].sensor_id, '') }}"
- service: rest_command.emby_start_show
data_template:
show: "{{ tags[trigger.event.data.tag_id].show }}"
token: !secret emby_key
userid: !secret emby_user
session: "{{ scanners[trigger.event.data.device_id].session_id }}"
I’ve narrowed the issue down to the last line of the automation; something is wrong with session_id
. if I replace the last line of my automation with '{{ states.sensor.embysession_livingroom.state }}'
everything works fine; the issue is, I need this to be a variable in order to use this automation with multiple devices. Am I calling the variable incorrectly or something?
Also, If I scan the tag a second time (after Emby is open) the title plays just as it should, which definitely adds to my confusion.
EDIT: I’ve managed to find a workaround to get my automation working by replacing session_id: '{{ states.sensor.embysession_livingroom.state }}'
with session_id: (my session ID)
While this works, I’d much rather be able to use the sensor data instead of the actual string, just in case for some reason that ID changes. That, and I’d just like to understand why that’s the only part of my automation that doesn’t seem to work properly with a templated value (and the fact it works after scanning the tag a second time is really strange).