index.adoc 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. [#single-button-toggle]
  2. = Single Button Toggle
  3. A single button toggle controls something that has two states, using just one action. Each press flips the state, and the button's icon changes to show the current state - a "closed" icon when the state is `closed`, and an "open" icon when the state is `open`.
  4. Unlike the xref:solutions/latching-toggle/index.adoc[Latching Toggle], which uses two interlocked actions, the button here is never disabled. It can always be pressed, and it always does the opposite of the current state.
  5. In OliveTin, the state is stored in an xref:entities/intro.adoc[entity file], which the action updates itself, and the icon is chosen with an xref:action_customization/icons.adoc[image icon] whose file name comes from the entity's state.
  6. The example on this page controls a garden water valve.
  7. [cols="1,1"]
  8. |===
  9. | Valve closed | Valve open
  10. a| image::solutions/single-button-toggle/dashboard-closed.png[]
  11. a| image::solutions/single-button-toggle/dashboard-open.png[]
  12. |===
  13. == How it works
  14. * The entity file `/etc/OliveTin/entities/water_valve.yaml` holds the state of the valve (`open` or `closed`). It is the single source of truth for the dashboard.
  15. * The **Toggle valve** action's icon is an `<img>` tag that uses `{{ .CurrentEntity.state }}` as the file name, so state `closed` shows `closed.png` and state `open` shows `open.png`.
  16. * The action has no `enabledExpression`, so it is never disabled.
  17. * When it runs, the action reads the current state from the entity file, works out the opposite state, and changes the `state:` line in the file. OliveTin watches entity files, so the dashboard and the icon update as soon as the file changes.
  18. * The state is read from the file, not from the dashboard, so two presses in quick succession each flip the valve, even if the second press comes before OliveTin has reloaded the entity. OliveTin runs one execution of an action at a time by default (`maxConcurrent: 1`), so the presses do not overlap.
  19. == Icons
  20. Save the two icons into the `custom-webui/icons/` directory next to your `config.yaml`, so that OliveTin serves them at `/custom-webui/icons/closed.png` and `/custom-webui/icons/open.png`. See xref:action_customization/icons.adoc[Icons] for more about serving your own icons.
  21. [cols="1,1"]
  22. |===
  23. | `closed.png` | `open.png`
  24. a| image::solutions/single-button-toggle/icons/closed.png[closed.png,48]
  25. a| image::solutions/single-button-toggle/icons/open.png[open.png,48]
  26. |===
  27. You can use any images you like, as long as each one is named after the state it represents.
  28. == Entity file
  29. [source,yaml]
  30. .`/etc/OliveTin/entities/water_valve.yaml`
  31. ----
  32. include::example$solutions/single-button-toggle/water_valve.yaml[]
  33. ----
  34. == Configuration
  35. [source,yaml]
  36. .`config.yaml`
  37. ----
  38. include::example$solutions/single-button-toggle/config.yaml[]
  39. ----
  40. == Customising this pattern
  41. * The same pattern works for anything with two states, such as lights, relays, services, or a maintenance mode. Add more icons if your entity has more than two states, and change the action to cycle through them.
  42. * Replace the `echo` command with your real hardware control, for example a GPIO command, an MQTT publish, or a call to a smart plug API.
  43. * Only write the new state to the entity file once the hardware command has succeeded. If the command fails, the shell exits before the state changes and the dashboard keeps showing the real state.
  44. * If something else can change the device (a physical switch, a schedule), have a script update the entity file too. The icon will then follow it.
  45. * The icon path starts with `/`, so it works from any dashboard. If OliveTin is served under a sub-path behind a reverse proxy, include that sub-path in the icon's `src`.
  46. * This action changes every `state:` line in the entity file, so to control several valves, give each one its own entity file, or update the right entity with a YAML-aware tool such as `yq`.
  47. * Add ACLs if only certain users should be able to operate the valve.
  48. == See also
  49. * xref:entities/intro.adoc[Entities]
  50. * xref:action_customization/icons.adoc[Icons]
  51. * xref:solutions/latching-toggle/index.adoc[Latching Toggle]
  52. * xref:solutions/human-in-the-control-loop/index.adoc[Human in the Control Loop]