Best practices to keep automations readable

I agree totally that sunset is not a good trigger. One of my first automations was to turn my study desk lamp on at sunset. On clear sunny days I wonder why it came on so early, and on dark cloudy days I have to turn it on manually.

This:

I have an aqara light sensor in an outer wall of my appartment and it’s spot on. It’s unexpensive and you needn’t fight with several sensors that often don’t update as soon or well as expected.

This one makes a better example of how beautiful and tidied up a complex flow can look vs a yaml automation. 8 triggers and even more conditions.

That’s pretty ugly IMO. Yaml all day :muscle:

2 Likes

Just curious. In that particular flow, in the conditional section, the flow sets different values to the variables playlist and source. Then after setting those variables according to different conditions, the action section evaluates those variables and acts accordingly.

How can those variables be set in yaml? within a single automation I mean.

There’s a variable section for scripts and automations, you can pass those all around everywhere. A quick example of something like that

alias: Announce at max volume
variables:
  old_volume: "{{ state_attr(media_player, 'volume_level') }}"
actions:
- service: media_player.set_volume
  target:
    entity_id: "{{ media_player }}"
  data:
    volume_level: 1.0
- service: notify.tts
  data:
    message: foo
- service: media_player.set_volume
  target:
    entity_id: "{{ media_player }}"
  data:
    volume_level: "{{ old_volume }}"
2 Likes

Would this work?

alias: Example
variables:
  playlist: >
    {% if is_state('input_boolean.away', 'off') and is_state('input_select.modo', 'Daily') and is_state('input_boolean.bluetooth_ducha', 'on')%}
      Ducha
    {% else %}
      ESPACIO
    {% endif %}
actions:
    [....]

Yes, anything you can dream of would work. Personally I use Jinja for everything which makes my automations look really simple but do complex things. You just need to know how to game the system. This of course probably requires a heavy amount of yaml, jinja, and template knowledge.

1 Like

Definitely it’s just personal prefference. That would be horrible to me, it would be the opposite of readable.

1 Like