Humidity Sensor Automation not firing

I am trying to setup an automation that triggers turning on a bathroom fan when the humidity rise about the house humidity.

Here is the automation code:

- id: Master_Bathroom_Humidity_High
  alias: Master Bathroom Humidity High
  trigger:
  - platform: numeric_state
    entity_id: sensor.humidity_sensor_59
    value_template: '{{ sensor.humidity_sensor_59 - sensor.main_floor_humidity }}'
    above: 7
  action:
  - service: homeassistant.turn_on
    entity_id: light.bathroom_fan_67

- id: Master_Bathroom_Humidity_Normal
  alias: Master Bathroom Humidity Normal
  trigger:
  - platform: numeric_state
    entity_id: sensor.humidity_sensor_59
    value_template: '{{ sensor.humidity_sensor_59 - sensor.main_floor_humidity }}'
    below: 5
  action:
  - service: homeassistant.turn_off
    entity_id: light.bathroom_fan_67

I am getting the following error in the ha log file when it looks to be evaluting the automation from a trigger event.

Template error: UndefinedError: ‘sensor’ is undefined
helpers/condition.py (ERROR)

What am I missing?

In a template you either need to use states.domain.object.state or states('domain.object') to get the value of the entity’s state. Since you’re doing math, I’d recommend casting the state value to int or float (since typically the state is a string, even if it looks like a number.)

I’ve never used value_template in a numeric_state trigger, but the docs seem to imply it creates a variable named state which is the entity’s state object. If that’s true, then for sensor.humidity_sensor_59 you could (in theory) shorten it to state.state.

Lastly, when turning a light on or off I’d recommend using the light.turn_on and off services (as opposed to the homeassistant services. The homeassistant service will just turn around and make a call to the corresponding light service, so you might as well be more efficient and call the light service directly.)

So, putting that all together:

- id: Master_Bathroom_Humidity_High
  alias: Master Bathroom Humidity High
  trigger:
  - platform: numeric_state
    entity_id: sensor.humidity_sensor_59
    value_template: "{{ state.state|float - states('sensor.main_floor_humidity')|float }}"
    above: 7
  action:
  - service: light.turn_on
    entity_id: light.bathroom_fan_67

- id: Master_Bathroom_Humidity_Normal
  alias: Master Bathroom Humidity Normal
  trigger:
  - platform: numeric_state
    entity_id: sensor.humidity_sensor_59
    value_template: "{{ state.state|float - states('sensor.main_floor_humidity')|float }}"
    below: 5
  action:
  - service: light.turn_off
    entity_id: light.bathroom_fan_67

Thanks… that was the missing piece.

I am not getting it to fire on. Still verifying whether it is turning off or not properly.

Part of the challenge is that the 4-in-1 sensor doesn’t transmit constant data to maximize battery life so tweaking that as well to help find the sweat spot.

Hi , i triesd to use the same script but doesn’t work
I have a xiaomi humidity which goes between 0 and 100% but the light doesn’t turn on , any help?

- id: Toilet_Humidity_High
  alias: Toilet Humidity High
  trigger:
  - platform: numeric_state
    entity_id: sensor.0x00158d000464c4a6_humidity_3
    value_template: "{{ state.state|float - states('sensor.0x00158d000464c4a6_humidity_3')|float }}"
    above: 8
  action:
    service: light.turn_on
    entity_id: light.light4  
  ###########################################
  ## 
  ###########################################
- id: Toilet_Humidity_Normal
  alias: Toilet Humidity Normal
  trigger:
  - platform: numeric_state
    entity_id: sensor.0x00158d000464c4a6_humidity_3
    value_template: "{{ state.state|float - states('sensor.0x00158d000464c4a6_humidity_3')|float }}"
    below: 6
  action:
    service: light.turn_off
    entity_id: light.light4  

thanks

edit: all done , i was missing to compare with another sensor

1 Like