I used ChatGPT to help extend an automation in esphome

I have a register booster fan, and I only wrote the automation to take into account turning on/off during/after heating

when the rate of change of the average temperature of the house is increasing, it turns on the fan if it has been off for five minutes, and shuts it off when the rate of change is <0 and has been on for 5 minutes.

ChatGPT helped me through this behemoth of a yaml logic statement to account for cooling also after a bit of hand holding. Not really tested yet, but I think it’s going to work.

binary_sensor:
  - platform: template
    name: "Temperature Comparison"
    id: tempcompare
    lambda: |-
      return (id(average_temperature_24h).state > id(outside_temperature).state);

sensor:
  - platform: homeassistant
    name: "Average Temperature (24h)"
    entity_id: sensor.average_temperature_24h
    id: average_temperature_24h

  - platform: homeassistant
    name: "Weather Home"
    entity_id: sensor.weather_home
    id: outside_temperature

  - platform: homeassistant
    name: "Bedroom Temperature Derivative"
    id: temperaturederivative
    entity_id: sensor.average_temperature_derivative_2_min
    on_value_range:
      - below: 0
        then:
          if:
            condition:
              and:
                - binary_sensor.is_on: tempcompare
                - for:
                    time: 5min
                    condition:
                      switch.is_off: relay
            then:
              - switch.turn_on: relay
            else:
              if:
                condition:
                  and:
                    - binary_sensor.is_off: tempcompare
                    - for:
                        time: 5min
                        condition:
                          switch.is_on: relay
                then:
                  - switch.turn_off: relay
      - above: 0
        then:
          if:
            condition:
              and:
                - binary_sensor.is_on: tempcompare
                - for:
                    time: 5min
                    condition:
                      switch.is_off: relay
            then:
              - switch.turn_on: relay
            else:
              if:
                condition:
                  and:
                    - binary_sensor.is_off: tempcompare
                    - for:
                        time: 5min
                        condition:
                          switch.is_on: relay
                then:
                  - switch.turn_off: relay


for the record my previous automation was as follows:

  # - platform: homeassistant
  #   name: "Bedroom Temperature Derivative"
  #   id: temperaturederivative
  #   entity_id: sensor.average_temperature_derivative_2_min
  #   on_value_range:
  #     - below: 0
  #       then:
  #         if:
  #           condition:
  #             for:
  #               time: 5min
  #               condition:
  #                 switch.is_on: relay
  #           then:
  #             - switch.turn_off: relay
  #     - above: 0
  #       then:
  #         if:
  #           condition:
  #             for:
  #               time: 5min
  #               condition:
  #                 switch.is_off: relay
  #           then:
  #             - switch.turn_on: relay

made some manual changes to logic after the fact, but even just helping with the nested conditional in formatting alone was a major help.

6 Likes

Yeah I had a go too with some simple stuff:

Daryl 
esphome code to read dallas sensor on pin 3

To read a Dallas temperature sensor using esphome, you will need to connect the sensor to a digital input pin on your ESP device. You can then use the following code in your esphome configuration to read the temperature from the sensor:

Copy code
esphome:
  name: my_sensor
  platform: ESP8266

sensor:
  - platform: dallas
    pin: 3
    temperature:
      name: "Dallas Temperature"
      unit_of_measurement: "°C"
This code will create a sensor in esphome called "Dallas Temperature" that will report the temperature in degrees Celsius. The sensor will be connected to digital input pin 3 on your ESP device.

If you need further assistance, please let me know.




Daryl 
can you add a ssd1306 to display the temperature?

Yes, you can use an SSD1306 OLED display to display the temperature reading from the Dallas sensor. To do this, you will need to connect the OLED display to the I2C bus on your ESP device and add the following code to your esphome configuration:

Copy code
esphome:
  name: my_sensor
  platform: ESP8266

sensor:
  - platform: dallas
    pin: 3
    temperature:
      name: "Dallas Temperature"
      unit_of_measurement: "°C"

display:
  - platform: ssd1306
    reset_pin: D4
    sda: D3
    scl: D2

text_sensor:
  - platform: template
    name: "Temperature Display"
    lambda: |-
      return std::to_string(id(dallas_temperature).state);

output:
  - platform: ssd1306
    id: display
    display: text_sensor.Temperature_Display

Can’t think of anything difficult for it to try…

1 Like