How to combine 2 scripts to make 1 button

I currently am running 2 different scripts one for tv on and one for tv off.

Here’s what I’m using…

turn_on_tv:
  alias: turn on tv
  sequence:
    - service: media_player.turn_on
      data:
        entity_id: media_player.shield

    - service: light.turn_on
      data:
        entity_id: light.tv_stand_2

    - service: media_player.turn_on
      data:
        entity_id: media_player.vizio_smartcast

    - delay: "00:00:03"

    - service: media_player.select_source
      target:
        entity_id: media_player.vizio_smartcast
      data:
        source: HDMI-3

    - service: switch.turn_on
      data:
        entity_id: switch.living_room_ambient_tv
    - delay: "00:00:15"

That turns everything on…

Here’s the one for off:

turn_off_tv:
  alias: turn off tv
  sequence:
    - service: light.turn_off
      data:
        entity_id: light.tv_stand_2

    - service: switch.turn_off
      data:
        entity_id: switch.living_room_ambient_tv

    - service: media_player.turn_off
      data:
        entity_id: media_player.vizio_smartcast

    - service: media_player.turn_off
      data:
        entity_id: media_player.shield

Is it possible to combine the two into one? Currently I have two different dashboard buttons ‘on’ and ‘off’ I’d just like to combine somehow and just have one dashboard button.

OR perhaps change my button card instead…

- <<: *cb_standard_button_narrow
          name: TV on
          icon:  mdi:television-classic
          tap_action:
            action: call-service
            service: script.turn_on_tv

and

- <<: *cb_standard_button_narrow
          name: TV off
          icon:  mdi:television-classic
          tap_action:
            action: call-service
            service: script.turn_off_tv 

Thanks all!

Yes.

A script can accept a variable passed to it from your button cards. For example, the buttons can pass a value of on or off.

Passing a variables to scripts

The script can use a choose statement to determine what it should do based on whether it was passed an on or off value.

Choose a group of actions

Examples can be found in the supplied links.

1 Like

Seems like a prefect case for a template switch.

1 Like

Here’s my template switch only it’s telling me that failed to call service switch/tv Service not found… or am I looking for the wrong thing here? switch.tv

tv.yaml

- platform: template
  switches:
    tv:
      friendly_name: "Entertain On/Off"
      value_template: "{{ is_state('media_player.turn_on', 'on') }}"
      turn_on:
        - service: media_player.turn_on
          target:
            entity_id: media_player.shield
        - service: light.turn_on
          target:
            entity_id: light.tv_stand_2
        - service: media_player.turn_on
          target:
            entity_id: media_player.vizio_smartcast
        - delay: "00:00:03"
        - service: media_player.select_source
          data:
            entity_id: media_player.vizio_smartcast
            data:
              source: HDMI-3
        - service: switch.turn_on
          target:
            entity_id: switch.living_room_ambient_tv
        - delay: "00:00:15"
        
      turn_off:
        - service: light.turn_off
          target:
            entity_id: light.tv_stand_2
        - service: switch.turn_off
          target:
            entity_id: switch.living_room_ambient_tv
        - service: media_player.turn_off
          target:
            entity_id: media_player.vizio_smartcast
        - service: media_player.turn_off
          target:
            entity_id: media_player.shield

OK it’s failing right here →

- service: media_player.select_source
          data:
            entity_id: media_player.vizio_smartcast
            data:
              source: HDMI-3

How can I fix that? Cannot call data it says.
Thanks!

Tried this but getting an error:
Figured I’d ‘check’ something to see if it was already running but …

- platform: template
  switches:
    tv:
      tap_action:
      action: call-service      
      data_template:
        entity_id: |
          {% if is_state('media_player.vizio_smartcast', 'off') %}
            script.tv_on
          {% else %} 
            script.tv_off
          {% endif %} 
Invalid config for [switch.template]: [tap_action] is an invalid option for [switch.template]. Check: switch.template->switches->tv->tap_action. (See ?, line ?).

Suggestions? Thank you!

You can’t combine lovelace/dashboard configuration with template switches…

1 Like

seeing that you’re talking about the documentation in another thread, please just take the time to look at template switches documentation:

In that image, do you see the word tap_action, action, or data_template in the bold field names?

Compare your configuration to the words in bold. Do they match?

1 Like

Yes they match

No they dont!

The documents say the following fields are valid:

friendly_name, unique_id, value_template, availability_template, turn_on, turn_off, and icon_template.

You have

tap_action, action, and data_template.

Sorry thought you were referring to the alignment since the red line was there… :wink:
That’s okay… I’ll work on it.

Thank you for the input. The time I have to work on HA is this time of the day generally before work. I give it about 3 hours a day… and now it’s time for work :slight_smile: Wish I had unlimited hours to do work on this… but… I’ve grown accustomed to food and shelter so off to work I go. Have a great day.

Thanks again.

@petro

Come on - you could just told him the easiest solution. We must not forget that not everybody know what we do :slight_smile:

Next thing would be using variables to use a single script.

  - platform: template
    switches:
      entertainment:
        friendly_name: "Entertain On/Off"
        value_template: "{{ is_state('media_player.vizio_smartcast', 'on') }}"
        turn_on:
          - service: script.tv_on
        turn_off:
          - service: script.tv_off

1 Like

You aren’t in the other thread where he’s talking about the documentation being outdated. I’m trying to get him to read the documentation and understand it. So he understands that the documentation is not outdated, he just doesn’t understand it (Not his fault).

Well, both of us know that there is a lot of work ahead to update the documentation. HA get filled with lot of new stuff every month and lots of Breaking Changes also. Developers aren’t the best to write documentation as they fall in the typical mistake believing others are as skilled as they are.

In General, most documentation should be written having in mind “everybody are a beginner”.

I know him quite well and I know the lack of updates to documentation has frustrated him (and also me earlier). In this case, he mixed scripts and the switch template in the stress of not getting things to work - this has at some point happen to us all, right? Sometimes the mistake are right in front of your eyes but you don’t catch it. Telling someone to “read the documentation” doesn’t necessarily make people learn, assisting with the code does.

This is how you’ve helped me earlier :slight_smile:

Thanks for all help provided throughout the years @petro you are a very skilled person :+1:

1 Like

The point I was trying to make was to at least attempt to start learning the documentation, which is why I didn’t give him the answer outright. I’ve helped him many times in the past with outright answers. At some point he should attempt to learn the docs. I simply chose this as a starting point. You have to start somewhere.

Doing what I can… I do read it and try it then I post the example and things like ‘use this not this’…
That’s where the documentation and my ‘it’s outdated’ comes from…
An example of that would be [not exact] but I’ll use something like ‘data:’ from the example and post my results then someone will say use ‘data_template’ now… this is what I’ve been running into… so it doesn’t match what the docs say… yes it may be minor to someone who knows… but to someone who doesn’t it’s an issue and I’m grateful to the person that pointed out my mistake without making me feel like an idiot.

I am grateful for the help from @stigh and everyone else that takes a minute to clarify things and/or help. I’ve had people show me how to make automations smaller… I learned JS and PHP on my own and yaml will take me a while because it’s just not coming to me for some reason… but then I do have a lot going on outside of this…

So again if I appreciate the help and trust me if I’m asking it’s because I’m trying to do this…

And that person who told you to use data_template is wrong. data is what you should be using data_template is outdated. If you see that in the docs, suggest an edit to the page to change it from data_template to data.

It’s just an example… made up of course… I was actually looking for the post where someone pointed something out to me … but didn’t find it. I have one foot out the door as I am late.

Thank you for the help!

I understand what you were saying and as always I Thank you for the help you’ve offered!