Motion triggered script

I worked out a script to get still images from my Amcrest security camera in my garage and tied it to a motion sensor:

  - alias: Garage Motion
    trigger:
      platform: state
      entity_id: binary_sensor.aeotec_zw100_multisensor_6_sensor_26_0
      to: 'on'
    action:
      - service: script.garagewget

However, it is not getting the images as it should. I can run the script manually and it works perfectly. I have another script that is used to backup my configuration directory. I can tie the motion sensor to that and it works every time motion is detected. So I am not sure what else to look for.

I have HA installed on a server (not using the all in one installer) and everything else is working as it should. I see no error messages in HA or anything in the logs on the camera itself. The camera is connected with WiFi, so I did expect it to be a little slower than it would be if over Ethernet, but I can remotely log into the camera without issues.

I can see the two scripts I have set up in the entities list. I can go into HA services and I can manually run the backup script from HA and it works perfectly, but when I try to run the script to get the camera images manually, nothing happens. Both scripts have the same permissions as well.

If anyone has any ideas, I would greatly appreciate it.

Well if you have seen both the trigger and action have worked before, then its probably just a syntax error or typo somewhere i would think, do you get any error messages?

Although, it looks good to meā€¦

No, I get no errors. I updated my original post with some additional details.

Here is the script I am using:

#!/bin/bash

x=1
while [ $x -le 3 ]
do
  wget -P /path/to/store/files/ http://admin:[email protected]/cgi-bin/snapshot.cgi_$(date +"%m%d%Y-%H%M%S")
  x=$(( $x + 1 ))
  sleep 5
done

Should be:

- alias: Garage Motion
    trigger:
      platform: state
      entity_id: binary_sensor.aeotec_zw100_multisensor_6_sensor_26_0
      to: 'on'
    action:
      service: script.turn_on
      entity_id: script.garagewget

Ahhhh, okay, thank you for that @anon43302295. Let me make that change and see how it goe.

1 Like

I made the changes:

  - alias: Garage Motion
      trigger:
        platform: state
        entity_id: binary_sensor.aeotec_zw100_multisensor_6_sensor_26_0
        to: 'on'
      action:
        service: script.turn_on
        entity_id: script.garagewget

Reloaded the automation but still no joy. I am restarting HA just in case for good measure and will retry. I did not see any errors when reloading automations, not did any show up when triggering the motion sensor.

I had to change the yaml to:

  - alias: Garage Motion
    trigger:
      platform: state
      entity_id: binary_sensor.aeotec_zw100_multisensor_6_sensor_26_0
      to: 'on'
    action:
      service: script.turn_on
      entity_id: script.garagewget

When I restarted HA it did not like the formatting and was complaining about the mapping of values. I will have to keep working with it once I get home from work. Itā€™s got to be something silly or simple.

On the HA components page your script is definitely called script.garagewget ?

The syntax is definitely correct, hereā€™s one of my automations that runs a scriptā€¦

Link removed

Normally when it complains about mapping values for me itā€™s because I have an extra space somewhere, or Iā€™ve put a dash somewhere where it shouldnā€™t be - hope that helps.

Yes sir, I have, in fact, just to be sure, copied it from the components/entities page as it showed up there, verified the script file was the same name, etc.

I will double check everything, as I am certain you are dead on and its something really simple. Thanks for the example too, very much appreciated.

no worries, Iā€™ve just realised the spacing was out in my first example, probably because I used the forum quote and then editted back, but yeah, the syntax is right so itā€™s going to be a rogue space or something daft.

Oh, just to confirm, are you putting this straight in to configuration.yaml ? or are you using an include? If an include_dir_xxx make sure itā€™s the correct one, so in my case I use include_dir_list and then the leading hyphen is taken away from the files in that directory.

My github should be self-explanatory on the latter point, but if you need anything else let me know.

Shouldnā€™t a bash script called with
service: shell_command.name_of_cmd
and the shell_command callā€™s the bash script?

Just for future reference the way OP has it does work btw you dont have to use service: script.turn_on

So @StormStrikes you are trying to call an external bash script?

In that case @VDRainer is correct, you should make a shell command that will call the script, then use service: shell_command.name_of_cmd

Make sure the home assistant user has the required permissions though.

Sorry for the delay, still at work.

Yes, I am trying to call an external bash script. Well it resides on the same server that HA does, but yeah, thatā€™s it.

What I am curious about though is the way I have the configuration now, as shown in my first post, I was able to call another script that backs up my configuration directory and it worked perfectly. I did not use the service: shell_command option, I just changed script.habackup to script.garagewget.

However, I will try the service: shell_command when I get home. I think I tried that at some point last night and could not get it to work either, but who knows what I may have goofed up.

This is where I tend to harp on the documentation. There is a script: component and a shell_command: component. HA appears to consider a ā€˜scriptā€™ a sequence of pre-programmed events that occurs in response to some trigger. Whereas in Linux a script is generally what I posted earlier, the #!/bin/bash code. But a shell command would be something like ifconfig, where you issue a command and get a response.

The documentation shows a shell command as:

shell_command:
  set_ac_to_slider: 'irsend SEND_ONCE DELONGHI AC_{{ states.input_slider.ac_temperature.state }}_AUTO'

Thatā€™s not the kind of shell command I am trying to run. I am trying to run an actual script file:

#!/bin/bash

x=1
while [ $x -le 3 ]
do
  wget -P /path/to/store/files/ http://admin:[email protected]/cgi-bin/snapshot.cgi_$(date +"%m%d%Y-%H%M%S")
  x=$(( $x + 1 ))
  sleep 5
done

I guess it can be subjective or even relative in which you call a script and which you call a shell command. Ultimately itā€™s neither here nor there, but it does make for a confusing setup. Because under HAā€™s documentation for ā€˜shell commandā€™, how would I run the code I want to run. How do I reference my script file which is on the hard drive?

Do I, instead, write shell_command: script.garagewget? The shell command component appears to be looking for something that has already been defined and named WITHIN HA, not some bash file on the hard drive.

When I first tested this, I tried it with my backup script file that I have:

  • alias: Garage Motion
    trigger:
    platform: state
    entity_id: binary_sensor.aeotec_zw100_multisensor_6_sensor_26_0
    to: ā€˜onā€™
    action:
    - service: script.habakup

When I triggered the motion sensor, it ran the backup routine. The habakup.sh file is in the very same location that the garagewget.sh file is and has the exact same permissions.

So my next step was to switch it over:

  • alias: Garage Motion
    trigger:
    platform: state
    entity_id: binary_sensor.aeotec_zw100_multisensor_6_sensor_26_0
    to: ā€˜onā€™
    action:
    - service: script.garagewget

That did not work and it did not run the code. So if one requires shell_command: why would not the other? Both show up in the entities list:

The documentation just seems to make this more confusing than it needs to be. Why it works with one and not the other is beyond me and I am just not getting any clues with respect to error messages, etc. to give me any clues about where to look or what may be failing.

I just tried this:

  - alias: Garage Motion
    trigger:
      platform: state
      entity_id: binary_sensor.aeotec_zw100_multisensor_6_sensor_26_0
      to: 'on'
    action:
      - service: script.turn_on
        entity_id: script.garagewget

That did not work either. Below is how I have my configuration.yaml set up:

shell_command:
  habakup: '/my/path/to/habakup.sh'
  garagemotion: '/my/path/to/garagewget.sh'

And then:

script:
  habakup:
    alias: Backup Home Assistant
    sequence:
      - service: shell_command.habakup
  garagewget:
    alias: Garage Motion
    sequence:
      - service: shell_command.garagewget

Again, I can trigger the habakup file with the motion sensor, but not the garagewget file.

Your calling service: shell_command.garagewget but your shell_command is named garagemotion.
By the way, you can call the shell_command directly from your automation.

  - alias: Garage Motion
    trigger:
      platform: state
      entity_id: binary_sensor.aeotec_zw100_multisensor_6_sensor_26_0
      to: 'on'
    action:
      service: shell_command.garagemotion

@VDRainer, I canā€™t thank you enough. I did not think I would ever get that working as I had hoped. But I changed it to:

  - alias: Garage Motion
    trigger:
      platform: state
      entity_id: binary_sensor.aeotec_zw100_multisensor_6_sensor_26_0
      to: 'on'
    action:
      service: shell_command.garagemotion

as you suggested and it worked like a champ. Reading the documentation, I donā€™t think I would have ever put that together. Thank you so much!