One time password: checking and handling

I wanted a way to use the OTP (one time password) sensor in an automation, but I also wanted an else clause in there to alert me when things go wrong.

Just setup the OTP sensor as described in the docs sensor.otp
You need to, of course, make the script do something usefull but this is the automation

id: py_test
alias: 'OTP checking'
trigger:
    platform: event
    event_type: telegram_command
    event_data:
      command: '/otpCheck'
action:
   - service: python_script.otpcheck
     data_template:
       key: '{{trigger.event.data["args"][0]}}'

and this is the otpcheck.py script:

otpEntity = "sensor.otpkey"
key = data.get('key')

# Get the OTP code
otpKey = hass.states.get(otpEntity).state
if (key == otpKey):
    hass.services.call('telegram_bot', 'send_message', {'title':'*Ok*', 'message': 'w00t'})
else:
    hass.services.call('telegram_bot', 'send_message', {'title':'*ERROR*', 'message': 'Oops, something went wrong'})

This will allow you to perform actions based on the OTP code, and handle the else scenario as well

1 Like