Controlling an entity on/off with one automation

This is probably simple but I’m not that familiar with programming. I have Caseta devices for switches, and a few ZWave PIR devices. I’m managed to get them working through the automations, but I have one automation for ON, then another automation for OFF. Looks clunky. Is there any way to do it with just one automation?

post the automations you have now. make sure to post the actual text of the code (not a screen shot) and format it properly.

Then someone can help you.

`id: ‘1576622365973’
alias: Half bath lighting ON
description: ‘’
trigger:

  • entity_id: sensor.vision_security_zp3111_multisensor_4in1_burglar_2
    from: ‘0’
    platform: state
    to: ‘8’
    condition: []
    action:
  • device_id: bc72cc2a04c64d929447f8e6d27fb7df
    domain: switch
    entity_id: switch.inovelli_unknown_type_0004_id_0001_switch
    type: turn_on
  • id: ‘1576622607968’
    alias: Turn on bathroom receptacle
    description: ‘’
    trigger:
    • at: 06:30
      platform: time
      condition: []
      action:
    • device_id: cc5043fdcf2c4759adab95440b2717d2
      domain: switch
      entity_id: switch.linear_nortek_security_control_llc_wo15z_1_single_wall_outlet_switch
      type: turn_on
  • id: ‘1576622664886’
    alias: Turn off bathroom receptacle
    description: ‘’
    trigger:
    • at: 07:15
      platform: time
      condition: []
      action:
    • device_id: cc5043fdcf2c4759adab95440b2717d2
      domain: switch
      entity_id: switch.linear_nortek_security_control_llc_wo15z_1_single_wall_outlet_switch
      type: turn_off
  • id: ‘1576623816337’
    alias: Turn on string lights
    description: ‘’
    trigger:
    • event: sunset
      platform: sun
      condition: []
      action:
    • device_id: 77ad9099ba1641e38dba64e0c00dfc0c
      domain: switch
      entity_id: switch.inovelli_unknown_type_0004_id_0001_switch_2
      type: turn_on
  • id: ‘1576893225700’
    alias: Half bath lighting OFF
    description: ‘’
    trigger:
    • entity_id: sensor.vision_security_zp3111_multisensor_4in1_burglar_2
      from: ‘8’
      platform: state
      to: ‘0’
      condition: []
      action:
    • device_id: bc72cc2a04c64d929447f8e6d27fb7df
      domain: switch
      entity_id: switch.inovelli_unknown_type_0004_id_0001_switch
      type: turn_off
  • id: ‘1576966649374’
    alias: Pressure pump on
    description: ‘’
    trigger:
    • at: 05:30
      platform: time
      condition: []
      action:
    • device_id: 067f70b06694406d9b1ac46511b0c075
      domain: switch
      entity_id: switch.linear_nortek_security_control_llc_wo15z_1_single_wall_outlet_switch_2
      type: turn_on
  • id: ‘1576966777757’
    alias: Pressure pump off
    description: ‘’
    trigger:
    • at: ‘21:00’
      platform: time
      condition: []
      action:
    • device_id: 067f70b06694406d9b1ac46511b0c075
      domain: switch
      entity_id: switch.linear_nortek_security_control_llc_wo15z_1_single_wall_outlet_switch_2
      type: turn_off
  • id: ‘1577041184479’
    alias: Pantry light on
    description: ‘’
    trigger:
    • above: ‘5’
      below: ‘9’
      entity_id: sensor.vision_security_zp3111_multisensor_4in1_burglar
      platform: numeric_state
      condition: []
      action:
    • alias: ‘’
      data: {}
      entity_id: switch.kitchen_pantry
      service: switch.turn_on
  • id: ‘1577041757760’
    alias: Pantry light off
    description: ‘’
    trigger:
    • below: ‘4’
      entity_id: sensor.vision_security_zp3111_multisensor_4in1_burglar
      platform: numeric_state
      condition: []
      action:
    • alias: ‘’
      data: {}
      entity_id: switch.kitchen_pantry
      service: switch.turn_off
  • id: ‘1577589713212’
    alias: Lock the doors
    description: ‘’
    trigger:
    • at: ‘21:00’
      platform: time
      condition: []
      action:
    • device_id: e248b2d8220d4fb980142e835b2ea09a
      domain: lock
      entity_id: lock.kwikset_spectrum_brands_smartcode_916_locked
      type: lock
    • device_id: 01227a0b1b7847ae8e88d76ba08d324d
      domain: lock
      entity_id: lock.kwikset_spectrum_brands_unknown_type_0003_id_0541_locked
      type: lock
  • id: ‘1577589983865’
    alias: Lock the doors
    description: ‘’
    trigger:
    • at: 07:30
      platform: time
      condition: []
      action:
    • device_id: 75c6420c495647b7aeb211ac515c4a22
      domain: lock
      entity_id: lock.kwikset_spectrum_brands_unknown_type_0003_id_0541_locked
      type: lock
    • device_id: 012bcabcc94647fdb5ff15061400ca8f
      domain: lock
      entity_id: lock.kwikset_spectrum_brands_smartcode_916_locked
      type: lock
  • id: ‘1577895329413’
    alias: Laundry room lights ON
    description: ‘’
    trigger:
    • entity_id: binary_sensor.shenzhen_neo_electronics_co_ltd_battery_powered_pir_sensor_sensor_3
      from: ‘off’
      platform: state
      to: ‘on’
      condition: []
      action:
    • alias: ‘’
      data: {}
      entity_id: switch.laundry_room_main_lights
      service: switch.turn_on
  • id: ‘1577900633640’
    alias: Laundry room lights OFF
    description: ‘’
    trigger:
    • entity_id: binary_sensor.shenzhen_neo_electronics_co_ltd_battery_powered_pir_sensor_sensor_3
      from: ‘on’
      platform: state
      to: ‘off’
      condition: []
      action:
    • alias: ‘’
      data: {}
      entity_id: switch.laundry_room_main_lights
      service: switch.turn_off`

It’s the Turn On, then Turn Off that looks really clunky on any of the automations

I think you forgot about this part.

select your code above in the editor window and then click on the </> button.

But from just glancing at the code it’s going to be difficult to reduce the automations to one for both on & off unless you are comfortable with templating. And since you say you aren’t familiar with programming I’m assuming that you aren’t comfortable with templating.

Anyway, go ahead and fix the formatting of the code and we might be able to move on from there.

- id: '1576622365973'
  alias: Half bath lighting  ON
  description: ''
  trigger:
  - entity_id: sensor.vision_security_zp3111_multisensor_4in1_burglar_2
    from: '0'
    platform: state
    to: '8'
  condition: []
  action:
  - device_id: bc72cc2a04c64d929447f8e6d27fb7df
    domain: switch
    entity_id: switch.inovelli_unknown_type_0004_id_0001_switch
    type: turn_on
- id: '1576622607968'
  alias: Turn on bathroom receptacle
  description: ''
  trigger:
  - at: 06:30
    platform: time
  condition: []
  action:
  - device_id: cc5043fdcf2c4759adab95440b2717d2
    domain: switch
    entity_id: switch.linear_nortek_security_control_llc_wo15z_1_single_wall_outlet_switch
    type: turn_on
- id: '1576622664886'
  alias: Turn off bathroom receptacle
  description: ''
  trigger:
  - at: 07:15
    platform: time
  condition: []
  action:
  - device_id: cc5043fdcf2c4759adab95440b2717d2
    domain: switch
    entity_id: switch.linear_nortek_security_control_llc_wo15z_1_single_wall_outlet_switch
    type: turn_off
- id: '1576623816337'
  alias: Turn on string lights
  description: ''
  trigger:
  - event: sunset
    platform: sun
  condition: []
  action:
  - device_id: 77ad9099ba1641e38dba64e0c00dfc0c
    domain: switch
    entity_id: switch.inovelli_unknown_type_0004_id_0001_switch_2
    type: turn_on
- id: '1576893225700'
  alias: Half bath lighting OFF
  description: ''
  trigger:
  - entity_id: sensor.vision_security_zp3111_multisensor_4in1_burglar_2
    from: '8'
    platform: state
    to: '0'
  condition: []
  action:
  - device_id: bc72cc2a04c64d929447f8e6d27fb7df
    domain: switch
    entity_id: switch.inovelli_unknown_type_0004_id_0001_switch
    type: turn_off
- id: '1576966649374'
  alias: Pressure pump on
  description: ''
  trigger:
  - at: 05:30
    platform: time
  condition: []
  action:
  - device_id: 067f70b06694406d9b1ac46511b0c075
    domain: switch
    entity_id: switch.linear_nortek_security_control_llc_wo15z_1_single_wall_outlet_switch_2
    type: turn_on
- id: '1576966777757'
  alias: Pressure  pump off
  description: ''
  trigger:
  - at: '21:00'
    platform: time
  condition: []
  action:
  - device_id: 067f70b06694406d9b1ac46511b0c075
    domain: switch
    entity_id: switch.linear_nortek_security_control_llc_wo15z_1_single_wall_outlet_switch_2
    type: turn_off
- id: '1577041184479'
  alias: Pantry light on
  description: ''
  trigger:
  - above: '5'
    below: '9'
    entity_id: sensor.vision_security_zp3111_multisensor_4in1_burglar
    platform: numeric_state
  condition: []
  action:
  - alias: ''
    data: {}
    entity_id: switch.kitchen_pantry
    service: switch.turn_on
- id: '1577041757760'
  alias: Pantry light off
  description: ''
  trigger:
  - below: '4'
    entity_id: sensor.vision_security_zp3111_multisensor_4in1_burglar
    platform: numeric_state
  condition: []
  action:
  - alias: ''
    data: {}
    entity_id: switch.kitchen_pantry
    service: switch.turn_off
- id: '1577589713212'
  alias: Lock the doors
  description: ''
  trigger:
  - at: '21:00'
    platform: time
  condition: []
  action:
  - device_id: e248b2d8220d4fb980142e835b2ea09a
    domain: lock
    entity_id: lock.kwikset_spectrum_brands_smartcode_916_locked
    type: lock
  - device_id: 01227a0b1b7847ae8e88d76ba08d324d
    domain: lock
    entity_id: lock.kwikset_spectrum_brands_unknown_type_0003_id_0541_locked
    type: lock
- id: '1577589983865'
  alias: Lock the doors
  description: ''
  trigger:
  - at: 07:30
    platform: time
  condition: []
  action:
  - device_id: 75c6420c495647b7aeb211ac515c4a22
    domain: lock
    entity_id: lock.kwikset_spectrum_brands_unknown_type_0003_id_0541_locked
    type: lock
  - device_id: 012bcabcc94647fdb5ff15061400ca8f
    domain: lock
    entity_id: lock.kwikset_spectrum_brands_smartcode_916_locked
    type: lock
- id: '1577895329413'
  alias: Laundry room lights ON
  description: ''
  trigger:
  - entity_id: binary_sensor.shenzhen_neo_electronics_co_ltd_battery_powered_pir_sensor_sensor_3
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - alias: ''
    data: {}
    entity_id: switch.laundry_room_main_lights
    service: switch.turn_on
- id: '1577900633640'
  alias: Laundry room lights OFF
  description: ''
  trigger:
  - entity_id: binary_sensor.shenzhen_neo_electronics_co_ltd_battery_powered_pir_sensor_sensor_3
    from: 'on'
    platform: state
    to: 'off'
  condition: []
  action:
  - alias: ''
    data: {}
    entity_id: switch.laundry_room_main_lights
    service: switch.turn_off

I think that’ll appear correctly. I’m reading about templates right now, I may be able to pick it up. Thanks for the correction.

let’s start with an easy example then:

- id: '1577895329413'
  alias: Laundry room lights ON
  description: ''
  trigger:
  - entity_id: binary_sensor.shenzhen_neo_electronics_co_ltd_battery_powered_pir_sensor_sensor_3
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - alias: ''
    data: {}
    entity_id: switch.laundry_room_main_lights
    service: switch.turn_on
- id: '1577900633640'
  alias: Laundry room lights OFF
  description: ''
  trigger:
  - entity_id: binary_sensor.shenzhen_neo_electronics_co_ltd_battery_powered_pir_sensor_sensor_3
    from: 'on'
    platform: state
    to: 'off'
  condition: []
  action:
  - alias: ''
    data: {}
    entity_id: switch.laundry_room_main_lights
    service: switch.turn_off

in the automation above you are using a simple state change of the motion sensor to turn the light off or on.

it can be reduced to the following:

- id: '1577895329413'
  alias: Laundry room lights
  description: ''
  trigger:
  - entity_id: binary_sensor.shenzhen_neo_electronics_co_ltd_battery_powered_pir_sensor_sensor_3
    platform: state
  action:
  - entity_id: switch.laundry_room_main_lights
    service_template: >
      {% if trigger.to_state.state == 'on' %}
        switch.turn_on
      {% else %}
        switch.turn_off
      {% endif %}

in the above the automation it is monitoring for any state change in the binary_sensor. it will then look to see if the new state is on or off. If the new state is on it will call the switch.turn_on service for the entity_id. otherwise it will turn the switch off.

you can do similar things to template the service for any of the above automations that have specific “to:” and “from:” states. as long as those are the only two states that the entity can have (or you don’t really care what the old state was) then you can just use the new state to compare from the “to:” option.

it gets harder when you start using above: and below: but not impossible.

But let’s see how you come along with those examples for now.

EDIT: lot’s of typos

Thank you. I copied the code, it works perfectly. Now that I have an example I see that it’s actually fairly simple. But thanks for helping out a newbie.