How to select second lowest temperature?

Hi everyone, i’m trying to do an automation to start my HVAC based on temperature and solar production.

I’m using a min sensor to decide which HVAC to start first, but i can occurr into a situation in which the HVAC in the coldest room has already started and when i check again to start another HVAC the automation “skips” because min sensor is again on the same room… is there any way to select the minimum temperature between the HVAC that are OFF or at least to select the second lowest?

Thanks

P.S. I’m not very familiar with NodeRed, but would that give an help in this case?

Both of those are possible, but the first one seems more sensible…

{{ (expand('climate.1', 'climate.2', 'climate.3', 'climate.4')
| selectattr('state', 'eq', 'off')
| sort(attribute = 'attributes.current_temperature')
| list)[0].entity_id }}

AMAZING!
That’s what i was looking for, just two questions: i should copy those lines into a template sensor right?
And secondly: i use 5 external temperature sensors, so sorting would be on those sensors and not on an HVAC attribute, how should i modify it?
Thanks again

That’s really up to you. If it’s information that you think you’ll use in multiple places then it makes sense to create a template sensor. If not, then you can use it as a variable in the automation.

Since you need to make a selection based on the states of both devices, we will need to figure out how to link them. In most cases, this is a good place to use the Area where the devices are located. Have you assigned Areas to your sensors and hvac devices? Is therea 1:1 relationship between them… i.e. are the climate devices you want to control assigned to the same areas as the thermometers that you want to base their behavior on?

It would be helpful if you would share your current automation so we can see how the parts are currently related.

That’s the action part in my automation at the moment:

- choose:
    # Temp min = room 1
    - conditions: 
      - "{{ state_attr('sensor.temperatura_minima_daikin', 'min_entity_id') == 'sensor.temperatura_pt' }}"
      # Temperatura soggiorno < 19°C
      - condition: numeric_state
        entity_id: sensor.temperatura_pt
        below: '19'
      # PDC non già accesa
      - condition: state
        entity_id: climate.daikin_soggiorno
        state: 'off'
      sequence:
      - service: script.turn_on
        target:
          entity_id: script.accensione_pdc_soggiorno
    # Temp min = room 2
    - conditions: 
      - "{{ state_attr('sensor.temperatura_minima_daikin', 'min_entity_id') == 'sensor.temperatura_camera_2' }}"
      # Temperatura camera < 18.5°C
      - condition: numeric_state
        entity_id: sensor.temperatura_camera_2
        below: '18.5'
      # PDC non già accesa
      - condition: state
        entity_id: climate.daikin_camera_2
        state: 'off'
      sequence:
      - service: script.turn_on
        target:
          entity_id: script.accensione_pdc_camera_2
    # Temp min = room 3
    - conditions: 
      - "{{ state_attr('sensor.temperatura_minima_daikin', 'min_entity_id') == 'sensor.temperatura_camera_3' }}"
      # Temperatura camera < 18.5°C
      - condition: numeric_state
        entity_id: sensor.temperatura_camera_3
        below: '18.5'
      # PDC non già accesa
      - condition: state
        entity_id: climate.daikin_camera_3
        state: 'off'
      sequence:
      - service: script.turn_on
        target:
          entity_id: script.accensione_pdc_camera_3
    # Temp minima = camera 4
    - conditions: 
      - "{{ state_attr('sensor.temperatura_minima_daikin', 'min_entity_id') == 'sensor.temperatura_camera_4' }}"
      # Temperatura camera < 18.5°C
      - condition: numeric_state
        entity_id: sensor.temperatura_camera_4
        below: '18.5'
      # PDC non già accesa
      - condition: state
        entity_id: climate.daikin_camera_4
        state: 'off'
      sequence:
      - service: script.turn_on
        target:
          entity_id: script.accensione_pdc_camera_4
    # Temp minima = camera 5
    - conditions: 
      - "{{ state_attr('sensor.temperatura_minima_daikin', 'min_entity_id') == 'sensor.temperatura_camera_5' }}"
      # Temperatura camera < 18.5°C
      - condition: numeric_state
        entity_id: sensor.temperatura_camera_5
        below: '18.5'
      # PDC non già accesa
      - condition: state
        entity_id: climate.daikin_camera_5
        state: 'off'
      sequence:
      - service: script.turn_on
        target:
          entity_id: script.accensione_pdc_camera_5

Where sensor.temperatura_minima_daikin is a min sensor between the 5 temperature sensors

I think the way to do this that is least complicated would be to set up a list of dictionaries that link the necessary information. In your automation, that could be something like:

action:
  - variables:
      d: >-
        {{ [ {'id': 'soggiorno',
        'state': states('climate.daikin_soggiorno'),
        'temp': states('sensor.temperatura_pt') | float(100)},
        {'id': 'camera_2',
        'state': states('climate.daikin_camera_2'),
        'temp': states('sensor.temperatura_camera_2') | float(100)},
        {'id': 'camera_3',
        'state': states('climate.daikin_camera_3'),
        'temp': states('sensor.temperatura_camera_3')|float(100)},
        {'id': 'camera_4',
        'state': states('climate.daikin_camera_4'),
        'temp': states('sensor.temperatura_camera_4')|float(100)},
        {'id': 'camera_5',
        'state': states('climate.daikin_camera_5'),
        'temp': states('sensor.temperatura_camera_5')|float(100)} ] }}
      lowest_id: >-
        {{ (d | selectattr('state', 'eq', 'off') | sort(attribute = 'temp') | first)['id'] }}
      lowest_temp: >-
        {{ (d | selectattr('state', 'eq', 'off') | sort(attribute = 'temp') | first)['temp'] }}
  - condition: template
    value_template: >-
      {% if lowest_id == 'soggiorno' %}
        {{ lowest_temp < 19 }}
      {% else %}
        {{ lowest_temp < 18.5 }}
      {% endif %}
  - service: script.accensione_pdc_{{lowest_id}}
1 Like

Amazing, thanks, i can finally do exactly what i wanted to do with my automation!