Pass arguments to shell_command on event

Hi All,

(running on Home Assistant 0.60.0 on a Pi3)

I am trying, to no avail, to get command-line arguments passed to my shell script. Working with a consolidated configuration, I have:

automation:
  - id: Bulk Quad Detector
    alias: Bulk Quad Detector
    hide_entity: true
    trigger:
    - platform: event
      event_type: zwave.scene_activated
      event_data:
        entity_id: zwave.quad1
    action:
      service: shell_command.test_press
      data_template:
        scene_id:   "{{ trigger.event.data['scene_id']  }}"
        # scene_id:   "{{ trigger.event.data.scene_id  }}"
        entity_id:  "{{ trigger.event.data['entity_id']  }}"
        # entity_id:  "{{ trigger.event.data.entity_id  }}"
        scene_data: "{{ trigger.event.data['scene_data']  }}"
        # scene_data: "{{ trigger.event.data.scene_data  }}"

shell_command:
  test_press: 'echo sceneId: "{{ scene_id }}" >> /tmp/quad.log'
  # test_press: 'echo $(date) >> /tmp/quad.log'

If I only echo the date to that log it works fine - but when I try to include that scene_id nothing shows up in that log (the file isn’t even created), nor is there any indication that it failed in the home-assistant.log. I have tried a couple different ways of doing the data template, as above, and as trigger.event.data.entity_id, both with the same outcome.

Even just a hardcoded literal for the data template doesn’t work:

      data_template:
        scene_id:  "foo"

So I don’t think that it is the trigger... part (though no guarantees that is right, either)

Any tips? should this work?

Thanks,

-jamie

Don’t quote the shell command

snips_toggle_sound:  /home/homeassistant/.homeassistant/shell_command/snips_toggle_sound.sh {{ sound }}

I am not sure how it works with redirection though.

1 Like

I trigger an automation using an input_select for controlling an Amcrest camera. My configuration for the shell command is:

shell_command:
  ptz_amcrest: bash /home/homeassistant/.homeassistant/camera.move 19 {{states('input_select.move_camera')}}

Perhaps you need something like this:
shell_command:
  test_press: echo sceneId: {{ 'scene_id' }} >> /tmp/quad.log

When I try without any quotes in the shell_command I get an error saying mapping values are not allowed here

I replaced the echo & redirect with a script (which was the actual goal, anyway) and that worked. Something behaves differently when there is a template in the command.

this:

automation:
  - id: bulk_quad_detector
    alias: Bulk Quad Detector
    hide_entity: true
    trigger:
      platform: event
      event_type: zwave.scene_activated
      event_data:
	entity_id: zwave.quad1
    action:
      - service: shell_command.test_press_echo
        data_template:
          scene_id: "{{ trigger.event.data.scene_id }}"
      - service: shell_command.test_press_script
	data_template:
          scene_id: "{{ trigger.event.data.scene_id }}"
      - service: shell_command.test_press_date
        data_template:
          scene_id: "{{ trigger.event.data.scene_id }}"

shell_command:
  test_press_echo: 'echo test_press_echo sceneId: "{{ scene_id }}" >> /tmp/quad.log'
  test_press_script: '/home/homeassistant/homeassistant-config/grab.sh test_press_script sceneId: "{{ scene_id }}"'
  test_press_date: 'echo test_press_date $(date) >> /tmp/quad.log'

should spit out three lines in that file, but the first test_press_echo gets lost. it ends up with:

Wed 27 Dec 21:10:20 UTC 2017 test_press_script sceneId: 4
test_press_date Wed 27 Dec 21:10:20 UTC 2017

So I got it working - If someone else runs into this, don’t mix >> and {{ }}

Thanks!!

3 Likes

Hey @jamie,

Care to share your final code?
I’m struggling to retrieve the entity_id…
Here is my code:

Automation

- alias: Set Chromecast Radio Picture
  trigger:
    - platform: state
      entity_id: media_player.kitchen
      to: 'playing'
    - platform: state
      entity_id: media_player.bedroom
      to: 'playing'
    - platform: state
      entity_id: media_player.house
      to: 'playing'
    - platform: state
      entity_id: media_player.downstairs
      to: 'playing'
    - platform: state
      entity_id: media_player.onkyo
      to: 'playing'
  action:
  - service: shell_command.chromecast_radio_picture
    data_template:
      my_media_player: '{{ trigger.event.data.entity_id }}'

shell_command:

chromecast_radio_picture: '/home/cctv/.homeassistant/shell_scripts/ChromeCast_Radio.py "{{ my_media_player }}"'

never mind, I figured out I should use

    - platform: event
      event_type: state_changed

instead of

    - platform: state
      entity_id: media_player.kitchen
      to: 'playing'
    - platform: state
      entity_id: media_player.bedroom
      to: 'playing'
    - platform: state
      entity_id: media_player.house
      to: 'playing'
    - platform: state
      entity_id: media_player.downstairs
      to: 'playing'
    - platform: state
      entity_id: media_player.onkyo
      to: 'playing'