DHT22 Automation Trigger

Hi everyone,

I have a DHT22 on my Pi and i’m using the DHT module. It works and returns the temperature.

My problem is about an automation trigger to turn on / off my heater.
The trigger is never fired.

Piece of my config:

    sensor:
      - platform: dht
        sensor: DHT22
        pin: 4
        name: temperature
        temperature_offset: -1
        humidity_offset: 0
        monitored_conditions:
          - temperature


    automation:
      - alias: "Turn on Heater"
        hide_entity: False
        trigger:
          - platform: numeric_state
            entity_id: sensor.temperature
            below: 50
        action:
          service: switch.turn_on
          entity_id: switch.fibaro_system_fgwpef_wall_plug_switch

Thanks for your assistance.

Here’s an automation that should work:

- id: turn_on_heater
  alias: "Turn On Heater if Temp Below"
  trigger:
    - platform: time
      seconds: '/2'
  condition:
    - condition: template
      value_template: '{{ states.sensor.dht_sensor_temperature.state < 50  }}'
  action:
    - service: homeassistant.turn_on
      entity_id: switch.fibaro_system_fgwpef_wall_plug_switch

So it checks every 2 seconds (you could have minutes etc. too), than checks if below 50, and turns on accordingly.

Thanks for your answer.

Same result, the trigger is never fired.

I think the pb is from ‘states.sensor.dht_sensor_temperature.state’ . I don’t know how the temp information is returned by the module.

The condition as @Bit-River proposed is based on the value of the sensor which should be visable in the frontend or - developer tools - states. You can also check the template to be true of false by entering it in the developer tools - templates.

sensor:
      - platform: dht
        sensor: DHT22
        pin: 4
        **name: temperature**
        temperature_offset: -1
        humidity_offset: 0
        monitored_conditions:
          - temperature

Try taking the “name: temperature” line out of your sensor definition.

Allow the DHT module to name them, and you’ll have a sensor called:

sensor.dht_sensor_temperature

Try the automation then.

You can always make a Binary Sensor;

- platform: threshold
  name: "House Temp"
  threshold: 50
  type: lower
  entity_id: sensor.temperature

and then the automation;

- alias: "Turn on Heater"
  trigger:
    platform: state
    entity_id: binary_sensor.house_temp
    to: 'on'
  action:
    service: switch.turn_on
    entity_id: switch.fibaro_system_fgwpef_wall_plug_switch

Thanks guys for your answers

My config:


  - alias: "Heater on"
    trigger:
      - platform: time
        minutes: '/1'
        seconds: 00
    condition:
      - condition: template
        value_template: '{{ States.sensor.dht_sensor_temperature.State < 50.0 }}'
    action:
      service: switch.turn_on
      entity_id: switch.fibaro_system_fgwpef_wall_plug_switch

And in the States page:

Same result, the trigger is never fired…

I think everything in your template should be lowercase. Do you have errors in the HA log?

Yes there is an error !

2017-10-05 14:37:00 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.4/asyncio/tasks.py", line 237, in _step
    result = next(coro)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/automation/__init__.py", line 342, in async_trigger
    if skip_condition or self._cond_func(variables):
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/automation/__init__.py", line 452, in if_action
    return all(check(hass, variables) for check in checks)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/automation/__init__.py", line 452, in <genexpr>
    return all(check(hass, variables) for check in checks)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/condition.py", line 314, in template_if
    return async_template(hass, value_template, variables)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/condition.py", line 296, in async_template
    value = value_template.async_render(variables)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/template.py", line 102, in async_render
    return self._compiled.render(kwargs).strip()
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: unorderable types: str() < float()

I think the DHT Sensor returns a string and not a float !

Ok, change the value template to this;

value_template: '{{ states.sensor.dht_sensor_temperature.state | float <50 }}'

and please note lowercase only!

1 Like

Perfect, It works.

Thanks man !

You’re welcome. Please flag this topic as solved.

1 Like