Automation

Automation in MudPi is done through a combination of actions and triggers. It is recommended you are familiar with those features in order to take full advantage of the automation capabilities of MudPi.

The basic concept of automation in MudPi is that an event occurs and causes a trigger to fire one or many actions. For example lets say you have a soil sensor attached to MudPi. A StateUpdated event occurs for this sensor and you have a state trigger listening to check if the soil is too dry. If the soil is below a certain moisture percent (threshold) then it triggers an action for a toggle component to power a water pump.

Here is how this automation config could look using example components:

"sensor": [{
    "key": "example_soil_sensor",
    "interface": "example",
    "data": 10, 
    "classifier": "moisture"
}],
"toggle": [{
    "key": "example_pump",
    "interface": "example"
}],
"trigger": [{
    "key": "example_soil_dry",
    "interface": "state",
    "source": "example_soil_sensor",
    "name": "Soil is Dry",
    "frequency":"once",
    "actions": [".example_pump.turn_on"],
    "thresholds": [
        {
            "comparison":"lte",
            "value":5
        }
    ]
}]

This is a great start, but what if our pump is hooked up to a water tank and that tank is empty? That is no good. This can be accounted for by adding a float sensor to check if the water tank has water and using a trigger group to turn on the pump. Now we are getting into more complex and powerful automations.

Lets update the configuration to add in another example sensor and trigger to visualize this setup:

"sensor": [{
    "key": "example_soil_sensor",
    "interface": "example",
    "data": 10, 
    "classifier": "moisture"
},
{
    "key": "example_float_sensor",
    "interface": "example",
    "data": 1, 
    "classifier": "liquid_level"
}],
"toggle": [{
    "key": "example_pump",
    "interface": "example"
}],
"trigger": [{
    "key": "example_soil_dry",
    "interface": "state",
    "source": "example_soil_sensor",
    "name": "Soil is Dry",
    "frequency":"once",
    "thresholds": [{
        "comparison":"lte",
        "value":5
    }]
},
{
    "key": "water_available",
    "interface": "state",
    "source": "example_float_sensor",
    "name": "Water in Tank",
    "frequency":"once",
    "thresholds": [{
        "comparison":"gte",
        "value":1
    }]
},
{
    "key": "soil_dry_and_water_available",
    "interface": "group",
    "name": "Water Dry Plants",
    "triggers": ["example_soil_dry", "water_available"],
    "actions": [".example_pump.turn_on"],
    "frequency":"once"
}]

Automation Sequences

Using actions with triggers you can perform a large variety of automations. However in some cases actions and triggers alone are not enough to solve every situation. Automation Sequences build on actions by allowing you to create a list of steps that contain actions to trigger with a duration in between each step once the actions have been triggered. Each step can also have its own thresholds to check as well. As you may imagine this opens up the possibility to a variety of complex combinations to make some very useful automations.

Lets update the configuration above to use a sequence:

"sensor": [{
    "key": "example_soil_sensor",
    "interface": "example",
    "data": 10, 
    "classifier": "moisture"
},
{
    "key": "example_float_sensor",
    "interface": "example",
    "data": 1, 
    "classifier": "liquid_level"
}],
"toggle": [{
    "key": "example_pump",
    "interface": "example"
}],
"trigger": [{
    "key": "example_soil_dry",
    "interface": "state",
    "source": "example_soil_sensor",
    "name": "Soil is Dry",
    "frequency":"once",
    "thresholds": [{
        "comparison":"lte",
        "value":5
    }],
    "actions": [".example_sequence.next_step"]
}],
"sequence": [
    {
    "name":"Example Watering Sequence",
    "key": "example_sequence",
    "sequence": [
        {
            "actions": [".example_pump.turn_on"],
            "duration": 10,
            "thresholds": [
                {
                    "source": "example_float_sensor",
                    "comparison":"eq",
                    "value":1
                }
            ]
        },
        {
            "actions": [".example_pump.turn_off"],
            "duration": 0
        }
    ] }
]

This configuration would now start the automation sequence when the soil is dry. The first step of the sequence checks if the float sensor is active and thus water in the tank. The added benefit of the sequence is that now we have a duration for how long to keep the pump on. After 10 seconds the pump will turn off.

You can read more about automation sequences in the automation sequence documentation.