Compare sensor values and start automation

Hello together,

I have several things which I want to do:

First, I want to compare two sensor values (abs. humidity, calculated it in a template sensor). One inside and one outside. If the abs. humidity inside is much bigger than the abs. humidity inside, I want to start a fan (via PWM) for a certain time.
As I am new to HA, I would like to ask you which is the smartest way to realize this operation.

Second, I want the fan also to start if somebody opens the garage door (recognized via reed sensor) and closes it afterwards (to drive a car into the garage or something like that). The problem here is that I don’t want the fan to start every time when someone, for example, opens the garage door 4 times in 10 minutes. So how can I add a specific time in which the automation won’t start again?

Third: Is it possible to put the PWM for example to 30% between 10 pm and 7 am and for the rest of the day to 80% power?

A lot of questions, I know. Hope you understand my English.

Thanks!

1 Like

All of those things are possible. Have you connected the fan to Home Assistant? The way Home Assistant communicates with the fan controller will determine how you need to design the actions of your automation.

Yes, i connected the fan to PWM using an ESP.

I now tried to solve my project for comparing the sensors, but i every time get an error.

automation:
  trigger:
    - platform: numeric_state
        value_template: >-
          {% set a, b = states('sensor.abs_feu_aussen') | float, states('sensor.abs_feu_g1') | float %}
          {{ a - b }}
        below: -2
  action:
    - service: fan.set_percentage
      target:
        entity_id: fan.garagenlufter
      data_template: 
        percentage: >-
        {% if now().hour in [22, 23, 0, 1, 2, 3, 4, 5, 6] %}
        40
        {% else %}
        100
        {% endif %}

Error: Message malformed: expected dictionary @ data[‘action’][0]
I just don’t know why… Can you help me?

1 Like

Well, that error is telling you that there is something wrong with the first action. I believe it is supposed to just be data, not data_template.

along with changing “data_template” to “data” you also need to indent your template two spaces:

  action:
    - service: fan.set_percentage
      target:
        entity_id: fan.garagenlufter
      data: 
        percentage: >-
          {% if now().hour in [22, 23, 0, 1, 2, 3, 4, 5, 6] %}
            40
          {% else %}
            100
          {% endif %}

Thank you guys. I got it now implemented without an error. I will check at the weekend whether it works as it should.

One thing which I just noticed: The automation turns the fan on. Does it turn the fan off again when the trigger condition isn’t true anymore?

no. you need another automation for that.

or you can incorporate trhe new trigger into the existing automation with a different action.

Ok. I’ll have a look on that tomorrow.

Is there a possibility to declare in the action “turn the fan on for 10 minutes and then turn it off”? (Turning on and off is clear to me, just the question if there is a function to declare the 10 minutes duration)

yes. there is a “delay:” option you can use in the action.

There are indentation errors. Correct indentation is important.

Try this version:

automation:
- alias: example1
  trigger:
    - platform: template
      value_template: "{{ (states('sensor.abs_feu_aussen') | float(0) - states('sensor.abs_feu_g1') | float(0)) < -2 }}"
  action:
    - service: fan.set_percentage
      target:
        entity_id: fan.garagenlufter
      data: 
        percentage: "{{ 40 if now().hour in [22, 23, 0, 1, 2, 3, 4, 5, 6] else 100 }}"
1 Like

All in all, this is the final concept:

- id: '1636642946011'
  alias: LĂźfter Garage Neu
  description: ''
  trigger:
    - platform: template
      value_template: "{{ (states('sensor.abs_feu_aussen') | float(0) - states('sensor.abs_feu_g1') | float(0)) < -2 }}"
    - platform: state
      entity_id: binary_sensor.garagentor
      from: "off"
      to: "on"
      for: "00:15:00"
  condition:
    - condition: numeric_state
      entity_id: sensor.rel_luftfeuchtigkeit
      above: 20
  action:
    - service: fan.set_percentage
      target:
        entity_id: fan.garagenlufter
      data:
        percentage: "{{ 40 if now().hour in [22, 23, 0, 1, 2, 3, 4, 5, 6] else 80 }}"
    - delay: "00:15:00"
    - service: fan.set_percentage
      target:
        entity_id: fan.garagenlufter
      data:
        percentage: 0
  mode: single

Hope it works! Maybe I place the second trigger in a second automation because I don’t want the condition for this case.

In conclusion, everything works as expected. Thank you guys!
Only my aht10 sensor gives some temperature values outside aren’t that accurate. Just have to figure out whether it’s the positioning or the sensor itself.

Thank you :slight_smile: