(SOLVED) Switch on/off depending on binary sensor and temperature

Hi everybody,

I am fairly new to home assistant (started out a few days ago with no previous knowledge), but I already like it that much that I am planning on replacing my current node-red in combination with ioBroker setup with hass.

I wrote the following automations/livingroom.yaml trying to

  • check whether the living room door has been opened
  • if so, check whether or not it is warmer than 18 °C at home
  • if so, turn on living room fan

The entire automation is supposed to also turn off the fan once the door has been closed (regardless of temperature value), but I wanted to get this working first.

This is my script

# Ventilator ein/aus
- id: '1564142473150'
  alias: "Ventilator ein/aus"
  trigger:
    - entity_id: binary_sensor.wohnzimmer_tur
      from: 'false'
      to: 'true'
      platform: state
  condition:
    - above: 18
      condition: numeric_state
      entity_id: weather.home
  action:
    - alias: 'Ventilator an'
      service: switch.turn_on
      entity_id: switch.ventilator

When I go to development tools => states (or similar; I am using German UI, but I’m sure you’ll know what I mean…), I can see the current living room door state ("{{ states.binary_sensor.wohnzimmer_tur.state }}") however, when I create this automation via webGUI, it only chooses binary_sensor.wohnzimmer_tur (I assume this is because I am grabbing the inital status anyway instead of other attributes…?).

I assumed that this would watch the door status changing from ‘false’ (which is the isOpen state) to ‘true’, and then continue. It is currently over 30°C here and hass displays this accordingly. However, I still cannot get the fan to turn on upon opening the living room door.

What do I need to change to fix this? Playing trial and error is a big difficult because hass requires a restart whenever the config has been changed, so I’m hoping to find some help here. I am just a hobbyist with no professional background in programming; please keep that in mind if this is a stupid question… I have tried multiple attempts but just can’t figure out what those lines need to be.

And one last question: can I create something like an “anti-automation”? By this I mean that the above automation will turn ON the fan if it is above 18°C and the door opens. Do I need to create another automation for the opposite (door closes or temperature below 18°C), or can I somehow define that “if this and if this2, then do that; if not this and/or if not this2, then do opposite”?

Thanks in advance for your help :slight_smile:

No matter what is displayed in the front end binary sensor states are either on or off.

  trigger:
    - entity_id: binary_sensor.wohnzimmer_tur
      from: 'off'
      to: 'on'
      platform: state

For turning off (multiple triggers are OR by default):

  trigger:
    - platform: state
      entity_id: binary_sensor.wohnzimmer_tur
      from: 'on'
      to: 'off'
    - platform: numeric_state
      entity_id: weather.home
      below: 18
  action:
    - service: switch.turn_off
      entity_id: switch.ventilator
1 Like

Also it might be worth removing the ‘from’ clause in the trigger and just using the ‘to’ clause

1 Like

Thank you both. It works now the way @tom_l quoted and it was also fine to remove from: from the file :slight_smile: