Create a toggle switch

Hello,

I’m trying to make a toggle switch for my chromecast radio player, but i can’t get it working.
My goal is to toggle the radio player with one button press.

I’ve got to scripts to play and stop the radio
Play: script.play_chromecast_radio
Stop: script.stop_chromecast_radio

What is the right way to create such a toggle switch with this two entity’s?

Thanks!

1 Like

There may be a better way, but the only way that comes to mind is to create an input_boolean (on/off) and two automations (one for on/one for off).

So something like this…

input_boolean:
  chromecast_radio:
    name: Chromecast Radio
automation:
  - alias: 'Chromecast Radio On'
    trigger:
      platform: state
      entity_id: input_boolean.chromecast_radio
        to: 'on'
    action:
      service: script.play_chromecast_radio

  - alias: 'Chromecast Radio Off'
    trigger:
      platform: state
      entity_id: input_boolean.chromecast_radio
        to: 'off'
    action:
      service: script.stop_chromecast_radio

…then create a button to toggle the new input_boolean

entities:
  - type: call-service
    name: Chromecast Radio
    service: input_boolean.toggle
    service_data:
      entity_id: chromecast_radio

EDIT: Corrected omission of state: (thanks DavidFW1960).

Think you left out state:
Could also do in one automation with conditions.

1 Like

Oops! Good catch – correcting.

The automations are not even necessary. You can have a template switch that uses those scripts directly:

input_boolean:
  chromecast_radio:
    name: Chromecast Radio

switch:
  platform: template
  switches:
    chromecast_radio:
      value_template: >
        {{ is_state('input_boolean.chromecast_radio', 'on') }}
      turn_on:
        - service: input_boolean.turn_on
          entity_id: input_boolean.chromecast_radio
        - service: script.play_chromecast_radio
      turn_off:
        - service: input_boolean.turn_off
          entity_id: input_boolean.chromecast_radio
        - service: script.stop_chromecast_radio

Then just reference switch.chromecast_radio in your front end.

3 Likes

Alternately, you can use one simple automation if you rename the scripts to:

script.chromecast_radio_on
script.chromecast_radio_off

The following automation will call the correct script based on the state of input_boolean.chromecast_radio.

  - alias: 'Chromecast Radio On/Off'
    trigger:
      platform: state
      entity_id: input_boolean.chromecast_radio
    action:
      service_template: script.chromecast_radio_{{trigger.to_state.state}}
12 Likes

Oh this is brilliant! I’m implementing this everywhere. :star_struck:

2 Likes

This works perfect, thanks!!

When i change the script name all goes to hell, thanks anyway!
Choose the easyest way from SteveDinn.

Thanks for the quick reply’s, it seems so easy to you guys haha

If you wish, I can help you make it return from hell.

The solution I offered is a single, super-simple automation that has been tested and confirmed to be functional. All that’s required is:

  1. Your existing input_boolean.chromecast_radio
  2. Renaming your existing two scripts:
    script.chromecast_radio_on
    script.chromecast_radio_off
  3. Adding the automation (shown in my previous post) to wherever you currently store all your automations

Restart Home Assistant to make all changes take effect. Done.

The input_boolean now controls turning the radio on/off (via the scripts).

It’s your choice of course. However, that solution requires the creation of a template switch that effectively duplicates the existing input_boolean. You don’t need two toggles to do the job, just one.

Going to try it soon, got this time of the year very less time to play with HA.

What you tell is the perfect way to do this.
The less code the better!

Need Help Please…

Im using 1 button of external rf remote to control light ON and OFF. i can switch ON and OFF via the home assistant frontend and on the external rf remote. my issue is that i need the home assistant to show the state when i use the external rf button. Using External rf remote; when i press ON, the light ON, state on frontend ON. When I press OFF, the light OFF but home assistant state no change.
here is my binary_sensor config:

  • platform: mqtt
    state_topic: “tele/rfbridge2/RESULT”
    value_template: ‘{{value_json.RfReceived.Data}}’
    payload_on: ‘064283’
    payload_off: ‘064283off’
    name: “Button_A”

alias: “Button_A”

off_delay: 1

script:
button_light_on:
alias: Button Light 4 ON
sequence:

  • service: light.turn_on
    data:
    entity_id: light.rm4_ceiling_light
    mode: single
    icon: ‘icons: mdi- bulb’

button_light_off:
alias: Button Light 4 OFF
sequence:

  • service: light.turn_off
    data:
    entity_id: light.rm4_ceiling_light
    mode: single
    icon: ‘icons: mdi- bulb’

input_boolean:
button_light:
name: Light Button

automation:

  • alias: Light 4 ON
    trigger:
    platform: state
    entity_id: input_boolean.button_light
    to: ‘on’
    action:
    service: script.button_light_on
    id: d2adcbb9f9104852bde6e6f4b55c4d6d

  • alias: Light 4 OFF
    trigger:
    platform: state
    entity_id: input_boolean.button_light
    to: ‘off’
    action:
    service: script.button_light_off
    id: c4dd7c5b6ae44db5a1bd3302b8af46dd

Thank you in advance for the help.

wat the hack!
. no repond

service_template: script.chromecast_radio_{{trigger.to_state.state}}

This is brilliant but service_template is now deprecated. Is there still a clean way to do this?