Automations doesn't trigger sometimes

Hi all,

I have a problem with a pretty straightforward automation script. The problem is that the script not always triggers (while the conditions are true);

Sometimes it works for 3 days, and sometimes it only works one day. To get the script / trigger working again i need to restart HomeAssistant or i need to reload the automations.

The script:

  alias: Turn on fan if humidity is > 75 and temperature > 30 and fan is off
  description: ''
  trigger:
  - platform: template
    value_template: '{{ states.sensor.room_sensor_mijia_humidity | float > 75 }}'
  condition:
  - condition: template
    value_template: '{{ states.sensor.sonoffth_fan_ds18b20_temperature.state | float > 30 }}'
  - condition: state
    entity_id: switch.sonofftouch_t2afan
    state: 'off'
  action:
  - device_id: sdf3fd3ssd4fsdf
    domain: switch
    entity_id: switch.sonofftouch_t2afan
    type: turn_on

When the script doesn’t work i always check the conditions. The conditions are truebut the script will not always trigger.

I can fix this with a time_pattern trigger but is not a real nice solution. Right?

Thanks!

Try taking the | float out of the condition value template, you are comparing a float to a string (state: 'off"), I think this will help.

1 Like

Thanks!

Could you explain why it sometimes (mostly) works fine?

However: i’m doing something not right :wink:

I disagree with @Kdem, you need to compare ‘numbers’ (be they floats or integers) else you will find :
‘2’ > ‘10’ == True

However this will only trigger if the value was less than 75 and moves to greater than 75, this should be fine for ‘normal’ operations but if you restart (etc.) with the value already above 75, it won’t.

You should also be using initialisation proof syntax, so instead of : -

'{{ states.sensor.room_sensor_mijia_humidity.state | float > 75 }}'

use

"{{ states('sensor.room_sensor_mijia_humidity') | float > 75 }}"
1 Like

I was talking about the | float in the condition value template, not the trigger. Also you left the .state out in the template editor, its in your original code.

1 Like

Okay, check. This script will only trigger when the shower is on, so for example in the morning the value is always lower then 75. However; sometimes it will not trigger but the conditions are both true… any explanation or solution for this?

Numeric state trigger and condition would be what you’re really looking for here.

  alias: Turn on fan if humidity is > 75 and temperature > 30 and fan is off
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.room_sensor_mijia_humidity
    above: 75
  condition:
  - condition: numeric_state
    entity_id: sensor.sonoffth_fan_ds18b20_temperature
    above: 30
  - condition: state
    entity_id: switch.sonofftouch_t2afan
    state: 'off'
  action:
  - device_id: sdf3fd3ssd4fsdf
    domain: switch
    entity_id: switch.sonofftouch_t2afan
    type: turn_on

You may find that works more reliably

1 Like

States are always strings, so if you want to compare them as numbers you need to convert them to numbers. Removing the float filter wouldn’t help.

If what you show in the first box is true, and there’s not a typo, then this would seem to imply the problem is not the trigger or the conditions, but rather the sensor isn’t always available. You should check to make sure that sensor is reliable and doesn’t disappear sometimes.

1 Like

Agreed that’s a better way to write the automation, but they work basically identically to the template trigger & condition in the OP. I can’t see how it would really make a difference in this case.

same argument

Thanks for the reply’s guys. I’m going to test some of your suggestions and i will let you know.

ps. Regarding my screenshots: there was a typo indeed.

Guys,

After the changes the automation worked for 3 days but this morning it failed to trigger again.

I have also found the following issue in HA Github. Seems familiar.

My guess this is a bigger problem and more people should have this problem right?

An ugly workaround is to implement a 1 minute time_pattern trigger.

You could instead create a binary sensor, with or without sensor.time as an entity id, or better still, one of each, then perhaps create another to check if they ever disagree.
Monitor these for a bit and see which you prefer.
Note the binary_sensor will always display the correct sense (if it’s working) ie it won’t be bothered about the transitions. (you could then use the better (???) one to drive a template switch to control your fan)
The above would also allow you to gather evidence for your supposition regarding a ‘bug’

1 Like

That sounds like a good plan! Could you please give some breadcrumps regardings the creation of the binary sensor? Thanks!

Not near a workstation at the moment but it’s always best to start with the docs
Go to integrations then search for item : -

Edit: and then search the forum for different examples

1 Like

Check, will try this. Thanks!

\\edit

To easy :slight_smile: Just followed the documentation with succes! Tommorow This afternoon i will change the automations based on binary sensors.

Glad you got it sorted. Even gladder (is that even a word???) that you have added skills to your repertoire AND searched the docs.
Can you post your two sensors for reference and checking ?
Did you create a template switch or are you just using automations ?
Monitor them and report back with anything interesting.

Hi Mutt,

Hahaha thanks :slight_smile:

Sure! I have created a tempate switch and i’m using the binary switch in the automations.

Automation (two triggers, the old one and the new one which uses the newly created binary sensors.

  trigger:
  - platform: template
    value_template: '{{ states(''sensor.room_sensor_mijia_humidity'') | float > 75 }}'
  - entity_id: sensor.humidity_bath_more_than_75
    from: 'False'
    platform: state
    to: 'True'

Binary sensor configuration in configuration.yml

sensor:
  - platform: template
    sensors:
      temp_water_heat_more_than_37:
        friendly_name: "temp > 37"
        value_template: >-
          {{ states('sensor.sonoffth_18b20_temperature') | float > 37 }}
      temp_water_leiding_less_than_37:
        friendly_name: "temp < 37"
        value_template: >-
          {{ states('sensor.sonoffth_18b20_temperature') | float < 37 }}
      humidity_bath_more_than_75:
        friendly_name: "humidity > 75"
        value_template: >-
          {{ states('sensor.room_sensor_mijia_humidity') | float > 75 }}

you didn’t create binary_sensors here, but merely regular template sensors with a true/false state…

make them binary_sensors using

binary_sensor:

instead of

sensor:

also, in the trigger part, you should then use binary_sensor as entity_id, checking for state ‘on’, not state ‘True’

binary_sensor:
  - platform: template
    sensors:
      humidity_bath_more_than_75:
        friendly_name: "humidity > 75"
        value_template: >-
          {{states('sensor.bath_mijia_humidity')|float > 75}}
        device_class: moisture

and automation trigger:

  trigger:
    - platform: state
      entity_id: binary_sensor.humidity_bath_more_than_75
      to: 'on'
1 Like