Script and Condition

Hi

I am trying to make my a script play along with a condition containing templates, and I simply cant figure out, why this is not working.

Long story short, when I press a switch, I want an automation to send two variables to a script. The script checks if the switch is still on, if so, volume up and call another script that does exactly the same (loop so I can longpress the switch).

It seems to work without the condition, but as I see it, there is nothing wrong with my condition, is there?

Automation:

- alias: Volume Up Køkken
  initial_state: True
  trigger:
    platform: state
    entity_id: switch.stue_4_overst_hojre
    to: 'on'
  action:
      service: script.volume_op_1
      data_template:
        switch: "stue_4_overst_hojre"
        where: "livingroom"

Scripts:

volume_op_1:
  alias: "Skru op for højttaler 1"
  sequence:
    - delay:
        miliseconds: 200
    - condition: template
      value_template: "{{ is_state('switch.[switch]', 'on') }}"
    - service: media_player.volume_up
      data_template:
        entity_id: "media_player.{{ where }}"
    - service: script.volume_op_2
      data_template:
        switch: "{{ switch }}"
        where: "{{ where }}"  
          
volume_op_2:
  sequence:
    - delay:
        miliseconds: 200
    - condition: template
      value_template: "{{ is_state('switch.[switch]', 'on') }}"
    - service: media_player.volume_up
      data_template:
        entity_id: "media_player.{{ where }}"
    - service: script.volume_op_1
      data_template:
        switch: "{{ switch }}"
        where: "{{ where }}"

change that to this:

      value_template: "{{ is_state('switch.{}'.format(switch), 'on') }}"

using brackets will not work on a string. That only works on objects. The function is_state is looking for an entity_id string. Which will only have the format 'domain.object_id', not 'domain.[object_id]'.

So to clarify. This is a state object:

states.light.laundry

Items inside can be accessed in 1 of 2 ways:

Direct access to state:

states.light.laundry.state

Keyword access to state:

states.light.laundry['state']

When using any of the built in attribute accessing functions: states(), state_attr(), is_state(), etc. They expect strings. They can only be accessed with the entity_id.

states('light.laundry')
state_attr('light.laundry','brightness')
is_state('light.laundry','on')

Why not pass the entity id to the script instead of just the latter part?

action:
      service: script.volume_op_1
      data_template:
        switch: "switch.stue_4_overst_hojre"
        where: "livingroom"
condition: template
      value_template: "{{ is_state( {{ switch }} , 'on') }}"

No reason actually. I guess that was right about when I gave up :stuck_out_tongue:

That helped a lot on the condition, thanks.
Supposed that I change the variable switch to switch.tryk_4_overst_hojre and where to media_player.livingroom, whould it make the template_value more clean, or will it have no effect.

And one more question. I try to pass the variable to the other script, but that part seems to not work aswell. Is that even possible to pass a variable on like that?

Think I was to fast on the trigger. I had a test automation turning up the volume once along with the actual automation I wanted to use. So it seems the condition still don’t let me pass. So back to the question if it could help setting the entire entity Id into the variable

That shouldn’t work! remove the interior {{}} and it should work.

Yes it would make it cleaner.

- alias: Volume Up Køkken
  initial_state: True
  trigger:
    platform: state
    entity_id: switch.stue_4_overst_hojre
    to: 'on'
  action:
      service: script.volume_op_1
      data_template:
        switch: switch.stue_4_overst_hojre
        where: media_player.livingroom
volume_op_1:
  alias: "Skru op for højttaler 1"
  sequence:
    - delay:
        miliseconds: 200
    - condition: template
      value_template: "{{ is_state(switch, 'on') }}"
    - service: media_player.volume_up
      data_template:
        entity_id: "{{ where }}"
    - service: script.volume_op_2
      data_template:
        switch: "{{ switch }}"
        where: "{{ where }}"  
          
volume_op_2:
  sequence:
    - delay:
        miliseconds: 200
    - condition: template
      value_template: "{{ is_state(switch, 'on') }}"
    - service: media_player.volume_up
      data_template:
        entity_id: "{{ where }}"
    - service: script.volume_op_1
      data_template:
        switch: "{{ switch }}"
        where: "{{ where }}"
1 Like

You are the best! Thanks for helping me out.
I could not make it work after copy/pasting it in after correcting my typo in milliseconds (no errors when checking the config), so i ended up writing everything in from scratch.

I ended up with this:

Automation:

- alias: Volume op
  initial_state: True
  trigger:
    platform: state
    entity_id: switch.stue_4_overst_hojre
    to: 'on'
  action:
    - service: media_player.volume_up
      entity_id: media_player.livingroom
    - delay:
        milliseconds: 250
    - service: script.volume_op
      data_template:
        where: media_player.livingroom
        switch: switch.stue_4_overst_hojre

Scripts:

volume_op:
  alias: "Volume op på højttaler"
  sequence:
    - condition: template
      value_template: "{{ is_state(switch, 'on') }}"
    - service: media_player.volume_up
      data_template:
        entity_id: "{{ where }}"
    - service: media_player.volume_up
      data_template:
        entity_id: "{{ where }}"
    - delay:
        milliseconds: 50
    - service: script.volume_op_2
      data_template:
        where: "{{ where }}"
        switch: "{{ switch }}"
      
volume_op_2:
  alias: "Volume op på højttaler 2"
  sequence:
    - condition: template
      value_template: "{{ is_state(switch, 'on') }}"
    - service: media_player.volume_up
      data_template:
        entity_id: "{{ where }}"
    - service: media_player.volume_up
      data_template:
        entity_id: "{{ where }}"
    - delay:
        milliseconds: 50
    - service: script.volume_op
      data_template:
        where: "{{ where }}"
        switch: "{{ switch }}"