Run a script... shell command?

I want to run a bash script that deletes email from gmail.

# Clear Out Gmail Alarm Notifications
shell_command:
  test_shell: /config/delete-gmail-email.sh

But it won’t run.

The delete-gmail-email.sh script runs fine from the ssh addon (I set the addon to install the expects package)

What I really want to do is have a lovelace entity button that can execute this script at will…

The shell command just immediately spews an error when I try and call the service:

Wed Feb 20 2019 16:16:07 GMT+1100 (Australian Eastern Daylight Time)
Error handling message: {'type': 'call_service', 'domain': 'shell_command', 'service': 'test_shell', 'service_data': {}, 'id': 16}
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/websocket_api/decorators.py", line 17, in _handle_async_response
    await func(hass, connection, msg)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/websocket_api/commands.py", line 147, in handle_call_service
    connection.context(msg))
  File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1130, in async_call
    self._execute_service(handler, service_call))
  File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1152, in _execute_service
    await handler.func(service_call)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/shell_command/__init__.py", line 89, in async_service_handler
    cmd, process.returncode)
  File "/usr/local/lib/python3.7/logging/__init__.py", line 1418, in exception
    self.error(msg, *args, exc_info=exc_info, **kwargs)
  File "/usr/local/lib/python3.7/logging/__init__.py", line 1412, in error
    self._log(ERROR, msg, args, **kwargs)
  File "/usr/local/lib/python3.7/logging/__init__.py", line 1519, in _log
    self.handle(record)
  File "/usr/local/lib/python3.7/logging/__init__.py", line 1529, in handle
    self.callHandlers(record)
  File "/usr/local/lib/python3.7/logging/__init__.py", line 1591, in callHandlers
    hdlr.handle(record)
  File "/usr/local/lib/python3.7/logging/__init__.py", line 905, in handle
    self.emit(record)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/system_log/__init__.py", line 168, in emit
    _figure_out_source(record, stack, self.hass))
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/system_log/__init__.py", line 99, in __init__
    self.root_cause = str(traceback.extract_tb(tb)[-1])
IndexError: list index out of range

Do you need to whitelist /config?

ummmmm no idea… how would I even do that?
I’m on Hass.io which is always difficult for this kind of thing.

I did find a solution though… a python script that does the same thing works.

I am also clearing out my Gmail inbox for alarm emails… I am using a python script that I can call from a button or automation, why not use python?

you might want to read the post directly above yours

Ow ok :wink: python works :wink:
Sorry, I don’t use shell scripts, not tested it

Maybe interesting below , seems you need to use the full path …

Also here, full path used in last replies…

I saw all those threads and posts before I posted. I don’t think any of them got it working.

ow ok, sorry, cant help then either :slight_smile:

I’m using the python one I found… it’s solved but thanks for your interest.

do you have a pyton that clears the complete inbox? or can you select the folder you want to clear?

It clears the complete inbox which is what I wanted. I found a lot of examples with selective deletes.

What happens is if my alarm is triggered, my router uses one analog line to call my other line (both VoIP) and then, if the Fritz detects the call is from the VoIP number, it goes to an answering machine and forwards the email to a gmail I setup just for getting these emails. When that gmail gets an email from my email address and has the phone number in the subject, the IMAP unread email in Home Assistant sees it and does a persistent notification as well as a notification to my mobile phone and I have a conditional card in lovelace that sees there is email and opens up a card showing the unread email and gives me a button I can now use to trigger the python script to delete the emails (which just have tones in them)

Job done… So I always know if my alarm has been triggered when I’m not here.

well, in my case, i have also setup another mailbox, just for alarm mails and also mails for arm/disarm status
since my alarm since has NO integration possibilites, i use a HA Imap sensor
so, every 5 seconds i check that mailbox
if i receive a mail with arm or disamed, based on the body text keywords, i run an automation todo all lights/music on and so on… its some kind of presence detector in my case :slight_smile:

Nice.

My alarm is 20 years old probably. I had a remote control fitted when I installed it and I just recently added a dry-contact switch so I can turn it on/off vis home assistant. It was a nightmare getting the dialer to work with VoIP… we don’t have a PSTN line anymore since we went on VDSL2 and the system was set to pulse not tone dialling and also waiting for a non-existent dialtone… So I have an automation now when I leave that closes the garage door (also retrofitted with a sonoff sv dry-contact) and sets the alarm and then an automation that checks if the state of the switches changed in the last 60 seconds and sends me a notification that the garage is closed and the alarm is set…

All this and the alerts for about $20…

1 Like

Nice ideas :wink:

@DavidFW1960 I don’t suppose you could share this Python script and its setup?

Script: (Edit in username/password) Save as emptyinbox.py in config directory with yaml files:

#!/usr/bin/python3
import imaplib
box = imaplib.IMAP4_SSL('imap.gmail.com', 993)
box.login("USERNAME-HERE","PASSWORD-HERE")
box.select('Inbox')
typ, data = box.search(None, 'ALL')
for num in data[0].split():
   box.store(num, '+FLAGS', '\\Deleted')
box.expunge()
box.close()
box.logout()

Sensor to detect alarm triggered & Shell command to run the script.

sensor:
# Alarm Emails
  - platform: imap
    name: Alarm Triggers
    server: imap.gmail.com
    port: 993
    username: !secret outlooksyncuser
    password: !secret outlooksyncpass
    folder: inbox
    search: !secret alarmnotification

shell_command:
  removealarm: python /config/emptyinbox.py

In Lovelace:

          - type: horizontal-stack
            cards:
              - type: conditional
                conditions:
                  - entity: sensor.alarm_triggers
                    state_not: "0"
                  - entity: sensor.alarm_triggers
                    state_not: "unavailable"
                card:
                  type: entities
                  entities:
                  - entity: sensor.alarm_triggers
              - type: conditional
                conditions:
                  - entity: sensor.alarm_triggers
                    state_not: "0"
                  - entity: sensor.alarm_triggers
                    state_not: "unavailable"
                card:
                  type: 'custom:button-card'
                  color_type: card
                  entity: sensor.alarm_triggers
                  name: Alarm Event
                  state:
                    - value: 0
                      operator: '>'
                      color: red
                      icon: mdi:alert
                      style:
                        - animation: blink 2s ease infinite
                      spin: false
                    - operator: default
                      color: green
                      icon: mdi:shield-check
                  tap_action:
                    action: call-service
                    service: shell_command.removealarm

Note it uses the custom button-card

Hope this helps.

1 Like

Sorry to resume such an old topic - but I was searching for something else and came across this one. I’m really interested in understanding how you made the alarm control smart. I do have a pretty old alarm system and if there’s any chance to be able to control it remotely it would be great ! Could you share what kind of switch you bought and how you wired it to the alarm?

Is your alarm able to send mails?