Trigger switch based on temperature

Hello everyone,

I’m quite new to Home Assistant and ESPhome so apologize if something might be obvious to some of you :slight_smile:

I’m working with a m5stack switch connected with a qmp6988 temperature sensor that’s being controller by a thermostat card on the dashboard, I’m trying to make it that the switch goes on when some conditions are met like : the thermostat is off and the temperature is below a certain range.

So far I’ve worked with lambda to get the thermostat state, but I’m not quite sure how to get the actual temperature value so that the logic would work.

Here is an example of how I worked so far


esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

i2c:
  sda: 26
  scl: 32
  scan: true

switch:
  - platform: gpio
    id: status_switch
    name: "Relay"
    pin: 22 
   #pin: GPIO22
     
sensor:
  - platform: qmp6988
    temperature:
      name : "LAB 1 temperature"
      id: ENVIII
    pressure:
      name: "LAB 1 pressure"
      oversampling: 64x
    address: 0x70
    update_interval: 10s
    iir_filter: 2x

light:
  - platform: fastled_clockless
    chipset: WS2812B
    pin: 27
    num_leds: 1
    rgb_order: GRB
    id: status_led
    name:  Light
    effects:
      - random:
      - flicker:
      - addressable_rainbow:

binary_sensor:
  - platform: gpio
    pin:
      number: 39
      inverted: true
    name: Button
    on_press:
      then:
        - light.toggle: status_led
        - switch.toggle: status_switch

climate:
  - platform: thermostat
    visual:
      min_temperature: 18
      max_temperature: 25
      temperature_step: 0.1
    name: "Thermostat Climate Controller"
    id: myThermostat
    sensor: ENVIII
    min_heating_off_time: 300s
    min_heating_run_time: 300s
    min_idle_time: 1s
    heat_action:
      - switch.turn_on: status_switch
      - light.turn_off: status_led
    idle_action:
      - switch.turn_off: status_switch
 
    on_state: 
      - if:
        condition:
          - lambda: "return (id(myThermostat).mode = 0;" #get the mode selected from the card
          # temperature conditions
      then:
        heat_action:
          # trigger the switch, heat on for 5 mins then turn back off
          - switch.turn_on: status_switch
          - min_heating_run_time: 300s
          - switch_turn_off: status_switch

You’ll need to post us your sensor configs for starters. Also what’s with the double spacing in your yaml above?

Hi, thanks for the heads up

I’ve edited it so the whole code is in it, no clue what happened for the double spacing was fine to me when I posted it… anyway now it’s fixed :slight_smile:

1 Like

You might be better off putting an on_value_range on your temperature sensor to trigger your actions.

climate:
  - platform: thermostat
    visual:
      min_temperature: 18
      max_temperature: 25
      temperature_step: 0.1
    name: "Thermostat Climate Controller"
    id: myThermostat
    sensor: ENVIII
    min_heating_off_time: 300s
    min_heating_run_time: 300s
    min_idle_time: 1s
    heat_action:
      - switch.turn_on: status_switch
      - light.turn_off: status_led
    idle_action:
      - switch.turn_off: status_switch
 
    on_state: 
      - if:
        condition:
          - lambda: "return (id(myThermostat).mode = 0;" #get the mode selected from the card
          # temperature conditions
      then:
        heat_action:
          # trigger the switch, heat on for 5 mins then turn back off
          - switch.turn_on: status_switch
          - min_heating_run_time: 300s

You’ve defined a thermostat, so everything under on_state: is redundant.

The thermostat will react to the temperature from the sensor and simply perform the heat_action or idle_action based on your parameters.

It won’t do that if it is “off” though I think?

Probably a good time to clarify what outcomes/behaviours you want @zinc

Correct. You can set the on-off state and the target temperature from HA, or via Climate Automation actions:

I was thinking if @zinc does in fact want to turn it on if it is off and temp is below x then you could trigger a climate action to turn it on from a on_value_range on the temperature sensor (with a condition there to check it is off. But you may not even need that condition).

@Mahko_Mahko
I’ve tried to add the on_value_range in the sensor component, the switch does turn on and everything works, the issue is that on the thermostat card it show as idle, also by settings fixed values it create a situation when if the temperature is below the value I stated in the code, and I activate the heating mode on the card, the switch will turn off whenever the temperature reaches said cap.

To clarify my goal: I want to automate my thermostat so that whenever I leave my office (that imply I will set to idle the thermostat myself) it will have a constant temperature during the night.

@zoogara
On you last reply, by setting the on-off state you mean making the automation from the home assistant settings?

You should be able to use the built-in “away” preset to achieve what you want.

From what I can tell from your use case, you shouldn’t need to be doing any special code at all. Just configure a thermostat properly. Then it should all just work. Others have solved these problems and made it easy to set up.

I suggest you spend a bit more time getting familiar with the docs.

Thank you @Mahko_Mahko for the heads up, I’ll take a look :smiley:

1 Like