Empty Dummy service in automation

Hi,

Please check out:

I have created an empty service that does nothing. It can be used in service templates within automation to skip actions in specific case.

1 Like

I use a script called script.dummy for that purpose. It only contains a 1 millisecond delay. What is the advantage of a custom empty service?

It is true that scripts can be used instead, at least for me use of empty service feels more natural than starting dedicated script. It was my small project and my intention was to share my approach to the problem in case someone finds it more convenient.

From performance perspective service call triggers function, which in custom service case does nothing and immediately replies, so no impact on CPU resources to perform any more actions.

My use of this service:

    - alias: ''
      service_template: >
        {% if is_state('binary_sensor.someone', 'on') %}
          {% if is_state('switch.entrance_camera_motion_detection', 'on') %}
            camera.disable_motion_detection
          {% else %}
            none.none
          {% endif %}
        {% else %}
          {% if is_state('switch.entrance_camera_motion_detection', 'off') %}
            camera.enable_motion_detection
          {% else %}
            none.none
          {% endif %}
        {% endif %}
      data_template:
        entity_id: camera.entrance_camera

You can also use conditions within an action to permit execution of the service only if the correct conditions are present.

In your example, the service is executed only if the binary_sensor and switch have the same state. If they do not, then no service should be executed. So all we need is a condition that ensures the binary_sensor and switch have the same state (i.e. if they do not have the same state then execution stops at the condition and does not proceed to evaluate the service).

- alias: 'Example'
  trigger:
  - <your trigger>
  action:
  - condition: template
    value_template: >
      {{ states('binary_sensor.someone') == states('switch.entrance_camera_motion_detection') }}
  - service_template: >
      camera.{{'disable' if is_state('binary_sensor.someone', 'on) else 'enable'}}_motion_detection
    entity_id: camera.entrance_camera

I have some rather complex conditions and need to execute multiple independent actions within automation (it is just one of them). Conditions resulting in False stop the execution and creating multiple automation with same trigger creates a lot of duplicated code, which gets time consuming with modifications. In my case it is simpler just to skip action within one automation.

There are different options for sure, iā€™m just sharing how i solved it in my case with custom service.
If anyone else wants to use it, it is available on github.
There are cases where service or script would be better then conditions, but yes, conditions also work.

Everything you said is true but my response was for the example you provided, not as a general response to every possible use-case.

The community appreciates the addition of your custom component. Emphyrio and I simply pointed out that there are existing built-in methods to handle this situation. Now there is yet another way, using your custom component.

Agreed, it is nice to have options. My personal preference is to stick as much as possible to options that are available in core Home Assistant.