Multiple Conditions in "Choose"

Hi guys,
I hope someone will be able to help me :slight_smile:
I’m trying to modify my automation in order to select a different value depending on a switch state…
If in heat mode, I will use the heateing temperature and if in cool mode, I will use the cooling temperature !

Here is the code I’m trying to adapt:

- id: heating2_timerswitch_on_choose_temp
  alias: Heating Chambres when enabling timer or restoring from away select the appropriate temperature
  trigger:
    - platform: state
      entity_id: "input_boolean.heating_timer_switch"
      to: "on"
    - platform: template
      value_template: "{{ not is_state_attr('climate.thermostat_chambres', 'preset_mode', 'away') }}"
  mode: single
  max_exceeded: silent
  action:
    - choose:
        - conditions:
            - condition: template
              value_template: >
                {% set t = strptime((now().timestamp() | timestamp_local), "%Y-%m-%d %H:%M:%S").timestamp()-strptime(now().strftime("%Y-%m-%d 00:00:00"),"%Y-%m-%d %H:%M:%S").timestamp() %}
                {% set time_start = state_attr('input_datetime.heating_weekday_morning_start','timestamp') %}
                {% set time_end = state_attr('input_datetime.heating_weekday_day_start','timestamp') %}
                {{ time_start <= t < time_end and is_state('binary_sensor.workday_sensor', 'on')}}
          sequence:
            - service: climate.set_temperature
              data_template:
                entity_id: climate.thermostat_chambres
                temperature: "{{ states('input_number.heating_temp2_morning') }}"
        - conditions:
            - condition: template
              value_template: >
                {% set t = strptime((now().timestamp() | timestamp_local), "%Y-%m-%d %H:%M:%S").timestamp()-strptime(now().strftime("%Y-%m-%d 00:00:00"),"%Y-%m-%d %H:%M:%S").timestamp() %}
                {% set time_start = state_attr('input_datetime.heating_weekday_day_start','timestamp') %}
                {% set time_end = state_attr('input_datetime.heating_weekday_evening_start','timestamp') %}
                {{ time_start <= t < time_end and is_state('binary_sensor.workday_sensor', 'on')}}
          sequence:
            - service: climate.set_temperature
              data_template:
                entity_id: climate.thermostat_chambres
                temperature: "{{ states('input_number.heating_temp2_day') }}"
        - conditions:
            - condition: template
              value_template: >
                {% set t = strptime((now().timestamp() | timestamp_local), "%Y-%m-%d %H:%M:%S").timestamp()-strptime(now().strftime("%Y-%m-%d 00:00:00"),"%Y-%m-%d %H:%M:%S").timestamp() %}
                {% set time_start = state_attr('input_datetime.heating_weekday_evening_start','timestamp') %}
                {% set time_end = state_attr('input_datetime.heating_weekday_night_start','timestamp') %}
                {{ time_start <= t < time_end and is_state('binary_sensor.workday_sensor', 'on')}}
          sequence:
            - service: climate.set_temperature
              data_template:
                entity_id: climate.thermostat_chambres
                temperature: "{{ states('input_number.heating_temp2_evening') }}"
        - conditions:
            - condition: template
              value_template: >
                {% set t = strptime((now().timestamp() | timestamp_local), "%Y-%m-%d %H:%M:%S").timestamp()-strptime(now().strftime("%Y-%m-%d 00:00:00"),"%Y-%m-%d %H:%M:%S").timestamp() %}
                {% set time_start = state_attr('input_datetime.heating_weekday_night_start','timestamp') %}
                {% set time_end = state_attr('input_datetime.heating_weekday_morning_start','timestamp') %}
                {{ (time_start <= t < 86400 or 0 <= t < time_end) and is_state('binary_sensor.workday_sensor', 'on')}}
          sequence:
            - service: climate.set_temperature
              data_template:
                entity_id: climate.thermostat_chambres
                temperature: "{{ states('input_number.heating_temp2_night') }}"

    - choose:
        - conditions:
            - condition: template
              value_template: >
                {% set t = strptime((now().timestamp() | timestamp_local), "%Y-%m-%d %H:%M:%S").timestamp()-strptime(now().strftime("%Y-%m-%d 00:00:00"),"%Y-%m-%d %H:%M:%S").timestamp() %}
                {% set time_start = state_attr('input_datetime.heating_weekend_morning_start','timestamp') %}
                {% set time_end = state_attr('input_datetime.heating_weekend_day_start','timestamp') %}
                {{ time_start <= t < time_end and is_state('binary_sensor.workday_sensor', 'off')}}
          sequence:
            - service: climate.set_temperature
              data_template:
                entity_id: climate.thermostat_chambres
                temperature: "{{ states('input_number.heating_temp2_morning') }}"
        - conditions:
            - condition: template
              value_template: >
                {% set t = strptime((now().timestamp() | timestamp_local), "%Y-%m-%d %H:%M:%S").timestamp()-strptime(now().strftime("%Y-%m-%d 00:00:00"),"%Y-%m-%d %H:%M:%S").timestamp() %}
                {% set time_start = state_attr('input_datetime.heating_weekend_day_start','timestamp') %}
                {% set time_end = state_attr('input_datetime.heating_weekend_evening_start','timestamp') %}
                {{ time_start <= t < time_end and is_state('binary_sensor.workday_sensor', 'off')}}
          sequence:
            - service: climate.set_temperature
              data_template:
                entity_id: climate.thermostat_chambres
                temperature: "{{ states('input_number.heating_temp2_day') }}"
        - conditions:
            - condition: template
              value_template: >
                {% set t = strptime((now().timestamp() | timestamp_local), "%Y-%m-%d %H:%M:%S").timestamp()-strptime(now().strftime("%Y-%m-%d 00:00:00"),"%Y-%m-%d %H:%M:%S").timestamp() %}
                {% set time_start = state_attr('input_datetime.heating_weekend_evening_start','timestamp') %}
                {% set time_end = state_attr('input_datetime.heating_weekend_night_start','timestamp') %}
                {{ time_start <= t < time_end and is_state('binary_sensor.workday_sensor', 'off')}}
          sequence:
            - service: climate.set_temperature
              data_template:
                entity_id: climate.thermostat_chambres
                temperature: "{{ states('input_number.heating_temp2_evening') }}"
        - conditions:
            - condition: template
              value_template: >
                {% set t = strptime((now().timestamp() | timestamp_local), "%Y-%m-%d %H:%M:%S").timestamp()-strptime(now().strftime("%Y-%m-%d 00:00:00"),"%Y-%m-%d %H:%M:%S").timestamp() %}
                {% set time_start = state_attr('input_datetime.heating_weekend_night_start','timestamp') %}
                {% set time_end = state_attr('input_datetime.heating_weekend_morning_start','timestamp') %}
                {{ (time_start <= t < 86400 or 0 <= t < time_end) and is_state('binary_sensor.workday_sensor', 'off')}}
          sequence:
            - service: climate.set_temperature
              data_template:
                entity_id: climate.thermostat_chambres
                temperature: "{{ states('input_number.heating_temp2_night') }}"

For every “condition: template”, I would like to add another condition:

 - condition: numeric_state
   entity_id: input_select.thermostat_mode
   value_template: heat

Tryed to do this, but not working:

- conditions:
            - condition: template
              value_template: >
                {% set t = strptime((now().timestamp() | timestamp_local), "%Y-%m-%d %H:%M:%S").timestamp()-strptime(now().strftime("%Y-%m-%d 00:00:00"),"%Y-%m-%d %H:%M:%S").timestamp() %}
                {% set time_start = state_attr('input_datetime.heating_weekend_morning_start','timestamp') %}
                {% set time_end = state_attr('input_datetime.heating_weekend_day_start','timestamp') %}
                {{ time_start <= t < time_end and is_state('binary_sensor.workday_sensor', 'off')}}
           - condition: numeric_state
             entity_id: input_select.thermostat_mode
             value_template: heat
          sequence:
            - service: climate.set_temperature
              data_template:
                entity_id: climate.thermostat_chambres
                temperature: "{{ states('input_number.heating_temp2_morning') }}"

So if in heat mode => choose this temperature from this input, and if in cool mode => choose this temperature from this input…

If someone as an idea would be really appreciated :slight_smile:

Thanks,

What makes you think that an “input_select” (which is text based) will work with a “numeric_state” condition platform?

And you aren’t using a template for the condition. you are using a state (a numeric_state wrongly in this case but a state nonetheless).

you should use just a “state” platform:

- condition: state
  entity_id: input_select.thermostat_mode
  state: heat

Thank you @finity ! This was obvious, but I didn’t see it…

- choose:
        - conditions:
            - condition: template
              value_template: >
                {% set t = strptime((now().timestamp() | timestamp_local), "%Y-%m-%d %H:%M:%S").timestamp()-strptime(now().strftime("%Y-%m-%d 00:00:00"),"%Y-%m-%d %H:%M:%S").timestamp() %}
                {% set time_start = state_attr('input_datetime.heating_weekday_morning_start','timestamp') %}
                {% set time_end = state_attr('input_datetime.heating_weekday_day_start','timestamp') %}
                {{ time_start <= t < time_end and is_state('binary_sensor.workday_sensor', 'on')}}
            - condition: state
                entity_id: input_select.thermostat_mode
                state: cool
          sequence:
            - service: climate.set_temperature
              data_template:
                entity_id: climate.thermostat_chambres
                temperature: "{{ states('input_number.heating_temp2_morning') }}"

Now I don’t have any error anymore but it seems that the condition is always true even if my input_select is not in state “cool”.

For exemple, my input is in state “heat” and when I change the value of the temperature, it still updates the thermostat…
Is there a need to specify that the condition must be an “AND”?

I’ve tried:

- choose:
        - condition: AND
        - conditions:
            - condition: template

But it gives me an error “missing property conditions”

I’m quite new at coding automation, so sorry for the newby question here. This automation is really elaborated so it’s complicated for me to understand it all.

Thanks for your help

Not really related to the problem but just something I noticed. Instead of the timestamp trickery you can do:

{{ now().fromtimestamp(state_attr('input_datetime.heating_weekday_morning_start', 'timestamp')).time() }}
and
{{ now().time() }}

Yeah, still think there should be an easier way to get a datetime object from an input_datetime but think this is already easier / clearer.

More to the problem, conditions are AND by default so no need for an “condition: AND”. But if you do want to use it

- choose:
        - condition: AND
          conditions:
            - condition: template
1 Like

Thank to your comment, I have reworked my timestamp trickery and it’s much more simpler like this !

- choose:
        - conditions:
            - condition: template
              value_template: >
                {{ now().fromtimestamp(state_attr('input_datetime.heating_weekday_morning_start', 'timestamp')).time() <= now().time() <= now().fromtimestamp(state_attr('input_datetime.heating_weekday_day_start', 'timestamp')).time() and is_state('binary_sensor.workday_sensor', 'on') }}
          sequence:
            - service: climate.set_temperature
              data_template:
                entity_id: climate.thermostat_chambres
                temperature: "{{ states('input_number.heating_temp2_morning') }}"
        - conditions:
            - condition: template
              value_template: >
                {{ now().fromtimestamp(state_attr('input_datetime.heating_weekday_day_start', 'timestamp')).time() <= now().time() <= now().fromtimestamp(state_attr('input_datetime.heating_weekday_evening_start', 'timestamp')).time() and is_state('binary_sensor.workday_sensor', 'on') }}
          sequence:
            - service: climate.set_temperature
              data_template:
                entity_id: climate.thermostat_chambres
                temperature: "{{ states('input_number.heating_temp2_day') }}"
        - conditions:
            - condition: template
              value_template: >
                {{ now().fromtimestamp(state_attr('input_datetime.heating_weekday_evening_start', 'timestamp')).time() <= now().time() <= now().fromtimestamp(state_attr('input_datetime.heating_weekday_night_start', 'timestamp')).time() and is_state('binary_sensor.workday_sensor', 'on') }}
          sequence:
            - service: climate.set_temperature
              data_template:
                entity_id: climate.thermostat_chambres
                temperature: "{{ states('input_number.heating_temp2_evening') }}"
        - conditions:
            - condition: template
              value_template: >
                {{ now().fromtimestamp(state_attr('input_datetime.heating_weekday_night_start', 'timestamp')).time() <= now().time() <= now().fromtimestamp(state_attr('input_datetime.heating_weekday_morning_start', 'timestamp')).time() and is_state('binary_sensor.workday_sensor', 'on') }}
          sequence:
            - service: climate.set_temperature
              data_template:
                entity_id: climate.thermostat_chambres
                temperature: "{{ states('input_number.heating_temp2_night') }}"

And thanks to this, I have found that if I add “and” using “is_state” instead of “state_attr” I have the right formula :slight_smile:

{{ now().fromtimestamp(state_attr('input_datetime.heating_weekday_day_start', 'timestamp')).time() <= now().time() <= now().fromtimestamp(state_attr('input_datetime.heating_weekday_evening_start', 'timestamp')).time() and is_state('binary_sensor.workday_sensor', 'on') and is_state('input_select.thermostat_mode', 'heat')}}

Thanks for your help !!