Trouble defining a temperature automation

Hello,
I’m having trouble defining an automation that, as the temperature of a sensor changes, does two different things depending on the temperature reached. in practice I would like that:

  • when the sensor temperature is <= 17, a scene is executed
  • when the sensor temperature is >= 20, a climate is set off (state: off).

The problem I’m encountering is to create a single IF … EIif … EndIf
at Action level that is able to activate the scene in one case and switch off the climate in the other.
Can you help me? Thanks in advance.

post what you have tried but is failing…properly formatted please.

You should also respond to @finity. What you may be after is the “choose” action for your automation.

Here’s an example where my air-conditioner automation determines whether to heat or cool based on the temperature and solar production - in my case it runs a script. You could tailor this to your own case.

action:
  - choose:
      - conditions:
          - condition: numeric_state
            above: '26'
            entity_id: sensor.sydney_observatory_hill_temp_feels_like
          - condition: numeric_state
            entity_id: sensor.produced_power_average_15min
            above: '2'
        sequence:
          - service: script.hvac_cooling_defaults
      - conditions:
          - condition: numeric_state
            entity_id: sensor.bom_fc_temp_max_0
            below: '18'
          - condition: numeric_state
            entity_id: sensor.produced_power_average_15min
            above: '1'
        sequence:
          - service: script.hvac_heating_defaults
1 Like

Hello all,

I had thought of a code like this:

alias: Accensione automatica
trigger:
  - platform: numeric_state
    entity_id: sensor.valvola_letto_current_temp
    below: 20
action:
  - service_template: >
      {% set temp = states('sensor.valvola_letto_current_temp')|float %}
      {% if temp <= 17.0 %}
        service: scene.turn_on
        entity_id: scene.split_corridoio_attiva_caldo
      {% elif temp >= 18 %}
        entity_id: climate.split_corridoio
        state: 'off'
      {% endif %}

but obviously, being newbie in HA, even if the logic is correct the coding is wrong. The problem is having to execute both a scene and a service in a single IF, and the service_template: is certainly wrong, but I don’t know how to fix it.

you can’t use multiple lines inside the service call like that.

as suggested above you will need to use a “choose”.

action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ states('sensor.valvola_letto_current_temp') | float<= 17.0 }}"
        sequence:
          - service: scene.turn_on
            entity_id: scene.split_corridoio_attiva_caldo
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ states('sensor.valvola_letto_current_temp') | float >17.0 }}"
        sequence: 
          - service: climate.set_mode
            entity_id: climate.split_corridoio
            state: 'off'

I don’t use any climate devices so you will need to figure out the correct requirements for the second service call for that section.

Hello @finity ,
thank you for this solution and thanks to @teda-230 for the previous one. I did not know the choose: opttion and it gives me the impression of being equivalent to a differently coded IF. What I don’t understand is why in your code there are two choices: while in the @teda-230 example there is only one. Surely both structures work, but on a logical level I would consider @teda-230 structure more correct, or am I wrong? Anyway thanks to both of you for your assistance and for your solutions.

EDIT: I changed the last line with
hvac_mode: 'off'
and tried it, but it reported this error:

Invalid config for [automation]: [hvac_mode] is an invalid option for [automation]. Check: automation->action->0->choose->1->sequence->0->hvac_mode.

I’m sure that hvac_mode is correct because it works in other automations:

device_id: 10021f731b63e11325844bdbb465a639
domain: climate
entity_id: climate.split_corridoio
type: set_hvac_mode
hvac_mode: 'off'

What could be the problem?

Because I’m dumb… :wink:

Nope, sorry. just a typo. You only need one.

I don’t know really. I don’t use any climate stuff so I don’t know what is acceptable.

I guess it could be an indentation problem tho.

It seems that it is from the error but without seeing the end result it’s impossible to know for sure.

Hi,

Try this as your action.

  - service: climate.set_hvac_mode
    data:
      hvac_mode: 'off'
    target:
      device_id: 10021f731b63e11325844bdbb465a639