Home Assistant: Difference between revisions

Mfed3 (talk | contribs)
Combine declarative and UI defined automations: - Added another example for how to declaratively make "entity_id" customizations that will generate yaml code for them.
Mfed3 (talk | contribs)
Declarative Nix Configuration Examples: - Added another example of how to include multiple conditions and multiple actions
Line 374: Line 374:
</syntaxHighlight>
</syntaxHighlight>


=== Declarative Nix Configuration Examples ===
== More Declarative Nix Configuration Examples ==
 
=== Entity Customization ===


==== Entity Customization ====
You can declaratively define how entities appear in the GUI with respect to their display names (friendly_name) the "show as" (device_class) and the icon displayed (icon). See this page for more documentation and how the yaml will ultimately be generated: https://www.home-assistant.io/docs/configuration/customizing-devices/. <syntaxhighlight lang="nix">
You can declaratively define how entities appear in the GUI with respect to their display names (friendly_name) the "show as" (device_class) and the icon displayed (icon). See this page for more documentation and how the yaml will ultimately be generated: https://www.home-assistant.io/docs/configuration/customizing-devices/. <syntaxhighlight lang="nix">
     config = {
     config = {
Line 395: Line 396:
       };
       };
     };
     };
</syntaxhighlight>
=== Automations ===
==== Automation with a Condition ====
<syntaxhighlight lang="nix">
        {
          alias = "Name To Display in Automations List";
          trigger = {
            platform = "state";
            entity_id = "binary_sensor.someid1";
            to = "off";
            for = "00:10:00";
          };
          condition = {
            condition = "state";
            entity_id = "binary_sensor.someid2";
            state = "on";
          };
          action = {
            service = "light.turn_off";
            entity_id = "light.someid";
          };
        }
</syntaxhighlight>
==== Automation with Multiple Conditions, Multiple Actions ====
<syntaxhighlight lang="nix">
        {
          alias = "Name in Automations GUI";
          trigger = {
            platform = "state";
            entity_id = "binary_sensor.someid";
            to = "on";
          };
          condition = [
            {
              condition = "state";
              entity_id = "sun.sun";
              state = "below_horizon";
            }
            {
              condition = "state";
              entity_id = "light.someid";
              state = "off";
            }
          ];
          action = [
            {
              service = "notify.notify";
              data = {
                message = "Some Notification";
              };
            }
            {
              service = "siren.turn_on";
              entity_id = "siren.someid";
            }
          ];
        }


</syntaxhighlight>If you did not create any automations through the UI, Home Assistant will fail loading because the <code>automations.yaml</code> file does not exist yet and it will fail including it. To avoid that, add a systemd tmpfiles.d rule:
</syntaxhighlight>If you did not create any automations through the UI, Home Assistant will fail loading because the <code>automations.yaml</code> file does not exist yet and it will fail including it. To avoid that, add a systemd tmpfiles.d rule: