Automation enable/disable based on entity state(s)

I have an automation that sends me a notification on my phone when my camera detects motion, based on this blueprint. I only want this automation active when my alarm control panel is armed_home, armed_away or triggered state. It needs to disable when the alarm has state disarmed.

I got this to work, with two separate automations, which enable/disable the alarm based on the alarm state. Is there a better/cleaner way to map one or multiple entity states to an automation’s enabled status?

Side question: after messing around with HA for two weeks now, I have a mixed setup from the UI and some in the yaml files (things I copied from the Docs or from the forum). It disturbs me that the configuration isn’t in one place. Configs you do via the UI don’t end up in the yaml files, I learned. Should I try to migrate the UI generated automations for example to the yaml files?

Not really. The automation simply needs a condition that prevents execution of the action if the alarm control panel’s state is disarmed.

Automations created via the Automation Editor (the “UI”) are stored, in YAML format, within the automations.yaml file.

Programmatically enabling/disabling automations is not a “best practice”.

Yes that would make sense, however this is based on a blueprint that doesn’t have a condition:

Oh yes, you are right, bad example. I was thinking for example about devices, added via the UI, or settings made in the UI, not showing up in configuration.yaml.
So does this mean I can have further control over this automation coming from a blueprint, by editing the automations.yaml file?

Yeah, it felt like bad practice indeed, having two automations to enable/disable another one…

I found the blueprint in the blueprints folder.
I guess I can take that code and turn into a normal automation, adding my own condition.

Yes, you can (carefully) edit the automation generated by the Blueprint.

The alternative is, as you have already suggested, to forego using a Blueprint and simply create the automation in the usual manner.

1 Like

For future reference, if someone else is looking at translating this blueprint:

This is what I ended up with and it works perfectly:

- alias: Garage cam human > app notification
  description: ''
    platform: state
    entity_id: binary_sensor.mymotiondetectorrule_cell_motion_detection
    from: 'off'
    to: 'on'
    snapshot_create_file_path: /config/www/tmp/snapshot_cam_garage_mainstream.jpg
    snapshot_access_file_path: '{{ snapshot_create_file_path | replace(''/config/www'',''/local'')
    alias: Alarm is NOT disarmed
    condition: not
    - condition: state
      entity_id: alarm_control_panel.home_alarm
      state: disarmed
  - delay: 0
  - service: camera.snapshot
    entity_id: camera.cam_garage_mainstream
      filename: '{{ snapshot_create_file_path }}'
  - service: notify.notify
      title: Garage camera persoon!
      message: Garage camera heeft een persoon gezien!
      data: '{{ {"image": "%s"} | format(snapshot_access_file_path) }}'

It was a bit challenging, but I’m glad I did it.
And I learned a lot about conditions and templates along the way.

1 Like