Use input boolean as condition for service

Please help me with some syntax. I want to insert conditions around some entity ids in the script below. For each media player (office, study, bedroom) I have an input Boolean (on / off). I would like to use the input_booleans to include/uninclude the media_player lines in the entity id entry below. I don’t know how to proceed:

 - service: media_player.sonos_unjoin
   data_template:
     entity_id: 
       - media_player.office
       - media_player.study
       - media_player.bedroom

I am just getting started, sorry for such a basic question. Thanks for any help!

Here’s an example of an input_boolean used in a condition.

Sorry to say, but this will be difficult purely because you are dealing with a list. Templating is possible with a list but you’d need to template each line due to the limitations of Jinja (Template code language). The problem with this is that your list is unique. You want this input boolean to include or not include media players. This could put you in a catch 22. I’m not sure you can provide a list in yaml without an item in it. For example, if have office and bedroom on, your list would be ['office', '', 'bedroom]. This could cause problems. If you want to try it, here’s the service w/ Jinja:

 - service: media_player.sonos_unjoin
   data_template:
     entity_id: 
       - "{{ 'media_player.office' if is_state('input_boolean.media_player_office','on') else '' }}"
       - "{{ 'media_player.study' if is_state('input_boolean.media_player_study','on') else '' }}"
       - "{{ 'media_player.bedroom' if is_state('input_boolean.media_player_bedroom','on') else '' }}"

I do not know what will happen if you send a list with various items empty. It could cause an error in the log. It could also be ignored and do nothing.

If you are getting errors with the above template during execution, you need to move to a different model. At that point you should probably go with a python script because basic templates and conditions will not get you where you want to be.

Again, this is all because you need to output a list for entity_id. If it was a single value and not a list, it wouldn’t be this complicated. The limitations come from Jinja, which is what templates are built off of. Jinja only outputs single strings. It’s impossible to take a for loop in jinja and turn it into a list meant for yaml.

It’s more complicated but couldn’t you do it something like this:

- service: media_player.sonos_unjoin
   data_template:
     entity_id: >
       {%- if is_state('input_boolean.office', 'on' -%}
         media_player.office
       {%- elif is_state('input_boolean.study', 'on' -%}
         media_player.study
       {%- elif is_state('input_boolean.bedroom', 'on' -%}
         media_player.bedroom
       {%- endif -%}
- service: media_player.sonos_unjoin
   data_template:
     entity_id: >
       {%- if is_state('input_boolean.study', 'on' -%}
         media_player.study
       {%- elif is_state('input_boolean.bedroom', 'on' -%}
         media_player.bedroom
       {%- elif is_state('input_boolean.office', 'on' -%}
         media_player.office
       {%- endif -%}
- service: media_player.sonos_unjoin
   data_template:
     entity_id: >
       {%- if is_state('input_boolean.bedroom', 'on' -%}
         media_player.bedroom
       {%- elif is_state('input_boolean.office', 'on' -%}
         media_player.office
       {%- elif is_state('input_boolean.study', 'on' -%}
         media_player.study
       {%- endif -%}

That way you catch all three conditions of the booleans.

You’d get an error if all 3 input_booleans were off, you’d also get 3 errors instead of 1. Also, you’d get some crazyness when only 1 is on because the service would unjoin the same device 3 times.

Well, using your method combined with another method you could do this. This would be error free and unjoin only if the input_boolean was on.

 - service: script.media_player_sonos_unjoin
   data:
     boolean: input_boolean.media_player_office
     player: media_player.office
 - service: script.media_player_sonos_unjoin
   data:
     boolean: input_boolean.media_player_study
     player: media_player.study
 - service: script.media_player_sonos_unjoin
   data:
     boolean: input_boolean.media_player_bedroom
     player: media_player.bedroom
script:
  media_player_sonos_unjoin:
    sequence:
      - condition: template
        value_template: "{{ is_state(boolean, 'on'}}"
      - service: media_player.sonos_unjoin
        data_template:
          entity_id: "{{ player }}"

I really like this approach, though I haven’t been able to get it to work.

[condition] is an invalid option for [script].

Any further thoughts?

I think you must be missing the line sequence and possibly the name? Can you post the yaml you are using?

  media_player_sonos_unjoin:
    sequence:
      - condition: template
        value_template: "{{ is_state(boolean, 'on'}}"
      - service: media_player.sonos_unjoin
        data_template:
          entity_id: "{{ player }}"

I had to add the “service:” to line 6, but otherwise based on your example. I couldn’t get to be acceptable to Home Assistant until I added “Service:” so I took a shot.

And that’s under the script section in configuration.yaml?

Correct.

I haven’t yet tried the second portion of your suggestion where the script is called. I added the code above to my script.yaml, and checked the config and got the error as I have shown. :frowning:

BTW I really appreciate all the help!

I don’t see why it’s not working for you. Conditions are available for scripts. When does that error occur?

During Configuration validation. I click on CHECK CONFIG button and I get this message:

Invalid config for [script]: [condition] is an invalid option for [script]. Check: script->script->media_player_sonos_unjoin->sequence->0->condition.

Hmm, that’s odd, you have 2 script s in that error.

Good observation. I am not yet familiar enough with HA error messages to notice these sort of things.

However, I can find no reason why. I have checked my YAMLs for an errand script: statement, but I don’t see anything.