Newbie trying to understand YAML

Coming over from Hubitat and trying to wrap my head around this universe. I’m running Hass.io and trying to transfer over some of my Hubitat automations one at a time…For the most part I’m using a combination of google searches and the Automation GUI to port these over. I have a question that I bet is pretty simple for 90% of the users here. I currently have two automations built: One to turn on a switched light when a door is open and another to turn off the same switched light when the door closes. I’ve got to imagine these can be combined but I can’t figure out how to do it in the GUI and when I start to mess with a text editor I get syntax errors…I’m curious to know how the ‘experts’ here would combine these two automations?

- id: '1579741181375'
  alias: Laundry Room Light - turn on
  description: ''
  trigger:
  - entity_id: binary_sensor.visonic_mct_340_e_3440120b_ias_zone
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - device_id: af884a417a494ea0820f6fd43594c5dc
    domain: switch
    entity_id: switch.honeywell_unknown_type_4952_id_3036_switch
    type: turn_on

- id: '1579741745648'
  alias: Laundry Room Light - turn off
  description: ''
  trigger:
  - entity_id: binary_sensor.visonic_mct_340_e_3440120b_ias_zone
    from: 'on'
    platform: state
    to: 'off'
  condition: []
  action:
  - device_id: af884a417a494ea0820f6fd43594c5dc
    domain: switch
    entity_id: switch.honeywell_unknown_type_4952_id_3036_switch
    type: turn_off

Ever thought of reading the actual documentation? Start here, read everything under the Automation heading.

Personally I wouldn’t combine them. If it works, don’t fix it.

First of all, welcome!! I think what @nickrout was ‘trying’ to say is that - sometimes the most ‘streamlined’ way of creating automations in HA isn’t always the easiest.

Personally, I subscribe to the KISS (Keep It Simple Stupid) approach to HA - even if it means I have more scripts / automations than I ‘could’ have. This isn’t to call anyone stupid at all, but merely saying that HA is VERY malleable and there is almost ALWAYS more than one way to skin-the-cat. If you have free-time and want to compress a bunch/couple automations / scripts / whatever into a single occurence - GREAT - but it isn’t always REQUIRED to achieve your objective(s).

Again, WELCOME to HA, and happy automating!

2 Likes

Yeah, if your having trouble wrapping your head around YAML then the alternative to be able to combine the two automations (templates) is probably far beyond you right now.

Keep them separate.

Just remove the “from” and “to” so that any state change triggers the automation:

  trigger:
  - entity_id: binary_sensor.visonic_mct_340_e_3440120b_ias_zone
    platform: state

How will that help?

they will still need to write templates to figure out which action to perform based on which direction the state changes.

Shouldn’t toggle just work?

It would as long as the switch is only ever controlled by the automnation.

as soon as the light gets turned on by a switch when the motion sensor triggers it will turn the light off. I doubt that’s the intended outcome.

1 Like

True, which just goes to show that the original automation was perfectly fine.

1 Like
- id: '1579741177775'
  alias: Laundry Room Light
  trigger:
  - entity_id: binary_sensor.visonic_mct_340_e_3440120b_ias_zone
    platform: state
  action:
  - service_template: >
      {% if is_state('binary_sensor.visonic_mct_340_e_3440120b_ias_zone', 'on')%}
        switch.turn_on
      {% else %}
        switch.turn_off
      {% endif %}
    entity_id: switch.honeywell_unknown_type_4952_id_3036_switch

that will do it