Moving away from Openhab to Hass.io. Help with humidity Automation

I have been running Hass.io alongside Openhab for some time now. I have really only been using Openhab for the automations component. All my interactions with the home are through nabucasa → google home intergation and through the lovelace interface.

I have all almost everything running on MQTT. lights and fans (31 total) through tasmota, Xiaomi sensors and buttons (19 total) via zigbee2mqtt and everything has been rock solid.

I am now trying to move away from Openhab and rely solely on Hass.io
I have successfully moved all my automations across to Hass.io accept for my ensuite bathroom humidity automation.

With my current Openhab automation on a change to the ensuite humidity sensor value it would compare this new value to the master bedroom sensor value in the adjacent room. If the difference is greater than 10 the fan turns on. If the difference is less than 5 the fan turns off.

I have been trying to make this happen in Hass.io for last last 3 weeks but can not seem to get the rule setup correctly and I am loosing my hair fast.

Current Openhab rule


when

Item FF_Ensuite_Humidity changed

then

if ((FF_Ensuite_Humidity.state as DecimalType).intValue > (FF_MasterBedroom_Humidity.state as DecimalType).intValue + 10) { FF_Ensuite_Fan.sendCommand(ON) }

else if ((FF_Ensuite_Humidity.state as DecimalType).intValue < (FF_MasterBedroom_Humidity.state as DecimalType).intValue + 5) {FF_Ensuite_Fan.sendCommand(OFF)}`

end

Current Hass.io attempt that only seems to turn the fan off and never switches it on

  alias: Ensuite Humidity Control
  trigger:
  - entity_id: sensor.0x00158d00024906f1_humidity
    platform: state
  action:
  - entity_id: fan.ensuite
    service_template: "{% if is_state('sensor.0x00158d00024906f1_humidity - sensor.0x00158d000245f03b_humidity',\
      \ '>10') %}\n  fan.turn_on\n{% else %}\n  fan.turn_off\n{% endif %}\n"

Any help or guidance on correcting my setup for my use case is greatly appreciated.

Side note, I have also tried using the generic hygrostat integration which is not really effective in my setup as the time it takes to update the hygrostat sensor, ~5 mins, the room is full of steam and the shower is over, the fan kicks on when the user is out and drying themselves which unfortunately does not meet the WAF.

These two examples are functionally equivalent.

Example 1:

    service_template: >
      {% if states('sensor.0x00158d00024906f1_humidity') - states('sensor.0x00158d000245f03b_humidity') > 10 %}
        fan.turn_on
      {% else %}
        fan.turn_off
      {% endif %}

Example 2:

    service_template: >
      {{ fan.turn_on states('sensor.0x00158d00024906f1_humidity') - states('sensor.0x00158d000245f03b_humidity') > 10 else fan.turn_off }}

If you discover the calculation doesn’t seem to work then you may need to convert each sensor’s state to an integer value like this:

      {{ fan.turn_on states('sensor.0x00158d00024906f1_humidity') | int - states('sensor.0x00158d000245f03b_humidity') | int > 10 else fan.turn_off }}

You may have noticed that this statement
service_template: >
provides the flexibility of writing a multi-line template. It’s even more flexible than what I’ve shown because you even do this:

    service_template: >
      {% if states('sensor.0x00158d00024906f1_humidity') - 
            states('sensor.0x00158d000245f03b_humidity') > 10 %}
        fan.turn_on
      {% else %} fan.turn_off 
      {% endif %}

EDIT
I just noticed that this template isn’t quite the same as what you did in openHAB.

to get exactly what you are looking for including @123’s service template:

  alias: Ensuite Humidity Control
  trigger:
  - entity_id: sensor.0x00158d00024906f1_humidity
    platform: state
  condition:
    condition: template
    value_template: >
      {% set h1 = states('sensor.0x00158d00024906f1_humidity') | float %}
      {% set h2 = states('sensor.0x00158d000245f03b_humidity') | float %}
      {{ h1 - h2 > 10 or h1 - h2 < 5 }}
  action:
    service_template: >
      {% set h1 = states('sensor.0x00158d00024906f1_humidity') | float %}
      {% set h2 = states('sensor.0x00158d000245f03b_humidity') | float %}
      fan.turn_{{ 'on' if h1 - h2 > 10 else 'off' }}
    entity_id: fan.ensuite

The condition limits the automation to only continue when below 5 or above 10. The service template handles turning on or off based on being above or below 10. This will result in:

less than 5 -> turn off
between 5 and 10 -> do nothing
greater than 10 -> turn on

@petro
Thank you for the quick reply
When I check my config I am getting the following error

Invalid config for [automation]: [entity_id] is an invalid option for [automation]. Check: automation->entity_id. (See /config/configuration.yaml, line 92).

@123
Thanks for the response. Yes i forgot to mention in the end i had given up on the hysteresis and just wanted to get something working that I could build upon. I will try with the interger as it does not seem to fire with the first example.

my spacing was off on entity_id. Try it again or just add 1 space before entity_id in the action section. I edited it so if you copy it should work.

1 Like

@petro
You legend, can I buy you a beer! Thanks I had moved it back one space instead of forward. Seems to be working perfectly now.

1 Like