Automation with math between 2 sensors

I am trying setup an automation to turn on a fan in my greenhouse. I have 2 sensors: sensor.greenhouse_inside_temperature and sensor.outside_temperature. I would like to turn on the fan if all of the following requirements are met:

  1. Fan isn’t already turned on
  2. Greenhouse temp is over 80 degrees F
  3. Inside temp of the greenhouse is > 5 degrees hotter than outside the greenhouse.

I am new to HA so I’m not sure how to setup bullet point 3. Do I use a condition template? script?

Here is what I have so far:

  - alias: Turn fan on when greenhouse is too hot
    trigger:
    - platform: numeric_state
      entity_id: sensor.greenhouse_inside_temperature
      above: 80
    condition:
    - condition: state
      entity_id: switch.greenhouse_fan
      state: 'off'
      -condition: template
      value_template: '{{ sensor.greenhouse_inside_temperature > sensor.greenhouse_outside_temperate + 5}}'
    action:
      service: homeassistant.turn_on
      entity_id: switch.greenhouse_fan

Thanks is advance!

The condition part seems off, I think should be something like

condition:
  condition: and
  conditions:
    - condition: state
      entity_id: switch.greenhouse_fan
      state: 'off'
    - condition: template
      value_template: '{{ sensor.greenhouse_inside_temperature > sensor.greenhouse_outside_temperate + 5}}'

Not sure about the template condition cause I’m not using it, looks good though

1 Like

I still can’t seem to make this work. Am I supposed to call the sensors like I am in the “value_template” section? In others examples, I see some people using ‘state.attribute’. I just can’t seem to find any documentation around using sensor data in the value_template section.

Here is what I currently have:

- alias: Turn fan on when greenhouse is too hot
  trigger:
  - platform: numeric_state
    entity_id: sensor.greenhouse_inside_temperature
    above: 80
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: switch.greenhouse_fan
        state: 'off'
      - condition: template
        value_template: '{{ sensor.greenhouse_inside_temperature > sensor.greenhouse_outside_temperate + 5}}'
  action:
    service: homeassistant.turn_on
    entity_id: switch.greenhouse_fan
1 Like

Just to help isolate this, if I remove the 2 lines of code for the condition template and value_template, the automation executes fine and the fan is turned on when the temp is > 80. So something is wrong with the condition template.

Perhaps changing:
sensor.greenhouse_inside_temperature

To:
sensor.greenhouse_inside_temperature.state (Assuming it has a state)

Also you may have to convert them to float, but possibly not.
float(sensor.greenhouse_inside_temperature.state)

I meant to come back and update this, but yes, I had to do exactly that. This is what my code looked like:

- condition: template
        value_template: "{{ states.sensor.greenhouse_inside_temperature.state | float - states.sensor.outside_temperature.state | float > 5 }}"

Hope this helps anyone in the future.

2 Likes

Glad it worked for you. :slight_smile:

very good :heart_eyes:

It is possible to get values from the variables and then use it to set passwords?

I mean, it is possible to get a code from “input_text component” and then use it to set http password or alarm code in the “manual alarm component”?