Help request: Choose action

Hi.
This automation is driving me crazy:

The choose action keeps going to default, and even when the conditions are not met, it continues when it shouldn’t.


I’m pasting here the yaml of the choose action.

choose:
  - conditions:
      - condition: numeric_state
        entity_id: input_number.veces_z1
        above: 1.9
        below: 2.1
      - condition: trigger
        id:
          - segundo
    sequence: []
  - conditions:
      - condition: numeric_state
        entity_id: input_number.veces_z1
        above: 2.9
        below: 3.1
      - condition: trigger
        id:
          - tercero
    sequence: []
default:
  - condition: numeric_state
    entity_id: input_number.veces_z1
    above: 0.9
    below: 1.1
  - condition: trigger
    id:
      - primero

Any ideas?
is it a better way to write this?
Thanks in advance

When none of the conditions are met, that is when the default action is run. See: https://www.home-assistant.io/docs/scripts#choose-a-group-of-actions

Your default action has conditions. Default actions don’t actually have these. Though an action list can.

How about explaining what you actually want to occur?

Hello Arturo Castaño,

Those conditions are and-ed, so the id from above has to match the condition below. When the trigger above is ‘segudo’, then for that to trigger the entity has to be outside of the 1.9-2.1 range. Make sure that is the case.

Also I feel adding conditions to the default is weird. I usually code so that default has a way to handle everything else, and not condition it.

I’m thinking (without having the whole code base) that you are double checking things and some of the logic is broken.

If you want a better answer, edit that and put the whole automation in there.

is it there a wahy to avoid the “default” path?

this is the whole automation.
It has another “conditioned default path” but thatone works fine

alias: Riego Z1
description: ""
triggers:
  - trigger: time
    at: input_datetime.hora1_z1
    id: primero
  - trigger: time
    at: input_datetime.hora2_z1
    id: segundo
  - trigger: time
    at: input_datetime.hora3_z1
    id: tercero
conditions: []
actions:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: input_number.veces_z1
            above: 1.9
            below: 2.1
          - condition: trigger
            id:
              - segundo
        sequence: []
      - conditions:
          - condition: numeric_state
            entity_id: input_number.veces_z1
            above: 2.9
            below: 3.1
          - condition: trigger
            id:
              - tercero
        sequence: []
    default:
      - condition: numeric_state
        entity_id: input_number.veces_z1
        above: 0.9
        below: 1.1
      - condition: trigger
        id:
          - primero
  - choose:
      - conditions:
          - condition: time
            weekday:
              - mon
          - condition: state
            entity_id: input_boolean.lun_z1
            state: "on"
        sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.z1
      - conditions:
          - condition: time
            weekday:
              - tue
          - condition: state
            entity_id: input_boolean.mar_z1
            state: "on"
        sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.z1
      - conditions:
          - condition: time
            weekday:
              - wed
          - condition: state
            entity_id: input_boolean.mie_z1
            state: "on"
        sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.z1
      - conditions:
          - condition: time
            weekday:
              - thu
          - condition: state
            entity_id: input_boolean.jue_z1
            state: "on"
        sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.z1
      - conditions:
          - condition: time
            weekday:
              - fri
          - condition: state
            entity_id: input_boolean.vie_z1
            state: "on"
        sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.z1
      - conditions:
          - condition: time
            weekday:
              - sat
          - condition: state
            entity_id: input_boolean.sab_z1
            state: "on"
        sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.z1
    default:
      - condition: time
        weekday:
          - sun
      - condition: state
        entity_id: input_boolean.dom_z1
        state: "on"
      - action: switch.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: switch.z1
  - delay: 00:{{ states('input_number.duracion_z1') | int }}:00
  - action: switch.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: switch.z1
mode: restart

Yes, match one of your choose options.

I just realized your default isn’t doing anything in the top branch.
In fact there is no sequence filled in in the top branch.

Are you trying to use the first Choose to control the second Choose…? If so, that’s not how they work.

I’m sorry, I did not understand you.
I manage to “force” a solution and now it is working. I use a “stop” action as default. So if the conditions are not met, it just stops.

alias: Riego Z1
description: ""
triggers:
  - trigger: time
    at: input_datetime.hora1_z1
    id: primero
  - trigger: time
    at: input_datetime.hora2_z1
    id: segundo
  - trigger: time
    at: input_datetime.hora3_z1
    id: tercero
conditions: []
actions:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: input_number.veces_z1
            above: 1.9
            below: 2.1
          - condition: trigger
            id:
              - segundo
        sequence: []
      - conditions:
          - condition: numeric_state
            entity_id: input_number.veces_z1
            above: 2.9
            below: 3.1
          - condition: trigger
            id:
              - tercero
        sequence: []
      - conditions:
          - condition: numeric_state
            entity_id: input_number.veces_z1
            above: 0.9
            below: 1.1
          - condition: trigger
            id:
              - primero
        sequence: []
    default:
      - stop: ""
  - choose:
      - conditions:
          - condition: time
            weekday:
              - mon
          - condition: state
            entity_id: input_boolean.lun_z1
            state: "on"
        sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.z1
      - conditions:
          - condition: time
            weekday:
              - tue
          - condition: state
            entity_id: input_boolean.mar_z1
            state: "on"
        sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.z1
      - conditions:
          - condition: time
            weekday:
              - wed
          - condition: state
            entity_id: input_boolean.mie_z1
            state: "on"
        sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.z1
      - conditions:
          - condition: time
            weekday:
              - thu
          - condition: state
            entity_id: input_boolean.jue_z1
            state: "on"
        sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.z1
      - conditions:
          - condition: time
            weekday:
              - fri
          - condition: state
            entity_id: input_boolean.vie_z1
            state: "on"
        sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.z1
      - conditions:
          - condition: time
            weekday:
              - sat
          - condition: state
            entity_id: input_boolean.sab_z1
            state: "on"
        sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.z1
      - conditions:
          - condition: time
            weekday:
              - sun
          - condition: state
            entity_id: input_boolean.dom_z1
            state: "on"
        sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.z1
    default:
      - stop: ""
  - delay: 00:{{ states('input_number.duracion_z1') | int }}:00
  - action: switch.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: switch.z1
mode: restart

the idea fo this automations is to have the variables in the UI, so its possible to modify it with helpers.
The three triggers are input_datetime for selecting 3 hours

The first “choose” is to select how many times a day irrigation starts. (1, 2 or 3 times a day)

The second is to select Monday to Sunday, based in sliders.

Do you think there is a prettier way to do this?
Any ideas are welcome.

If the goal is to have the first Choose act as conditional logic to determine whether the second Choose runs, you should be using the conditions block, not a Choose action.

alias: Riego Z1
description: ""
triggers:
  - trigger: time
    at: input_datetime.hora1_z1
    id: 1
  - trigger: time
    at: input_datetime.hora2_z1
    id: 2
  - trigger: time
    at: input_datetime.hora3_z1
    id: 3
conditions:
  - condition: template
    value_template: |
      {{ states('input_number.veces_z1') | int(0) >= trigger.id | int(0) }}
  - alias: Prueba si el input_boolean para hoy tiene el estado "on"
    condition: template
    value_template: |
      {% set dias_abbrev = ['lun', 'mar', 'mie', 'jue', 'vie', 'sab', 'dom']%}
      {% set ent = 'input_boolean.'~ dias_abbrev[now().weekday()] ~'_z1' %}
      {{ is_state(ent, 'on') }}
actions:
  - action: switch.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: switch.z1
  - delay: 00:{{ states('input_number.duracion_z1') | int }}:00
  - action: switch.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: switch.z1
mode: restart

You are right. That is much more elegant. Thanks a lot!!

I’ve made a couple changes in my earlier post because I think there was a flaw in the logic of the conditions you originally had, but I may be misunderstanding the bigger picture… so check my reasoning before making any changes.

From what you have said:

  • The value of input_number.veces_z1 is the number of times the irrigation should be allowed to run.
  • Assuming that the datetime Helpers always occur following the sequence 1, 2, 3.

If input_number.veces_z1 has a state of “2.0”, then the condition group you had in your original config for the first run of the irrigation…

- condition: numeric_state
  entity_id: input_number.veces_z1
  above: 0.9
  below: 1.1
- condition: trigger
  id: primero

… will not work. The irrigation cycle would only run one time, not two. You would have needed to use something like:

- or:
    - condition: trigger
      id: primero
    - and:
         - condition: numeric_state
           entity_id: input_number.veces_z1
           above: 1.1
         - condition: trigger
           id: segundo
    - and:
        - condition: numeric_state
          entity_id: input_number.veces_z1
          above: 2.1
        - condition: trigger
          id: tercero

In my post above I changed the trigger ID’s to numbers instead of the words “primero” etc to make templating easier:

- condition: template
  value_template: |
    {{ states('input_number.veces_z1') | int(0) >= trigger.id | int(0) }}

Hi!. I’m sorry for the late answer. I saw the first “mistake” in the logic, so I fixed it easily.

The template works great. That was my idea at the beginning, but I was not able to write the template… I’m more a “hardware guy” haha.
Thanks for the support!