Custom Code: Write custom trigger to play beep sound when speeding

  1. Start by creating a custom code module with name my_triggers of type Execution. Go to Advanced > Custom Code:

Here is the code in plain text for easy copy and paste:

import logging

from messaging import extract_error_from

log = logging.getLogger(__name__)

def speed_warning_beep(result):
    """
    Trigger that will play a warning beep sound when vehicle speed exceeds value of 50.
    """

    # Check for error result
    error = extract_error_from(result)
    if error:
        log.warning("Speed warning beep trigger got error result: {:}".format(result))

        return

    # Check if speed value is above threshold
    if result.get("value", 0) >= 50:

        # Only play sound when threshold value is exceeded
        if not __context__.get("speed_warning_beep", False):
            __salt__["cmd.run"]("aplay /opt/autopi/audio/sound/beep.wav")

            # Set flag in context
            __context__["speed_warning_beep"] = True

    # Speed value is below threshold
    else:

        # Reset flag in context
        __context__["speed_warning_beep"] = False

Now synchronise the changes to the device by clicking the the Sync button. If the device is offline the changes will automatically be synchronised on next startup.

  1. For the OBD service named OBD_MANAGER register the created custom trigger as a hook so it can be used in worker workflows. Go to Advanced > Services > OBD_MANAGER > Hooks > Create:

  1. Next we have to use the newly registered trigger hook by adding it to a workflow in a worker. Go to Advanced > Services > OBD_MANAGER > Workers > Create:

When above changes have been successfully synchronised to the device, it will start checking speed and play a single beep sound when exceeding 50km/h.

Not working?
Check the log file on the device to see if there are warnings and/or errors: https://community.autopi.io/t/guide-how-to-retrieve-logs-from-your-device/

Want an event instead of a beep?

Then add another trigger function to the my_triggers custom code module that will fire an event instead of playing a sound:

def speed_warning_event(result):
    """
    Trigger that will fire a 'vehicle/speeding' event when vehicle speed exceeds value of 50.
    """

    # Check for error result
    error = extract_error_from(result)
    if error:
        log.warning("Speed warning event trigger got error result: {:}".format(result))

        return

    # Check if speed value is above threshold
    if result.get("value", 0) >= 50:

        # Only play sound when threshold value is exceeded
        if not __context__.get("speed_warning_event", False):
            __salt__["event.fire"]({}, "vehicle/speeding")

            # Set flag in context
            __context__["speed_warning_event"] = True

    # Speed value is below threshold
    else:

        # Reset flag in context
        __context__["speed_warning_event"] = False

This event can then be used in the EVENT_REACTOR service to perform other actions. Also the event will be uploaded to the cloud and be visible from Advanced > Events.

Need more help?
Please visit our documentation site http://docs.autopi.io/ or contact support@autopi.io.

4 Likes

I keep getting validation errors when attempting to create the hook

Hi JVteam,

Can you state what specific validation error you are experiencing? Please send a screenshot showing the error to support@autopi.io.

I got this working, now I’d like to add more. Which ones of these are one time only and which do I have to recreate each time? Is there an API to show all the different types of commands you can create sounds for?

No update? hello???

I want to raise an event. The event will have a field that will contain a json field. I understand I can use something like
__salt__["event.fire"]({}, "vehicle/speeding")

but how do i specify the field and its value like {“pax”:10}

__salt__["event.fire"]({"pax":10, "another_data": variable_name}, "vehicle/speeding")

that’s how

That’s cool. AutoPi documentation is seriously degrading my ability to use it.