So I was right in the first reply to your post.
It seems you were. It just wasn’t enough information for a rookie like me to work on unfortunately. Thanks for your input though. I appreciate it.
I couldn’t find anything wrong with the automation’s logic. In fact, I couldn’t understand how it could possibly produce a blank entity_id given that the automation contains two safeguards:
- its condition rejects a blank to_state
- the data_template has an ‘else’
So my next question to you was going to be … did you reload the modified automation?
I’m certainly learning. A tad different to all the coding I’m use to. Thanks for your help! Really do appreciate it.
Question for you, if the media_player is off, is there any harm in sending it a media_play_pause
command? Would the command simply be ignored (given that the device is off or sleeping) or would it cause the device to do something like wake up out of sleep mode?
No harm. I just got an error in my logs saying unable to change state on media player as no session active.
OK, if you don’t mind the error message, here’s a streamlined version of the template. Even if you don’t use it, you may be able to apply some of the techniques it employs in other projects.
- id: 'masterswitch'
hide_entity: true
alias: Toggle lights on and off
trigger:
- platform: state
entity_id: sensor.0x00158d00034039aa_click
condition:
condition: template
value_template: "{{ not trigger.to_state.state == '' }}"
action:
service_template: "{{ 'media_player.media_play_pause' if trigger.to_state.state == 'single' else 'light.toggle' }}"
data_template:
entity_id: >
{% set actions = {'single': 'media_player.the_ultra',
'double': 'light.upstairs_lounge',
'triple': 'light.upstairs_bedroom',
'quadruple': 'light.upstairs_lounge, light.upstairs_bedroom'} %}
{{ actions[trigger.to_state.state] if trigger.to_state.state in actions.keys() else 'light.none' }}
It’s possible to add a test to check if the media_player is on but then it gains complexity and is no improvement of what you’re already using.
EDIT
Correction. Added missing closing %}
to set actions
.
Ooh very cool. Thanks for that! I most certainly will refer back to this for future projects. It’s good to learn the markup.
Is that code missing some closing tags in the set actions section?
Yes; good catch! I’ve corrected it.