Automation humidity above turn on light

I’m trying to make an automation when humidity (in bathroom) exceeds 75% a bed lamp will turn on. This is my idea to prepare bed light after shower.

I’ve been trying many solutions, but to no avail.

My sensor is: sensor.mi_h_bathroom (it’s from Xiaomi - LYWSD03MMC)
From what I’ve checked it is a string, so conversion needs to happen.
My light is: light.bed_lamp (IKEA tradfri GU10)

This works properly, returning true when it’s above 75. Tested on templates and I was setting state manually in states to emulate and try to get automation to work

{% if states.sensor.mi_h_bathroom.state | int > 75 %} true {% endif %}

I’ve tried with platform: numeric_state, but after reading forums it turned out when I checked, the sensor returns string, so this is probably why it didn’t work.

Here’s my automation.yaml with a few from my attempts:

# fist try
- id: x_2
  alias: x2
  initial_state: True
  trigger:
    - platform: template
      value_template: "{% if states.sensor.mi_h_bathroom.state | int > 75 %} true {% endif %}"
  action:
    service: light.turn_on
    data:
      entity_id: 
        - light.bed_lamp
       
# second try
- id: x_5
  alias: x5
  initial_state: True
  trigger:
    - platform: template
      value_template: "{% if states.sensor.mi_h_bathroom.state | int > 75 %} true {% endif %}"
  action: #this action template I took from other automation from GUI to turn on the lamp
  - type: turn_on
    device_id: 9720f5b4122e11ebb585299a11b0ecff 
    entity_id: light.bed_lamp
    domain: light
    brightness_pct: 30

# third try
- id: x_1
  alias: x1
  initial_state: True
  trigger:
    - platform: numeric_state
      entity_id: sensor.mi_h_bathroom
      above: '90'
  action:
  - type: turn_on
    device_id: 9720f5b4122e11ebb585299a11b0ecff
    entity_id: light.bed_lamp
    domain: light
    brightness_pct: 30

# fourth try
- id: x_3
  alias: x3
  initial_state: True
  trigger:
    - platform: numeric_state
      entity_id: sensor.mi_h_bathroom
      above: 90
  action:
  - type: turn_on
    device_id: 9720f5b4122e11ebb585299a11b0ecff
    entity_id: light.bed_lamp
    domain: light
    brightness_pct: 30
- alias: Light on when sensor over 75
  trigger:
    platform: numeric_state
    entity_id: sensor.mi_h_bathroom
    above: 75
  action:
    service: light.turn_on
    data:
      entity_id: light.bed_lamp_kamil_light
      brightness_pct: 30

You probably want a time condition in there to make sure that it’s bedtime.

1 Like

Thanks for you input. I will check that soon. Am I right in thinking, that the automation should work even when I set the state manually through “States”?

Actually, I image this automation to be a bedtime, so I would like in the future to add time condition to it, which should be simple enough, and probably a counter so that it won’t be lighting the lamp up again after let’s say 1h, when I’m asleep and the humidity is still above 75.

Yeah, setting the state manually will work. The sensor will return to its correct state on its next update. Remember that for the automation to trigger the number must go from below 75 to above 75.

1 Like

Thanks, tested and it works perfectly. Applying conditions now. Do you know if I can simulate time for the condition? For example set the current time to 15:00 for the purpose of test and how to set it back to the default one? Also, how can I check the time entity current value?

Not really, but if you code the condition properly you really don’t need to test it

- alias: Light on when sensor over 75
  trigger:
    platform: numeric_state
    entity_id: sensor.mi_h_bathroom
    above: 75
  condition:
    condition: time
    after: '21:00:00'
    before: '23:00:00'
  action:
    service: light.turn_on
    data:
      entity_id: light.bed_lamp_kamil_light
      brightness_pct: 30

(change the times to suit)

2 Likes

Just curious, do you have any kind of generator for the code, or do you write it manually? I’m a beginner, trying to learn :slight_smile:

I write it all manually. I do it on my phone so much that it auto-predicts most of the structure for me nowadays.

1 Like

Might be better to toggle the automation when you turn off the bathroom light after bedtime instead of rely on room humidity.

Thanks for your suggestion, but for now I don’t have smart lights in my bathroom and also toggling the lights doesn’t neccesarily mean I am taking a bath/shower, since my bedtime varies.

For future readers, I’m sharing my working automation.
The way it works: if humidity sensor in bahtroom exceeds 75% and it’s between 20:30 - 01:00 (shower time) and also the lamp has been off for at least 1 hour, turn on the lamp at 25% brightness

- alias: bed lamp on after shower
  trigger:
    platform: numeric_state
    entity_id: sensor.mi_humidity_bathroom
    above: 75
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: light.bed_lamp_light
      state: 'off'
      for:
        hours: 1
    - condition: time
      after: '20:30:00'
      before: '01:00:00'
  action:
    service: light.turn_on
    data:
      entity_id: light.bed_lamp_light
      brightness_pct: 25
1 Like