Automation with script_template in action

Hello. I am having a hard time getting this automation to function. I tried changing the state in developer tools and it will never fire. Any ideas? Thanks in advance

#humidifier
- id: "humidifier_off"
  alias: "Humidifier Off"
  trigger:
    platform: numeric_state
    entity_id: sensor.aeon_labs_zw100_multisensor_6_relative_humidity
    above: 52
    below: 38
    
  action:
    service_template: >-
      {%- if states('sensor.aeon_labs_zw100_multisensor_6_relative_humidity')|float > 52 -%}
      script.umidifier_power_off
      {%- else -%}
      script.humidifier_power_on
      {%- endif -%}

This is what I use

#
# Upper Hall
#

- id: upstairs_humidifier-on-lowcondition
  alias: Upper Hall - Humidifier (On) - Relative Humidity (<42%)
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: sensor.upper_bath_relative_humidity
  condition:
    - condition: template
      value_template: '{{ (states("sensor.upper_bath_relative_humidity") | int) < 42 }}'
    - condition: state
      entity_id: switch.upstairs_humidifier_switch
      state: 'off'
  action:
    service: homeassistant.turn_on
    entity_id: switch.upstairs_humidifier_switch

- id: upstairs_humidifier-off-hicondition
  alias: Upper Hall - Humidifier (Off) - Relative Humidity (>43%)
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: sensor.upper_bath_relative_humidity
  condition:
    - condition: template
      value_template: '{{ (states("sensor.upper_bath_relative_humidity") | int) > 43 }}'
    - condition: state
      entity_id: switch.upstairs_humidifier_switch
      state: 'on'
  action:
    service: homeassistant.turn_off
    entity_id: switch.upstairs_humidifier_switch   

Hth

Thank you. I’m wondering if integers works better than floats. I was also trying to combine on and off all into one. Thank you! I’ll try tomorrow.

you define a entity in the template, not a service.
Try to always use a service and a entity_id. When you define a entity with a template you have to use data_template

This is correct for scripts, you can call them by using the entity_id as the service.

@bookandrelease
You have a typo, you wrote script.umidifier_power_off instead of script.humidifier_power_off
Please be informed that your automation will only fire if the humidity has been between 38 and 52 and then changed to a humidity above 52 or below 38. It will not trigger if your humidity is for example 53 and changes to 54.

  action:
    service: script.turn_on
    data_template:
      entity_id: >-
        {%- if states('sensor.aeon_labs_zw100_multisensor_6_relative_humidity')|int > 52 -%}
        script.humidifier_power_off
        {%- else -%}
        script.humidifier_power_on
        {%- endif -%}

This is what I have now, and still nothing.

I have something like this setup for our Master Bathroom fan using an Aeotec-6 sensor.

- id: '1555779101394'
  alias: Master Bath Fan - ON
  trigger:
  - above: '75'
    entity_id: sensor.master_bathroom_sensor_relative_humidity_measurement
    platform: numeric_state
  condition: []
  action:
  - data:
      entity_id: switch.master_bath_fan
    service: switch.turn_on

- id: '1555779292146'
  alias: Master Bath Fan - OFF
  trigger:
  - below: '70'
    entity_id: sensor.master_bathroom_sensor_relative_humidity_measurement
    for: 00:01:00
    platform: numeric_state
  condition:
  - condition: state
    entity_id: switch.master_bath_fan
    state: 'on'
  action:
  - data:
      entity_id: switch.master_bath_fan
    service: switch.turn_off

Yes, I realize that it’s TWO automations, but for me the K.I.S.S. (keep it simple stupid) means I don’t try to get fancy and condense into one, but just leave what works alone and have two :wink:

Also, FWIW, if I put this into Developer Tools -> Template:

{{ states('sensor.master_bathroom_sensor_relative_humidity_measurement') }}

{% if states('sensor.master_bathroom_sensor_relative_humidity_measurement')|int > 20 %}
TRUE
{% endif %}

I get this result:
image

What do you get when you put the action part into the template editor (Sidebar -> Developer Tools -> Template)? If you trigger the automation manually (which skips all triggers and conditions and goes directly to the action part), does it execute the desired script? I copied your action part and changed the entity to one I have and it gives me the desired result.

@bookandrelease
Use 2 triggers
One above 52
And
One below 38

trigger:
    - platform: numeric_state
      entity_id: sensor.aeon_labs_zw100_multisensor_6_relative_humidity
      below: 38
    - platform: numeric_state
      entity_id: sensor.aeon_labs_zw100_multisensor_6_relative_humidity
      above: 52

Listing above and below together means the numeric_state has to be between the two values.

1 Like

This is overly complex you are just looking for true so : -

{{ states('sensor.master_bathroom_sensor_relative_humidity_measurement') | int > 20 }}

Would suffice

That was purely an example to show the raw humidity state value and the fact that suffixing with | int would allow the > calculation. Thx for your input though.

I get script.humidifier_power_off because the humidity is above 52 at this moment.

When you trigger the automation manually, does it execute the desired script?