Bathroom Automation

Hi There,

I am trying to create an automation / script that:

If the door sensor state is closed, Turn on the light if the humidity rises 2% or more within 3 minutes if fan (light) is already on, keep on while humidity is higher than 80% but not exceeding 1h off running time. if the fan (light) is on for 1 hour, it will sleep for 1 hour before turning it on again.

also, it will turn on the fan (light) for 10 minutes if the door sensor is closed for 5 minutes even if the humidity doesn’t change

This automation will never excide the fan (light) run time more than 1 hour

  • apologies my fan entity is set as “light”

I keep getting the error below, and not sure why. can someone please help?

Message malformed: Unable to determine action @ data[‘action’][0]

Automation:

description: "Guest Bathroom"
mode: single

  # Define a timer helper to manage timing conditions

  - alias: "Control Light based on Humidity and Door Sensor"
    trigger:
      platform: state
      entity_id: binary_sensor.sonoff_a440020f37
      to: "on"  # Trigger when the door sensor state is "on" (closed)
    action:
      - service: script.control_light_based_on_humidity_and_door_sensor

  - alias: "Turn On Light when Humidity Rises"
    trigger:
      platform: numeric_state
      entity_id: sensor.sonoff_a440024468_humidity
      above: 80
      for: "00:03:00"
    condition:
      - condition: state
        entity_id: binary_sensor.sonoff_a440020f37
        state: "on"
    action:
      - service: script.turn_on_light_when_humidity_rises

  - alias: "Turn Off Light after 1 Hour"
    trigger:
      platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.timer
    action:
      - service: switch.turn_off
        entity_id: switch.light.sonoff_a4400243d5

  - alias: "Sleep for 1 Hour after Turning Off Light"
    trigger:
      platform: state
      entity_id: switch.light.sonoff_a4400243d5
      to: "off"
      for: "01:00:00"
    action:
      - service: timer.start
        data:
          entity_id: timer.timer

  - alias: "Turn On Light for 10 Minutes when Door Closed for 5 Minutes"
    trigger:
      platform: state
      entity_id: binary_sensor.sonoff_a440020f37
      to: "on"
      for: "00:05:00"
    action:
      - service: script.turn_on_light_for_10_minutes_when_door_closed_for_5_minutes

Script:

script:
  control_light_based_on_humidity_and_door_sensor:
    sequence:
      - condition: numeric_state
        entity_id: sensor.sonoff_a440024468_humidity
        above: 80
      - service: switch.turn_on
        entity_id: switch.light.sonoff_a4400243d5
      - delay: '00:10:00'
      - service: switch.turn_off
        entity_id: switch.light.sonoff_a4400243d5

  turn_on_light_when_humidity_rises:
    sequence:
      - condition: state
        entity_id: binary_sensor.sonoff_a440020f37
        state: "on"
      - service: switch.turn_on
        entity_id: switch.light.sonoff_a4400243d5
      - service: timer.start
        data:
          entity_id: timer.timer

  turn_on_light_for_10_minutes_when_door_closed_for_5_minutes:
    sequence:
      - service: switch.turn_on
        entity_id: switch.light.sonoff_a4400243d5
      - delay: '00:10:00'
      - service: switch.turn_off
        entity_id: switch.light.sonoff_a4400243d5

An automation can only have one trigger section and one action section.

your automation has multiple of each section.

you will need to rethink your logic so it can all be done in one automation with multiple triggers under the trigger section and then use “choose:” to determine which actions are run based on which trigger fired the automation or use multiple automations with different triggers and actions for each.

EDIT:

on second look it looks like you just need to remove the:

description: "Guest Bathroom"
mode: single

from the top and then you will have multiple automations already. And you may need to adjust your indentation as well.

thank you. I’ve changed a few things around. (not tested yet). but no error:

alias: New automation
description: Light Control with Door Sensor and Humidity
trigger:
  - platform: state
    entity_id: binary_sensor.sonoff_a440020f37
    to: "on"
condition:
  - condition: template
    value_template: >-
      {{ state_attr('sensor.sonoff_a440024468_humidity', 'last_triggered_value')
      | float + 2 <= state('sensor.sonoff_a440024468_humidity') }}
  - condition: numeric_state
    entity_id: sensor.sonoff_a440024468_humidity
    above: 80
  - condition: or
    conditions:
      - condition: state
        entity_id: light.sonoff_a4400243d5
        state: "off"
      - condition: template
        value_template: >-
          {{ (now().timestamp() - state_attr('switch.light.sonoff_a4400243d5',
          'last_changed').timestamp()) > 3600 }}
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: light.sonoff_a4400243d5
            state: "off"
        sequence:
          - service: switch.turn_on
            entity_id: light.sonoff_a4400243d5
          - service: timer.start
            entity_id: timer.timer
            data:
              duration: "01:00:00"
      - conditions:
          - condition: state
            entity_id: light.sonoff_a4400243d5
            state: "on"
          - condition: numeric_state
            entity_id: sensor.sonoff_a440024468_humidity
            above: 80
          - condition: template
            value_template: >-
              {{ (now().timestamp() -
              state_attr('switch.light.sonoff_a4400243d5',
              'last_changed').timestamp()) <= 3600 }}
        sequence:
          - service: switch.turn_on
            entity_id: light.sonoff_a4400243d5
  - service: switch.turn_off
    entity_id: light.sonoff_a4400243d5
  - delay:
      hours: 1
mode: single

1 Like