How to catch exceptions in automations? Or use a dummy entity_id?

Hello everyone,

I am slowly going though automations to build a block of code which will account for all my switches.

So far I managed to have them all defined in a dict (for the sake of the example, below is just one switch, switch.switch_entension, which is linked with a payload wazaa):

- id: '1548853202370'
  alias: testing switch 5
  trigger:
  - platform: mqtt
    topic: rfbridge-1/rfin
  action:
  - data_template:
      entity_id: >
        {% set rf = {"wazaa": "switch_extension"} %}
        switch.{{ rf[trigger.payload] }}
    service: switch.toggle

This works when the payload is wazaa but an exception is raised when it is anything else (which is normal, this is the equivalent of the KeyError exception).

From there I have two solutions:

  • handle the exception (à la try: in Python)
  • check if the payload is an expected one
  • or just let it crash

Solution 1: I do not know how to do that and whether this is at all possible

Solution 2: something along the lines of

- id: '1548853202370'
  alias: testing switch 5
  trigger:
  - platform: mqtt
    topic: rfbridge-1/rfin
  action:
  - data_template:
      entity_id: >
        {% set rf = {"wazaa": "switch_extension"} %}
        {% if trigger.payload in rf.keys() %}
        switch.{{ rf[trigger.payload] }}
        {% endif %}
    service: switch.toggle

The problem is that for unknown payloads, I end up with

  action:
  - data_template:
      entity_id: 
    service: switch.toggle

which is incorrect.

Is there a way to put a dummy entity_id (in an {% else %} condition) so that nothing happens when the unknown payload arrives?

I would like to avoid solution nr 3 (let it crash) for aesthetical and possibly performance/stability reasons.

There may be a better way (I’m not 100% sure what you are trying to do) but if you change the service from switch.toggle to homeassistant.toggle you could use the entity id of an input_boolean set up as a dummy switch for your ‘else’ case.

Sorry for not having been clear.

I have power switches which I want to toggle depending on a payload I receive via MQTT. When the payload is AAA then I will toggle the switch XXX, when the payload is BBB, switch YYY would be toggled, etc.

Instead of having one YAML block per switch I want to use a dict rf = {"AAA": "XXX", "BBB": "YYY"} which links these two information and toggle switch.{{ rf[trigger.payload] }}. This means that the switch is automatically chosen depending on the payload.

This is great until I get a payload which is not expected, and therefore not used as a key in the dict.My automation then crashes and I wanted to avoid this.

One of the ways to avoid that would be to check if the key exists in the dict, and if not - force a dummy switch as the entity_id (which would do nothing, but also avoid the crash).

Then the input_boolean solution I suggested should work.

Thank you. Is homeassistant.toggle a more general form of switch.toggle (and why are there two of them - if you know)?

Yes it is.

I’m really not sure why we have platform specific services as well as a set of homeassistant services that do the same thing. Historical development perhaps?

It works great - thank you thank you thank you :slight_smile:

If someone would be looking for the same problem, the code which worked for me is the following:

# this goes to configuration.yaml
input_boolean:
  dummy_switch:
      name: dummy

# and this goes to automations.yaml
# this is specific to a Sonoff RF bridge called rfbridge-1 sending its RF data data to the topic {hostname}/rfin
# but is it reusable in the general context of the question
- id: '1548853202370'
  alias: testing switch 5
  trigger:
  - platform: mqtt
    topic: rfbridge-1/rfin
  action:
  - data_template:
      entity_id: >
        {% set rf = {"wazaa": "switch_extension"} %}
        {% if trigger.payload in rf.keys() %}
        switch.{{ rf[trigger.payload] }}
        {% else %}
        input_boolean.dummy_switch
        {% endif %}
    service: homeassistant.toggle

FYI, you should be able to use a dummy switch entity_id, too (with either switch.toggle or homeassistant.toggle.) Haven’t tested this exact combination, but based on how standard components are written, they will just ignore entity_id’s that don’t exist. (At least I’m pretty sure. :wink:)

You are correct. I just tried to replace input_boolean.dummy_switch above with switch.does_not_exist and it was ignored (the automation started and ended). Thank you for that other solution.

1 Like