How to make this two logic in a single automation?

how to make this single automation ? i have tried this two automations but i need a single automation .

“Turn on motor1 and solenoid2 when capacity is less then 1500 litre and turn off when capacity is more then 2000 litre”

- alias: "Turn off motor1 and valve2 when capacity is 2000 litre"
  trigger:
    platform: numeric_state
    entity_id: sensor.up_tank_ultrasonic_sensor_2
    above: 2000
  condition:
    condition: or
    conditions:
      - condition: time
        after: '07:00:00'
        before: '17:00:00'
      - condition: time
        after: '19:00:00'
        before: '05:00:00'  
  action:
    service: switch.turn_off
    entity_id:
      - switch.motor1_2
      - switch.solnoid2_2
      
- alias: "Turn on motor1 and valve2 when capacity is 1500 litre"
  trigger:
    - platform: numeric_state
      entity_id: sensor.up_tank_ultrasonic_sensor_2
      below: 1500
  condition:
    condition: or
    conditions:
      - condition: and
        conditions:
          - condition: time
            after: '07:00:00'
            before: '17:00:00'
      - condition: and
        conditions:
          - condition: time
            after: '19:00:00'
            before: '05:00:00'    
  action:
    - service: switch.turn_on
      entity_id: 
        - switch.motor1_2
        - switch.solnoid2_2      

Can I ask what the reasoning is for wanting a single automation? These seem like logically two automations with two different triggers and two different actions.

You could make it a single automation, but it would be awkward, IMO, because you just end up checking for the trigger condition again down in the action section.

- alias: single_automation
  trigger:
    - platform: numeric_state
      entity_id: sensor.up_tank_ultrasonic_sensor_2
      above: 2000
    - platform: numeric_state
      entity_id: sensor.up_tank_ultrasonic_sensor_2
      below: 1500
  condition:
    - condition: or
      conditions:
        - condition: and
          conditions:
            - condition: time
              after: '07:00:00'
              before: '17:00:00'
        - condition: and
          conditions:
            - condition: time
              after: '19:00:00'
              before: '05:00:00' 
  action:
    - service_template: >
        switch.turn_{{ 'off' if ((states('sensor.up_tank_ultrasonic_sensor_2') |int) > 2000) else 'on' }}
      entity_id: 
        - switch.motor1_2
        - switch.solnoid2_2  

Like Steve said, a unified automation’s action will have to determine which trigger (above or below) was responsible for executing the automation.

There is a trick you can use to avoid having to re-enter the amount (1500 or 2000) in the action’s template. It is based on the following:

  • If the automation triggers because the sensor’s value is below 1500, then trigger.below will contain 1500 and trigger.below will be None.
  • If the automation triggers because the sensor’s value is above 2000, then trigger.below will be None and trigger.above will contain 2000.
  • If you use the int filter on a value that is None it reports 0.

We can use this information to create this simple template:

{{'on' if trigger.below | int > 0 else 'off'}}

The template doesn’t contain any hard-coded values and will use whatever is specified for below in the trigger section (and the template will work as long as below is set to a value greater than zero).

Here’s the unified automation with the template:

- alias: "Turn on/off motor1 and valve2 when capacity is 1500/2000 litre"
  trigger:
    - platform: numeric_state
      entity_id: sensor.up_tank_ultrasonic_sensor_2
      above: 2000
    - platform: numeric_state
      entity_id: sensor.up_tank_ultrasonic_sensor_2
      below: 1500
  condition:
    condition: or
    conditions:
      - condition: time
        after: '07:00:00'
        before: '17:00:00'
      - condition: time
        after: '19:00:00'
        before: '05:00:00'  
  action:
    service_template: "switch.turn_{{'on' if trigger.below | int > 0 else 'off'}}"
    entity_id:
      - switch.motor1_2
      - switch.solnoid2_2
1 Like