Automation Assistance - Calling Script on Triggers

Hi Folks,
Hopefully a quick question for the smarter people than me.
I have a seemingly simple automation I’d like to run as follows:
If someone plays the XBOX (and is signed into XBOX Live), the appropriate Amp input is selected, and it’s at least an hour before sunset run a script (set mood lighting)…
Can’t seem to trigger the automation…
I can confirm the script works when manually triggered.
I can confirm the Value_Template is True when the input is on the Amp - tested via Developer Tools…
I can see that the XBox is signed in / Online - the API’s work okay it seems.

- id: 'SOMENUMBER'
  alias: GameModeON
  description: ''
  trigger:
  - platform: template
    value_template: '{{ states.media_player.denonavr.attributes.source == ''Game''
      }}'
    for:
      minutes: 1
  condition:
  - after: sunset
    after_offset: -01:00
    condition: sun
  - condition: state
    entity_id: sensor.SOMEXBOXIDHERE
    state: Online
  action:
  - data:
      entity_id: '1566105798599'
    service: script.turn_on

I’ve tried calling the script via it’s alias - no difference.

Can share the script if needed.

Scratching my head…
Any clues?

What is the entity_id of the script? It should be something like script.xxxx. I think you need to put this entity_id here instead of the id.

action:
  - data:
      entity_id: '1566105798599'
    service: script.turn_on
1 Like

Thank you.
I’ll give that a go.
Will advise.

You can go to Developer Tools -> Services and choose “automation.trigger” and for the entity_id choose the name of your automation. When you trigger an automation manually like this, all conditions and triggers are skipped and it goes directly to the action part. This way you can test whether your action works or not, without taking conditions or triggers into consideration.

Thanks for the extra info.
I wasn’t sure if manually triggering the automation bypassed the triggers and conditions.
manually triggering executes the script - that bit works (thanks for the entity_id: tip).

So my conditions might be a little dodgy.
Could I ask, should I use " instead of ’ around strings - like for the XBOX State reporting as Offline or Online? ‘Online’ or “Online” ?
I’ve not used either. I’ll test now. :slight_smile:

I think it could more be a problem of upper-/lowercase. If you go to Developer Tools -> States what do you see as the state for the xbox sensor is it “online” or “Online” or maybe even “on”?

Offline and Online

I think your problem is in the value_template of the trigger. You put two single quotes before and after Game instead of double quotes.

In general, if there is no space or special character in the string you can leave out the quotes around the string. For templates always put double quotes around the template and single quotes inside the template or vice versa.

So I now have:

- id: '1575607092024'
  alias: GameModeON
  description: ''
  trigger:
  - platform: template
    value_template: '{{ states.media_player.denonavr.attributes.source == "Game" }}'
  condition:
  - after: sunset
    after_offset: '-01:00:00'
    condition: sun
  - condition: state
    entity_id: sensor.XBOXUID
    state: Online
  action:
  - data:
      entity_id: script.1566105798599
    service: script.turn_on

Pretty sure the caps are right.
Pretty sure the value_template is True as it stands now… If the value_template returns True, then the condition should be met?

Don’t mix up conditions and triggers, if the value_template evaluates to True, the automation will be triggered.

I don’t see any errors in this code, is it not working?

I’ll test again this evening.
Thanks for following up.
I’ll take conditions out until it works… then take it from there.
Will let you know :slight_smile:

Hi again,
FINALLY managed to test it.
No luck :frowning:
MSFT API: Online (case specific)
media_player.denonavr: on (source: Game)
Can manually execute/trigger the script : no problem
When I check the attribute template in the Developer Tools / Template is comes back True
Specifically I’ve added this

{{ states.media_player.denonavr.attributes.source == "Game" }}

Do I need to enter something else perhaps in the automation from a syntax perspective?

about that last template, you can write that as follows:

- id: '1575607092024'
  alias: GameModeON
  description: ''
  trigger:
    - platform: template
      value_template: >
        {{ is_state_attr('media_player.denonavr','source','Game' )}}
  condition:
    - condition: sun
      after: sunset
      after_offset: '-01:00:00'
    - condition: template
      value_template: >
        {{is_state('sensor.XBOXUID','Online')}}
  action:
    - service: script.1566105798599

always try to write the conditions as such as possible in a template form so yo can check in the in the developer template engine.

have a look what the above gives, and, if an individual template doesn’t evaluate to true, check why and report back here?

note that Ive reordered the syntax a bit to make it easier to read for humans :wink: (I take it you’ve been using the UI automation editor?

btw, why would you have named the script with such a meaningless name? this id not very helpful in checking nor understanding the automation.

please share the script, so we can change that too.

Hi,
Thanks for the prompt reply. Most awesome.
Here’s the script:

'1566105798599':
  alias: EntertainmentTime
  sequence:
  - alias: Entertainment Time
    data:
      entity_id: light.hue_lightstrip_plus_1_2,light.livingroomlightshade
      transition: '5'
    service: light.turn_off
  - data:
      brightness: '190'
      entity_id: light.entertainmentlightstrip
      rgb_color:
      - 226
      - 146
      - 255
      transition: '5'
    service: light.turn_on

The script name is the ID that was auto generated… I’ve given it an alias, but never renamed the actual ID. Wasn’t sure if I could… ?

I’ll try your example out when I get tick - I’ve still got training wheels on with this stuff - I do use the UI Automation editor… Can I simply create my own automations (with sensible names) from scratch in the automations.yaml file, there doesn’t need to be a machine generated ID or anything, as long it’s unique?

I’ll be back!
Thanks again!

at the very least, this should be taken out of the sequence. Ive rewritten the script somewhat, along the same logic as the automation above.
this would do it:

script:
  entertainment_time:
    alias: Entertainment Time
    sequence:
      - service: light.turn_off
        data: 
          entity_id: light.hue_lightstrip_plus_1_2,light.livingroomlightshade
          transition: 5
      - service: light.turn_on
        data:
          entity_id: light.entertainmentlightstrip
          brightness: 190
          rgb_color: [226,146,255]
          transition: 5

and then call the script with the service: script.entertainment_time line in the automation

Your code is much more elegant than mine.
Excellent! - I’m keen to improve the rest of my code!
I can’t test your suggestion (to my problem) until tomorrow unfortunately.

So, just to clarify, I don’t need to use the system-generated ID’s for scripts and automations, as long as I ensure the meaningful ID’s I create are unique?

well, thats how I’ve always understood it yes. I don’t use the editor, but use yaml mode, but even then, of course always use unique names for automations and scripts, for anything.

one way to find out :wink:
unique doesn’t necessarily equal ‘a meaningless randomly generated number’

Worse in my book as I just can’t keep track of 13 digit numbers
You could assign the first 2 numbers to a room, the next 2 to appliance type, the next 2… You get my drift. But why bother ?
I agree with Marius, just name it sensibly and uniquely and you’re laughing. :rofl:

Okay, tested again… No luck still. :frowning:

Here’s what I’ve got…
Automation is:

- id: '1575607092024'
  alias: GameModeON
  description: ''
  trigger:
  - platform: template
    value_template: '{{ is_state_attr(''media_player.denonavr'',''source'',''Game''
      )}}

      '
  condition:
  - condition: sun
    after: sunset
    after_offset: -01:00:00
  - condition: template
    value_template: '{{is_state(''sensor.XBOXUID'',''Online'')}}

      '
  action:
  - service: script.1566105798599

Script is (not yet optimised):

'1566105798599':
  alias: EntertainmentTime
  sequence:
  - alias: Entertainment Time
    data:
      entity_id: light.hue_lightstrip_plus_1_2,light.livingroomlightshade
      transition: '5'
    service: light.turn_off
  - data:
      brightness: '190'
      entity_id: light.entertainmentlightstrip
      rgb_color:
      - 226
      - 146
      - 255
      transition: '5'
    service: light.turn_on

Can trigger the script - works okay.

Entities when tested:
" sensor.XBOXUID Online"
Templates when tested under Developer Tools / Template:

{{ is_state_attr('media_player.denonavr','source','Game' )}} True

{{is_state('sensor.XBOXUID','Online')}} True

It is dark outside, well and truly an hour before sunset…

The amplifier states / attributes are:

 media_player.denonavr	on	
volume_level: 0.385
is_volume_muted: false
media_content_type: channel
media_title: Game
source: Game
source_list: AUX,AppleTV,Blu-ray,Bluetooth,DVD,Game,HEOS Music,Phono,SONOS,TV Audio,Tuner
sound_mode: DTS SURROUND
blah blah

I’m sure it’s going to be simple… but I just can’t see it.
I tried removing the XBOX API from the conditions, no change.