Shell command in script condition

Hi guys,

I’m wondering if it is possible to have a condition in the script component that executes a command.
For example, I’ve got this:

script:
  alias: "Update config from Github"
  sequence:
    - shell_command.pullconfig
    - homeassistant.restart

What this does, is get my latest config from Github, and restart the HA instance.
This works, however, I can trigger it even when there are no changes on GitHub (compared to my local repo). I’ve got a sensor set up that polls Github and checks for the number of commits behind, the problem is it polls every minute now, which is quite a bit overkill, since I only sometimes make a change.
I can drive the interval up, but then new changes won’t get picked up directly, which is not wanted either.

I’m wondering if there’s a way to use a condition block in the script, which will execute a command?
For example, something like this:

script:
  alias: "Update config from Github"
  sequence:
    - condition:
      command: 'check-commits.sh'
    - shell_command.pullconfig
    - homeassistant.restart

Or does the script automatically stop executing when a shell_command send an error exit code?
In that case I can just make it in a shell_command and use that…

You could use a normal condition like stated here: https://home-assistant.io/getting-started/scripts/

And set it up like this:

  1. Call a shell_command that checks for commits and writes to a sensor inside of HA using the REST API
  2. Check the state of the sensor
  3. Do the rest

Your sensor could just be a dummy sensor which only ever gets written by the REST API. I use one for my alexa responses like this:

alexa_response:
  value_template: >-
    {{ states("sensor.alexa_response") }}

If you need further help with the REST API and how to connect via bash I might be able to help :slight_smile:

~Cheers

1 Like

Completely OT but nice to see you back @PhyberApex!

1 Like

Awesome thanks,
I thought about using the REST API, but since I haven’t used it before I was wondering if it was possible to do without.

I’m going to try it later tonight, but watching the steps you mentioned, it’s easier than I expected. :stuck_out_tongue:

Had some stuff to do IRL for a few months. Congrats to your mod status btw!

BAAACK to topic!

Bits that might help with the bash HA REST API thingy be sure to send your password as header in the POST request.

"x-ha-access":"PASSWORD"

~Cheers

1 Like

Thanks man, I’ve gotten it to work, the only thing left to do right now is figure out how I can feed data into that curl script (since my config is on Github, I’d like my password to come from the secrets file)

I would suggest giving it as a parameter into the perl script and using the Ha secrets file.

shell_command:
  thing: perl script {{ secret }}

git it set up like this right now:

shell_command:
  set_new_commits: 'bash set_commits.sh {{ secret http_password }}'

But it seems to complain about now finding the secret, I’ve tried with !secret, no brackets, without secret, but I can’t seem to get it to work, I’ve found here: Using secrets.yaml with shell commands? that it can’t be done, is this still the case?

1 Like

Geez my bad I totally confused this one with another tool I’m using which uses secrets in another way in yaml files. Seems like it does not work atm but it should be integrated if you ask me. So best thing to do would be to read it from a file in perl then. I am not familiar with perl tho :slight_smile:

~Cheers

Yea, me neither :slight_smile:
Anyways, I’ll hardcode it for now, just got to remember not to publish it on GitHub :smiley:

Thanks for all the help and the procedure on how to do this with the REST API :slight_smile:

1 Like