Hi,
I’d like to add a custom bash script to hassio, which should execute once a week (at night every thursday).
The question is:
-
How do I create a cronjob trigger, without having to modify the os crontab? The “Time Pattern” Automation only allows to configure daily cronjobs.
-
How can I add a custom script to hassio? Do I have to login via ssh and put a file somewhere? Or could I just add it as a script somewhere in hassio, and call it with a time-based action?
My goal is to execute a bash script that performs a ssh login on my access points and sends a reboot command to them. (taken from: https://github.com/stevejenkins/unifi-linux-utils/blob/e7743f6a24990c59e79a2f83de01553f30b64122/uap_reboot.sh)
Use a Template Trigger… Something like this that I use for my coffee maker that turns on at different times on different days.
- id: '1507434167220'
alias: Coffee Maker On
trigger:
- platform: time
at: '07:00:00'
- platform: time
at: '07:15:00'
- platform: time
at: '08:00:00'
- platform: time
at: '12:00:00'
- platform: time
at: '19:00:00'
- platform: time
at: '20:00:00'
- entity_id: person.liz
from: 'not_home'
platform: state
to: 'home'
condition:
condition: and
conditions:
- condition: state
entity_id: 'input_boolean.homeandawayauto'
state: 'on'
- condition: or
conditions:
- condition: template
value_template: "{{ now().strftime('%H:%M') == '07:00' and now().weekday()
< 5 and (states('input_boolean.holiday') == 'off') }}"
So make the trigger the time and then use the condition to nail it down to the day
On ‘Hassio’ you might not be able to get access to ‘enough’ of the underlying system to set up your full bash script.
But you should be able to use the shell_command to issue just the command for each AP separately, without the script behind it, with a simple weekly scheduled automation, i.e. a version of:
sshpass -p $password ssh -oStrictHostKeyChecking=no -oUserKnownHostsFile=$known_hosts_file $username"@$i" reboot
Example:
I defined these shell commands in my configuration file:
# restart_igrill_on_rpi5
restart_igrill_on_rpi5: 'ssh [email protected] sudo systemctl restart igrill.service'
restart_bluetooth_on_rpi5: 'ssh [email protected] sudo invoke-rc.d bluetooth restart'
And I trigger this script:
script_restart_igrill_on_rpi5:
sequence:
- service: shell_command.restart_bluetooth_on_rpi5
- service: shell_command.restart_igrill_on_rpi5
from a button in my Lovelace UI - but it might as well be done through a timed automation.
If I’d want to add a more complex bash script, where would I put it? Or should I avoid adding custom scripts via ssh, as those might be deleted on hassio upgrades?
I succeeded at least to trigger the ssh reboot command now with:
shell_command:
restart_wlan: 'ssh -i /config/ssh/id_rsa -oStrictHostKeyChecking=no user@<ip> reboot'
Question: I have several access points. How can I define the ips in the automation script, and then pass each ip to the shell command one after the other?
With my - very limited - skills I would just define a one-liner for each AP and call them sequentially.
You might be able to set up your bash script in your hassio configuration folder and call it with a shell_command action but I wouldn’t know how to achieve that - not sure if there are any user access/execution right limitations or such.
If you find out how to do that it would be great to share it - I’ve been looking into this a little while ago, but didn’t have much time for it, though. But it’s one of the reasons I still have a Venv installation running for my main instance.
Found out:
automation.yaml:
data:
ip: 192.168.178.25
service: shell_command.restart_wifi
configuration.yaml:
shell_command:
restart_wifi: 'ssh -i /config/ssh/id_rsa -oStrictHostKeyChecking=no root@{{ip}} reboot'
2 Likes