Automation / Script condition apply only to next service call

Hi All,

I am having multiple automations where I want to perform a specific condition within, but have this condition applied only to the following service call.
The service call afterwards should be not affected.

Let me give you an example:

My automation looks like e.g.:

automation:
  - id: MyAutomation
    alias: 'My Automation'
    description: 'my test automation'
    trigger:
      - platform: state
        entity_id: light.dummy
        to: 'on'
    action:
      - service: logbook.log
        data:
          entity_id: light.dummy
          name: light.dummy
          message: 'Logbook Message 1 '
      - condition: state
        entity_id: sun.sun
        state: "below_horizon"
      - service: logbook.log
        data:
          entity_id: light.dummy
          name: light.dummy
          message: 'Logbook Message 2 '
      - service: logbook.log
        data:
          entity_id: light.dummy
          message: 'Logbook Message 3 '

In this case “Logbook Message 2” and “Logbook Message 3” are only written if the sun is below_horizon.
What I want is that “Logbook Message 2” is only written if the sun is below_horizon, but “Logbook Message 3” is ALWAYS written.

The workaround I applied is as follows via a separated script:

automation:
  - id: MyAutomation
    alias: 'My Automation'
    description: 'my test automation'
    trigger:
      - platform: state
        entity_id: light.dummy
        to: 'on'
    action:
      - service: logbook.log
        data:
          entity_id: light.dummy
          name: light.dummy
          message: 'Logbook Message 1 '
      - service: script.conditional_logbook
      - service: logbook.log
        data:
          entity_id: light.dummy
          name: light.dummy
          message: 'Logbook Message 3 '
          
          
script:
  conditional_logbook:
    description: 'Write log only on sun down'
    sequence:
      - condition: state
        entity_id: sun.sun
        state: "below_horizon"
      - service: logbook.log
        data:
          entity_id: light.dummy
          name: light.dummy
          message: 'Logbook Message 2 '

Never the less this does not seem like the most elegant solution to me.
Any recommendations how to solve this “better” without a special script part?

Thanks

Just move logbook 3 above the condition.

    action:
      - service: logbook.log
        data:
          entity_id: light.dummy
          message: 'Logbook Message 1 '
      - service: logbook.log
        data:
          entity_id: light.dummy
          message: 'Logbook Message 3 '
      - condition: state
        entity_id: sun.sun
        state: "below_horizon"
      - service: logbook.log
        data:
          entity_id: light.dummy
          message: 'Logbook Message 2 '

yes… well thanks for that reply.
That’s the easiest and obvious but cannot be achieved this way in all cases.

As said I am writing an example here stripped down to the most easy case.

If you want to add several conditions to the action section, use the “choose” clause.

Thanks - seems this was the hint I needed.

The following works as exptected:

automation:
  - id: my_automation
    alias: 'My Automation'
    description: 'my test automation'
    trigger:
      - platform: state
        entity_id: light.dummy
        to: 'on'
    action:
      - service: logbook.log
        data:
          entity_id: light.dummy
          message: 'Logbook Message 1 '
      - choose:
          - conditions:
              - condition: state
                entity_id: light.dummy
                state: 'off'
            sequence:
              - service: logbook.log
                data:
                  entity_id: light.dummy
                  message: 'Logbook Message 2 '
      - service: logbook.log
        data:
          entity_id: light.dummy
          message: 'Logbook Message 3 '
1 Like