# Get a Slack message when a limit is crossed

# Get a Slack message when a limit is crossed

Post to a Slack channel when a reading crosses a value. The test keeps
running.

## Before you start

- A Slack connector on **Connectors** in the console. Its bot needs the
  `chat:write` scope. See [Slack](https://phloem.ai/docs/connectors/slack/).
- The bot invited to the channel it posts in.
- A Trellis setup you can deploy from Phloem Computer. See [Trellis](https://phloem.ai/docs/trellis/).

## Steps

1. In the console, open **Alerts** and choose **New role**.
2. Type a role name, such as `test-conductor`. Use lowercase letters, digits,
   `-` or `_`.
3. Pick your Slack connector in **choose a connector…** and type the channel,
   such as `#test-alerts`. To post in more places, press **+ another channel**.
4. Press **Save**.
5. In Computer, open **Trellis** and open your setup in **Code**. Add an
   `alarm` that names the role in `notify=`:

   ```python
   setup("heater")

   ch = channels()
   ch.read("/demo/temperature", unit="degC")

   alarm(
       "too_hot",
       when=ch["/demo/temperature"].above(80),
       severity="warn",
       notify=["test-conductor"],
   )

   page("Heater test", layout=col(alarms()))
   ```

   | | |
   |---|---|
   | **`when`** | `above(x)`, `below(x)`, `within(lo, hi)` or `outside(lo, hi)` on a channel |
   | **`severity`** | `info`, `warn` or `critical` |
   | **`notify`** | role names from **Alerts** |

6. Press **Deploy**.

## Check it worked

When the alarm fires, Slack gets a message such as
`[warn] too_hot on /demo/temperature …`, with how long ago it fired.

On **Alerts**, **Recent deliveries** lists each firing and its outcome:

| Outcome | Means |
|---|---|
| **delivered** | posted to the channel |
| **rolled up** | counted together with repeats or a burst from the same device |
| **too old to page** | fired too long ago to post as live |
| **no such role** | `notify=` names a role **Alerts** does not have |
| **no working channel** | the role's connector is gone or inactive |
| **failed · retrying** | the send failed and is retried; hover for the reason |

> **Note:** Messages are sent from the cloud. A firing reaches Slack once the
> device's data does, so an offline device posts late, with the firing's age.

- [Alerts](https://phloem.ai/docs/trellis/alerts/): The alarm reference
- [Stop a test on a limit](https://phloem.ai/docs/guides/stop-on-a-limit/): Stop the test instead of telling someone
- [Marks](https://phloem.ai/docs/review/marks/): Every firing is a mark you can find in Replay
