Can't get if/else to work in automations.yaml

Hi there, I can’t seem to get any if/else condition working in the automations.yaml. All I’m trying to do is to switch a Fibaro Wall Plug on if it’s off and vice versa. Here is what I tried:

- id: '1573505971844'
  alias: 'Schalter #1 - Aus [kurz]'
  trigger:
  - event_data:
      channel: 2
      name: MEQ1850393
      param: PRESS_SHORT
    event_type: homematic.keypress
    platform: event
  action:
  - device_id: 8d939318d6374dafa825c29dcb711978
    domain: switch
    entity_id: switch.fibaro_system_fgwpe_f_wall_plug_gen5_switch
    type: '{%if is_state("switch.fibaro_system_fgwpe_f_wall_plug_gen5_switch", "on")%}turn_off{%else%}turn_on{%endif%}'

I found this syntax many times in the forums, why is it not working?

The logfile says

Invalid config for [automation]: value is not allowed for dictionary value @ data['type']. Got None.

Are you sure you saw it like this? I don’t think “type” is a valid parameter for an action, it should be service instead, or in your case service_template. Anyway, why don’t you use the the “toggle” service? The toggle service does exactly that, turn off if it’s on and turn on if it’s off.

Something like:

- id: '1573505971844'
  alias: 'Schalter #1 - Aus [kurz]'
  trigger:
  - event_data:
      channel: 2
      name: MEQ1850393
      param: PRESS_SHORT
    event_type: homematic.keypress
    platform: event
  action:
  - service: switch.toggle
    entity_id: switch.fibaro_system_fgwpe_f_wall_plug_gen5_switch

It’s that new-fangled Device Automation thing. It’s a great idea when used via the UI. However, when converted to text in an automation, it looks like the long way to get something done (compared to calling a service). I’m not very familiar with its syntax. In this case, maybe it doesn’t like the presence of template in type? :man_shrugging:

Okay looks really weird, maybe I should setup a test instance of home assistant to test all these UI editors just to help other people.

1 Like

You are a braver man than I …
But then again, it’s what Taras does :smiley:

Edit: regardless the OP should go with your toggle suggestion, neater, cleaner and it does exactly what it says on the tin !

1 Like

Where did you find the syntax you used for that device automation action (sorry, quoting a code snippet doesn’t get formatted correctly…)?

Is it officially documented somewhere?

We think it was the Abortions Automations Editor

I guess I didn’t know where the output from the “device automations” GUI editor got stored. But it makes sense that it is just plugged into the default automations.yaml file like the regular editor does.

It seems that in trying to “make it easier” that new way of doing things is creating more “gotcha’s” and ends up making it harder.

And it not only makes it harder for new people when things don’t work correctly or “it’s not supported” but it also makes it harder for more experienced people try to help them when it doesn’t work because we have to go back and relearn everything/set up a test instance/etc to help them figure out if it’s their logic or the automation editor that isn’t working correctly …:roll_eyes:

I like helping people but the complications of setting up a test instance to try to decipher the intricacies of the GUI editors is of almost no interest to me. Especially when the “old school” way of writing it by hand isn’t that hard and the forums are overflowing with examples of how to do things that way.

2 Likes

I don’t use the automation editor either so first time for me seeing this too. The thought of stuff like “device_id:8d939318d6374dafa825c29dcb711978” littered throughout my code hurts my head.

My question is why does it need a device_ID, a domain, AND an entity_id ? Isn’t that rather redundant? I did play around with it for a bit to check it out and I think the deal is that only devices that show up in the integrations panel are available as a device trigger/condition/action. Looks like the options available for device trigger type are also automatically set. I get several trigger types for a multi-sensor but nothing to choose from for my Nest thermostat. None of the devices I have offer a device action so I couldn’t test that. I think OP manually typed his template in the type: parameter when only values provided by the automation editor should be there. Does his switch even offer a device action?

It does say in the device automations docs that they are only meant to be configured in the UI.

image

- id: '1574008928926'
  alias: New Automation
  description: ''
  trigger:
  - above: 75
    below: 85
    device_id: 327c66af38ca44f4ae135e5aa81fb2f6
    domain: sensor
    entity_id: sensor.upstairs_bathroom_humidity
    for:
      hours: 0
      minutes: 5
      seconds: 0
    platform: device
    type: humidity
  condition:
  - condition: device
    device_id: e05f3948361c4cd5bb7145c86fce2a89
    domain: binary_sensor
    entity_id: binary_sensor.side_entrance_motion
    for:
      hours: 0
      minutes: 5
      seconds: 0
    type: is_motion
  action:
  - alias: ''
    data: {}
    service: alarm_control_panel.alarm_arm_home
2 Likes

I’m assuming that it is a work in progress and that magically everything will just work and every piece of code given on the forums will be transformed into the new format, with us old diehards (not that I’ve been hand coding that long (but I’ve NEVER used this ‘more problems than solutions’ editor) ) coming round having been won over by the elegant simplicity of it all. :rofl:
Come the revolution, we’ll be first up against the wall. :roll_eyes:
I’d just like one example of a new format reasonably complex automation (against a standard one, preferably with some explanation) so I could use it as a rosetta stone.

What ghastly looking code :face_vomiting:

Maybe it shouldn’t have been released until the “progress” was closer to being completed.

It’s caused a bunch of confusion the way it is now.

Yep

Some more stuff to get past 10 character minimum

1 Like

Sorry for the confusion all. I’m a long time SmartHome user but new to Home Assistant. The code example was indeed created with the automation tool. I added the if/else snippet from another forum post because I was assuming you could use it anywhere instead of static strings. But the whole topic seems to be way more complex.

You are right that a toggle makes more sense in my example, but I wanted to learn the scripting basics to use it for other stuff later.

Guess I’ll ditch the GUI and start using the documentation and an external editor.

2 Likes

A few thoughts …

The Device Automations editor is very useful, especially for new users. It stores the code it generates in configuration.yaml or automations.yaml using new keywords like device_id and domain. It would be quite difficult to create this format manually, meaning without using the Device Automations editor, primarily because of device_id (it’s cryptic). However, it was never meant to be created manually, this is just its representation in YAML.

I think it’s a good thing that it does store it as YAML (despite being a bit cryptic) because the alternative would be how Hubitat works. All its automations (rules) are created via a graphical editior. This editor is more sophisticated than either Automation Editor or Device Automation editor. However none of the rules it produces can be saved as, or shared as, a text file. That’s right, there’s no way to transform the graphical rules into text. So if you visit their forum, all questions about rules are presented as screenshots. If you see a rule you like, you have to create it from scratch using the screenshot as a reference; the author is unable to share it as text.

One of Home Assistant’s strengths is its ability to save and share automations as text. If it did not have this feature, I doubt the growth of its popularity would have been as rapid as it’s been.