Reduce code for automation with multiple conditions

I have four light-switches in the hallway that should all trigger the same automation (change light). The automation itself should be dependant on the

  • current time (day, evening, night)
  • current state of the light (low, normal, bright)

Currently the automation for the “turn on”-button looks like this (with two buttons, one light state and one time period):

- trigger:
  - platform: event
    event_type: homematic.keypress
    event_data:
      name: MEQ1850393
      channel: 1
      param: PRESS_SHORT
  - platform: event
    event_type: homematic.keypress
    event_data:
      name: MEQ1850385
      channel: 1
      param: PRESS_SHORT
  condition:
  - condition: state
    entity_id: light.flur
    state: 'off'
  action:
    service: light.turn_on
    data_template:
        entity_id: >
          {% if ((states.sensor.time.state > '19:30') and (states.sensor.time.state <= '23:59')) or (states.sensor.time.state < '07:00') %}
            light.flur_wohnungstur
          {% else %}
            light.flur
          {% endif %}
        brightness: >
          {% if ((states.sensor.time.state > '19:30') and (states.sensor.time.state <= '23:59')) or (states.sensor.time.state < '07:00') %}
            255
          {% else %}
            32
          {% endif %}

Is there a way to create a single automation rule that works with

  • all four switches
  • all three time periods
  • all three light states

Or to start with the code I currently have:

  • Can I use wildcards for the switch names?
  • Can I use a {% %} block for the whole data_template part so I don’t need to use redundant time period checks? It doesn’t seem to work when I try that.

Regards
Dan

You haven’t really fully defined what you’re looking for and what all the events and entities are and how they’re related, but I can provide some general feedback to start…

First, you say "day, evening, night’, yet the code so far only has two cases that are time dependent. You might want to create a template sensor whose value indicates day, evening or night. Then you don’t have to repeat all the time comparisons, making your automation(s) easier.

Next, to answer the question, “Can I use a {% %} block for the whole data_template part so I don’t need to use redundant time period checks?”, the answer is no. Templates are values of YAML keys. They cannot span multiple YAML keys.

Lastly, you might consider defining scenes. It will make the action part of your automation easier because you just specify the scene, and hence you don’t have to spell out all the details (entity_id & brightness, at least), reducing the number of templates you’ll need.

1 Like

I was going to say something along the same lines but noticed Phil was replying, so I waited

You can always guarantee Phil’s answer will be better than mine (not saying much), and better than most people’s. That’s why the gave him Thunderbird 2.

Thank you Phil and all. I will definitely look into template sensors and scenes.

Just a general question: I’m coming from FHEM, where you are able to write perl-functions with entity-states as variables, making you totally free in creating your own logic. I found this a nice option for complex automations like the one above - is anything like this possible with Home Assistant, or is YAML always the way to go? Not criticizing anything, I’m just curious :slight_smile:

HA has the Python Scripts component and that can make some things easier (I’ve written and used several), but it’s not a replacement for automations (they can be called, though, from automations just like “normal” HA Scripts), and they run in a fairly limited “sandbox” environment. If you want to do more complex things then you probably have to look at other options, such as AppDaemon or Node-RED (neither of which I know much about, only that I see a lot of people use them.)

1 Like

I’m stuck with scenes, although I’m almost sure everything is setup correctly.

In the configuration.yaml

scene:
  - name: Romantic
    entities:
      light.wohnzimmer:
        state: on

In one of my automation files:

- alias: 'Aeon Wallmote Quad - Button #1'
  trigger:
  - event_data:
      entity_id: zwave.aeon_labs_zw130_wallmote_quad
      scene_data: 0
      scene_id: 1
    event_type: zwave.scene_activated
    platform: event
  action:
    service: scene.turn_on
    entity_id: scene.romantic

When I trigger the action, the logfile says:

Error while executing automation automation.aeon_wallmote_quad_button_1. Service not found for call_service at pos 1: Unable to find service scene/turn_on

What could this be?

Have you restarted homeassistant since adding the scene component?

2 Likes

Dang… I reloaded the automations, but not the whole system. Now it’s working. Thanks!

1 Like