Creating a template switch from two scripts (solved, with questions)

SOLVED, but still with questions (see bottom.)

Hi!

New to HA and struggling with something I believe ought to be quite simple. I have a Broadlink RM4 and have successfully learned codes and set up scripts to turn on and off some IR remote controlled candles.

Here’s what the script looks like:

What would be more natural, of course, is to have a single script that runs the on script to turn on, and off script to turn off. I believe a template script is the right choice for this, and I think I’m close, but can’t quite get the syntax to reference the scripts correct.

switch:
  - platform: template
    switches:
      fireplace_candles_switch:
        turn_on:
          target:
            entity_id: script.1663570647553
          service: fireplace_candles_on
        turn_off:
          target:
            entity_id: script.1663570647553
          service: fireplace_candles_off
        friendly_name: "Fireplace Candles"

I can’t quite figure out how to structure the action to refer to the scripts I already have. I know the entity_id for the script, and the name of the script, but not sure how to set up the config.

I have also tried:

turn_off:
          service: script.fireplace_candles_off

but get

Failed to call service switch/turn_off. Unable to find service script.fireplace_candles_off

The entity id for the remote itself is remote.broadlink_remote.


UPDATE
Ok, I actually figured out a way to make this work but figured I would post anyway for anyone who is searching in the future. My full working conf is now:

switch:
  - platform: template
    switches:
      fireplace_candles_switch:
        turn_on:
          service: script.1663570647553
        turn_off:
          service: script.1663571141834
        friendly_name: "Fireplace Candles"

I see people referencing devices, entities and scripts in various ways inside switch templates, and none of them were using the auto-generated script IDs as mine does, so I think there are more elegant ways to do it, but this is one simple way to create a switch from two scripts.

  1. Go to Developer Tools > States
  2. In the Entities column, type script. where it indicates “Filter entities”
  3. It will list all scripts.
  4. The names you see represent how the script can be referenced (i.e. it’s the script’s entity_id)
  5. If the name only has a number then that’s how it’s referenced.
  6. So if you don’t see script.fireplace_candles_off in the list then it doesn’t exist with that name

You can have a single script that can support both functions. When the script is called, you pass it a variable (like on or off) to instruct it what to do.
Reference: Passing variables to scripts

1 Like

Thank you! I do see their friendly names there:

But if I try to refer to them by those friendly names, that doesn’t work:

        #   service: script.1663570647553
          service: script.fireplace_candles_on

Failed to call service switch/turn_on. Unable to find service script.fireplace_candles_on

Thanks for the tip about using a single script!

Which, like you said, are their friendly_name. They’re not their entity_id.

If you wish, a script’s entity_id can be renamed.

1 Like

Thanks again!

I renamed the entities, but somehow this did not enable them to be triggered by the switch in the same way that they were when they had their auto-assigned numerical entity ids.

Failed to call service switch/turn_on. Unable to find service script. fireplace_candles_on
switch:
  - platform: template
    switches:
      fireplace_candles_switch:
        unique_id: fireplace_candles
        turn_on:
        #   service: script.1663570647553
          service: script.fireplace_candles_on
        turn_off:
        #   service: script.1663571141834
          service: script.fireplace_candles_off
        friendly_name: "Fireplace Candles"

Do they appear as script.fireplace_candles_on and script.fireplace_candles_off in the Entity column of Developer Tools > States?

Yes, they do.

The error message shows a space between script. and fireplace_candles_on.

Unable to find service script. fireplace_candles_on
                              ^
                              |
                            space

Confirm your Template Switch’s configuration doesn’t have a space in script.fireplace_candles_on.

My apologies. That extra space is not there in reality. The flash error message was too quick for me to copy-paste so I screen shotted it and copied it from the screenshot. The actual error message has no extra space.

Here’s what I see if I go to Developer Tools → Services

image

  1. Go to Developer Tools > Services
  2. Select the Fireplace Candles On script
  3. Switch to YAML mode

It should display this:

service: script.fireplace_candles_on
data: {}

It doesn’t! It shows the old numeric id there:

image

So I guess when I changed the entity_id for the script, it somehow didn’t propagate that change through to the services?

Looks like the renaming operation didn’t work the way we want and the script’s entity_id is still its original value (containing a number).

I actually don’t see Fireplace Candles On or fireplace_candles_on in the Services, only fireplace_candles.on.

Looks like the only way to truly change the script’s entity_id is by modifying the script’s configuration in the scripts.yaml file. I wouldn’t bother doing that unless you really dislike using script.1663570647553.

I went back to addressing the scripts by their numeric ids, but that didn’t work either, got the error above.

You mentioning scripts.yaml was the key I needed! When I first looked at it, it looked like this:

'1663570647553':
  alias: fireplace_candles.on
  sequence:
  - service: remote.send_command
    data:
      command: 'on'
      device: Fireplace Candles
    target:
      entity_id: remote.broadlink_remote
'1663571141834':
  alias: fireplace_candles.off
  sequence:
  - service: remote.send_command
    data:
      device: Fireplace Candles
      command: 'off'
    target:
      entity_id: remote.broadlink_remote

I changed it to:

'fireplace_candles_on':
  alias: "Fireplace Candles On"
  sequence:
  - service: remote.send_command
    data:
      command: 'on'
      device: Fireplace Candles
    target:
      entity_id: remote.broadlink_remote
'fireplace_candles_off':
  alias: "Fireplace Candles Off"
  sequence:
  - service: remote.send_command
    data:
      device: Fireplace Candles
      command: 'off'
    target:
      entity_id: remote.broadlink_remote

Which didn’t work either, because in the UI Developer Tools > States, I could see that my old scripts fireplace_candles_off were still there and the new ones had their entity ids changed to fireplace_candles_off_2. So I needed to delete the old ones, rename the new ones to fireplace_candles_off without the _2, and then restart, and now it works perfectly!

Thanks so much for your help. Sent you :coffee: :bowing_man:

1 Like

One point of curiosity – my device is Fireplace Candles. I wish I had named it with underscores, and thought perhaps I would try to rename it, but that device doesn’t show up in Settings → Devices. Where does the conf for device Fireplace Candles live? Can’t seem to find it in UI or yaml.

I’m not very familiar with the Broadlink integration but its documentation appears to indicate that the device option refers to the name you had assigned to the physical gadget (not in Home Assistant but I assume using Broadlink’s software). In other words, the device option doesn’t refer to Home Assistant’s devices that are listed in Settings → Devices.

1 Like