Automation not functioning correctly

I have an electric heater in a garden shed.
The heater is plugged into a Tuya Wifi plug.
I have a sonoff temperature sensor in the shed.
I have a sonoff door sensor on the shed door (detects door state as open or closed).

I want the heater on if the temperature is less than 5 C AND the door is closed.
I want the heater off if the temperature is greater than 5 C OR the door is open.

My automation partially works. Sometimes the heater remains on when it should be off. Sometimes the heater remains off when it should be on.

I have two automations.
Automation A:
Trigger:
Temp above 5C
Door open
Condition:
None
Action:
Turn off heater

Automation B:
Trigger:
Temp less than 5 C
Condition:
Door is closed
Action:
Turn on heater

Please advise fix that is needed
Thanks

You need to post your YAML code to get a good answer. Use the </> option for preformatted text.

66Quotes99,

Like this (pseudo code)?

Automation A:
Trigger:
  Temp above 5C
Condition:
  None
Action:
  IF door open
   turn heater off

Automation B:
Trigger:
  Temp less than 5 C
Condition:
  None
Action:
  If door closed
    turn on heater
  else
    turn off heater```

No, the actual YAML of your current automation.

It is available through the Automation Editor under the breadcrumbs menu at the top right by selecting the “Edit in YAML” option.

Based on what you have already posted, the most likely issue is that you need to add triggers for the door.

1 Like

Yea, I should lead by example. . . but thought the logic was the question, not the grammar . [g]

alias: test
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.boboweather_feels_like
    above: 5
conditions: []
actions:
  - if:
      - condition: state
        entity_id: binary_sensor.garage_door_contact
        state: "open"
    then:
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: switch.radiator_outlet
mode: single

alias: test2
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.boboweather_feels_like
    below: 5
conditions: []
actions:
  - if:
      - condition: state
        entity_id: binary_sensor.garage_door_contact
        state: "closed"
    then:
      - action: switch.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: switch.radiator_outlet
    else:
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: switch.radiator_outlet
mode: single

It’s neither. As a first problem, a binary sensor’s state is “on” or “off”, not “open” or “closed”. And that error wouldn’t have been apparent in pseudocode.

Yep, you’re right.

Though, I don’t think your YAML accomplishes the above. Going on those two sentences alone, I think what you want is:

alias: test
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.boboweather_feels_like
    above: 5
  - trigger: state
    entity_id:
      - binary_sensor.garage_door_contact
    to: "off"
    from: "on"
conditions: []
actions:
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: switch.radiator_outlet
mode: single
alias: test2
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.boboweather_feels_like
    below: 5
  - trigger: state
    entity_id:
      - binary_sensor.garage_door_contact
    to: "on"
    from: "off"
    # consider a for: so that you don't toggle the heater twice when entering or exiting.
conditions:
  - condition: numeric_state
    entity_id: sensor.boboweather_feels_like
    below: 5
  - condition: state
    entity_id: binary_sensor.garage_door_contact
    state: "on"
actions:
      - action: switch.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: switch.radiator_outlet
mode: single

My code

alias: Shed light off when door is open or temp > 10 C
description: Shed light off when door is open or temp > 10 C
triggers:
  - type: temperature
    device_id: 34
    entity_id: 04
    domain: sensor
    trigger: device
    above: 10
    for:
      hours: 0
      minutes: 0
      seconds: 5
  - type: opened
    device_id: 20
    entity_id: a1
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 0
      seconds: 5
conditions: []
actions:
  - type: turn_off
    device_id: 7e
    entity_id: f6
    domain: switch
mode: restart

My code

alias: Shed light on when door is closed and temp < 10 C
description: Shed light on when door is closed and temp < 10 C
triggers:
  - type: not_opened
    device_id: 20
    entity_id: a1
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 0
      seconds: 1
conditions:
  - condition: numeric_state
    entity_id: sensor.th_disc_shed_snzb_02p_temperature_2
    below: 10
actions:
  - type: turn_on
    device_id: 7e
    entity_id: f6
    domain: switch
mode: restart

Sort of. I’m trying to get my head around YAML. I’m beginning to think YAML is event driven rather than state. I’ve programmed in C and Visual Basic years ago so YAML is new to me.

Hello 66Quotes99,

YAML is the interface to HA and isn’t really a language. Script ing that allows you to talk to the core more like.
Automation triggers are all events in one form or another. The state has to change and that event if it matches the requirements triggers the rest to run.
The state doesn’t matter unless a change it makes matches the trigger pattern, and then it fires.
So yes, you are getting the idea.

1 Like

Door sensor = SNZB-04P
Temp sensor = SNZB-02P
Smart plug = Tuya

The penny has dropped and I understand how to create a Home Assistant Automation, this code works. 4 x Automations

alias: Shed, if door closes and T < 10 C, turn on heater
description: ""
triggers:
  - type: not_opened
    device_id: 2b
    entity_id: a60
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 0
      seconds: 5
conditions:
  - type: is_temperature
    condition: device
    device_id: 32
    entity_id: 03e
    domain: sensor
    below: 10
actions:
  - type: turn_on
    device_id: 7c9
    entity_id: f664f
    domain: switch
mode: single

alias: Shed, if door opens, turn heater off
description: Shed, if door opens, turn heater off
triggers:
  - type: opened
    device_id: 2bd
    entity_id: a60e
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 0
      seconds: 5
conditions: []
actions:
  - type: turn_off
    device_id: 7c9
    entity_id: f664
    domain: switch
mode: single

alias: Shed, if temp drops below 10 C AND door = closed, turn on heater
description: Shed, if temp drops below 10 C AND door = closed, turn on heater
triggers:
  - type: temperature
    device_id: 323
    entity_id: 03
    domain: sensor
    trigger: device
    below: 10
    for:
      hours: 0
      minutes: 0
      seconds: 5
conditions:
  - type: is_not_open
    condition: device
    device_id: 2bd
    entity_id: a60
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 5
actions:
  - type: turn_on
    device_id: 7c9
    entity_id: f664f
    domain: switch
mode: single

alias: Shed, if temp exceeds 10 C, turn heater off
description: Shed, if temp exceeds 10 C, turn heater off
triggers:
  - type: temperature
    device_id: 323
    entity_id: 03e
    domain: sensor
    trigger: device
    above: 10
    for:
      hours: 0
      minutes: 0
      seconds: 5
conditions: []
actions:
  - type: turn_off
    device_id: 7c9
    entity_id: f66
    domain: switch
mode: single
1 Like