Optimize automation yaml - join 2 automations

Hi everyone,

I created 2 automations, one to turn pump ON and another to turn pump OFF, comparing temperature differential between 2 sensors and with input_number threshold.
I would prefer one automation only, so I can disable it once, and not having to disable 2 automations.

Would it be possible to have just one automation?

Current automation:

- id: '1675970881065'
  alias: Motor Piscina acima diferencial x - ON
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.temp_diff_sensores_piscina
    above: input_number.pump_piscina_offset_on
  condition: []
  action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.relay_motor_piscina
  mode: single
- id: '1676025521420'
  alias: Motor Piscina abaixo diferencial x - OFF
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.temp_diff_sensores_piscina
    below: input_number.pump_piscina_offset_off
    alias: When Temp Diff sensores Piscina is below input_number.pump_piscina_offset_off
  condition: []
  action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.relay_motor_piscina
  mode: single

Thx in advance
Goahead

Almost there:

 automation:
  - id: ID12
    alias: Sensor Temp Diff Piscina. If top hotter than bottom Sensor run pump - stop if not'
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: sensor.temp_diff_sensores_piscina
    action:
      entity_id: switch.relay_motor_piscina
      service_template: >
        {% if ((states.sensor.temp_diff_sensores_piscina.state | float)) < (states.input_number.pump_piscina_offset_on.state | float ) %} off
        {% elif ((states.sensor.temp_diff_sensores_piscina.state | float )) > (states.input_number.pump_piscina_offset_off.state | float ) %} on
        {% else %} pending {% endif %}

How to change state of entity_id: switch.relay_motor_piscina?
on, off and pending above, are just text.
:thinking:

you can’t do “pending” but you can easily do on & off with your existing template (I haven’t verified the template is logically what you want):

action:
  entity_id: switch.relay_motor_piscina
  service: >
    {% if ((states.sensor.temp_diff_sensores_piscina.state | float)) < (states.input_number.pump_piscina_offset_on.state | float ) %} switch.turn_off
    {% elif ((states.sensor.temp_diff_sensores_piscina.state | float )) > (states.input_number.pump_piscina_offset_off.state | float ) %} switch_turn_on
    {% endif %}

or you can use your previous two automations and condense them into one:

- id: '1675970881065'
  alias: Motor Piscina acima diferencial x - ON-OFF
  trigger:
  - platform: numeric_state
    entity_id: sensor.temp_diff_sensores_piscina
    above: input_number.pump_piscina_offset_on
    id: 'on'
  - platform: numeric_state
    entity_id: sensor.temp_diff_sensores_piscina
    below: input_number.pump_piscina_offset_off
    alias: When Temp Diff sensores Piscina is below input_number.pump_piscina_offset_off
  action:
  - service: switch.turn_{{ 'on' if trigger.id == 'on' else 'off'}}
    target:
      entity_id: switch.relay_motor_piscina
  mode: single
1 Like

thx finity

worked like a charm.

I dont know how to turn that action on/off in case I want to deactivated it , so I used the automation you shared.

Super.

Thanks again for your help.
Goahead

1 Like