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):
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
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.
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).
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?
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. )
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.