Attempt to run a shell_command from a on/off switch not working

I am trying to make a simple on/off switch run two different shell commands. Everything I have read says this should work, but it doesn’t. No errors at all, just nothing happens whenever I flick the switch.

I had this working before but I was using HABridge to accomplish it. I have now switched to Emulated_Hue (which works great, btw) and am having trouble recreating it within HA. The script itself is pretty simple and simply turns on an Onkyo receiver, sets the volume level, then ssh’s into Volumio and starts playing an online radio station. Here is what I have done so far and these are in my configuration.yaml:

shell_command:
  script_livingroom_onkyo_start: curl telnet://192.168.254.15:1239
  script_livingroom_onkyo_stop: curl telnet://192.168.254.15:1240

# Boolean Presence
input_boolean:
  onkyo_volumio_switch:
    name: Onkyo Volumio Switch
    initial: off

switch:
  - platform: template
    switches:
     lights_switch:
#       value_template: "{{ is_state('input_boolean.onkyo_volumio_switch', 'on') }}"
       turn_on:
         service: shell_command.script_livingroom_onkyo_start
         entity_id: script.livingroom_onkyo_start
       turn_off:
         service: shell_command.script_livingroom_onkyo_stop
         entity_id: script.livingroom_onkyo_stop

The curl commands work fine from a command line and I have several of these already working. The actual script runs on a different host, 192.168.254.15. I just need the switch to trigger these curl commands and I cannot seem to accomplish this with the code above. My goal is to regain voice control via alexa to turn on/off my Onkyo with Volumio. Once the switch works, I am confident I can easily get this into emulated_hue without issues.

What am I missing?

Okay, I simplified the code a bit and while the on action now works, the off does not.

switch:
  - platform: template
    switches:
      onkyo_volumio:
        friendly_name: "Onkyo Volumio Switch" 
        value_template: "{{ is_state('switch.onkyo_volumio', 'on') }}"
        turn_on:
          service: shell_command.script_livingroom_onkyo_start
          entity_id: script.livingroom_onkyo_start 
        turn_off:
          service: shell_command.script.livingroom_onkyo_stop
          entity_id: script.livingroom_onkyo_stop

Still missing something here. Just cannot figure out what it could be. I get this error now when checking the config:

Invalid config for 'template' from integration 'switch' at configuration.yaml, line 175: Service shell_command.script.livingroom_onkyo_stop does not match format <domain>.<name> for dictionary value 'switches->onkyo_volumio->turn_off->0->service', got None

This is the OFF portion. If this is wrong, why does the ON portion work with essentially the same code?

you’ve labeled this an entity_id. are you trying to invoke that script? if so, it should be specified as a service. i’m not a guru at this part of ha, so there may be other things wrong, but this part certainly seems wrong.

this may get you closer… at least until a guruin this area steps in and schools us both…

switch:
  - platform: template
    switches:
      onkyo_volumio:
        friendly_name: "Onkyo Volumio Switch" 
        value_template: "{{ is_state('switch.onkyo_volumio', 'on') }}"
        turn_on:
          - service: shell_command.script_livingroom_onkyo_start
          - service: script.livingroom_onkyo_start 
        turn_off:
          - service: shell_command.script_livingroom_onkyo_stop
          - service: script.livingroom_onkyo_stop

EDIT: fixed . to _

Ah. First I didn’t realize the service: could be used twice. I did try this. Now both on and off fail with these log errors:

2024-06-02 22:52:39.502 INFO (MainThread) [homeassistant.helpers.script.onkyo_volumio_switch] Onkyo Volumio Switch: Running template script
2024-06-02 22:52:39.503 INFO (MainThread) [homeassistant.helpers.script.onkyo_volumio_switch] Onkyo Volumio Switch: Executing step call service
2024-06-02 22:52:39.504 ERROR (MainThread) [homeassistant.helpers.script.onkyo_volumio_switch] Onkyo Volumio Switch: Error executing script. Service not found for call_service at pos 1: Service script.livingroom_onkyo_stop not found

Also, I thought using the entity_id called the entity, but from what you said, it would seem it set it. Good to know. I need to rethink this.

I only added the entity_id because I thought it would fix all the un-deleteable “switches” that have no unique IDs and are seemingly impossible to manage or delete. Crap! I may need to restore a backup to get rid of all these crap entries.

I don’t think script.livingroom_onkyo_stop actually exists but shell_command.script.livingroom_onkyo_stop does. I’ve worked on this for about 9 hours today and am making stupid mistakes now. Time to call it an evening.

that error indeed means that this:

script.livingroom_onkyo_stop

doesn’t exist.

also i just noticed you had shell_command.script.livingroom_onkyo_stop
which i copied. that’s almost certainly incorrect. did you mean shell_command.script_livingroom_onkyo_stop
?

right now it sounds like you’re guessing a lot. which will unlikely get you to a happy state…

i think you’d benefit from giving this:

a really good read… there’s actually a section specifically about multiple actions and how to do them on this link in that doc: Template Switch - Home Assistant

I think you just found the issue! The turn off section has two dots instead of an underscore and I overlooked it for hours!! My old boss would always say, sometimes it takes two sets of eyes to find a typo, and he was right.

It works! Solved. Thank you so much! I will also read the link you sent…tomorrow.

Now to make it work via emulated_hue for voice control. Should be easy (knock on wood).

EDIT: I also removed the entity_id lines. I’m sure this was also problematic.

glad it’s working! :slight_smile:

1 Like