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.
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