Force trigger to be re-evaluated?

Is there a way to force a trigger to be re-evaluated?

The situation I have is that I have a trigger that when it’s “true” it fires an action to remediate what caused the trigger. The problem is that the remediation fails about 5% of the time and the trigger remains “true” and won’t fire again. So I use a time pattern trigger to fire the remediation every 5 minutes but that causes a lot more traffic on my network and fills up my logs.

I thought it might be more elegant if there was a way to make HA evaluate the trigger again and remediate if needed. Is this possible?

I guess your trigger is based on the event when a device or entity turns from false (or anything else) to true immediately.

What about introducing a 2nd trigger that checks i f the state of the trigger has been true for a certain period of time, e.g. 5min?

There is a service called automation.trigger, you can try it out in developer tools

It can be used to trigger an automation an automation and execute its actions, optionally skipping the conditions.

But the problem is that to use this you’d need another automation and effectively that has the same problem. So what I would suggest is this:

  1. Add your first state-based trigger that fires when true.
  2. Add your time pattern trigger of 5 minutes
  3. Add a condition that restricts it from proceeding unless the entity your watching in the state-based trigger is in the bad state

What will happen then is it will trigger when the state of your entity turns bad (since the condition is obviously true, its the same check as the trigger). It will also try to trigger every 5 minutes but will only do the actions if the state of your entity is bad (which it won’t be most of the time).

Does that work? I can be more specific in the example if you share the automation code if you need it.

Hmmm. Not sure how to write an automation like that. Can you point me in the right direction?

Also if you turn an automation off and back on, it will re-initialize the trigger(s), which might do what you want.

Can you show the trigger code you have right now?
Might be the most productive way to come up with suggestions.

The code below is (almost) straight from the documentation here:

automation:
  trigger:
    - platform: state
      entity_id: device_tracker.paulus
      to: "home"
    - platform: state
      entity_id: device_tracker.paulus
      to: "home"
      for: "01:10:05"

Happy to share. The overall issue that I am trying to address is that I have a pair of Zigbee thermostats which I like because they are locally controlled, they look nice, etc. What I don’t like is that the drop commands about 5% of the time. So I have my automations set local variables (helpers) instead of sending commands to the thermostats themselves. Then I have a single automation that changes the thermostat, which is the part I want to improve. With that bit of background, this should make sense:

- id: '1587088885443'
  alias: Themostat Settings
  description: ''
  trigger:
  - platform: template
    value_template: '{{ state_attr(''climate.thermostat_east'', ''target_temp_high'')
      != states(''input_select.thermostat_east_cool_setting'') }}'
  - platform: template
    value_template: '{{ state_attr(''climate.thermostat_east'', ''target_temp_low'')
      != states(''input_select.thermostat_east_heat_setting'') }}'
  - platform: template
    value_template: '{{ state_attr(''climate.thermostat_west'', ''target_temp_high'')
      != states(''input_select.thermostat_west_cool_setting'') }}'
  - platform: template
    value_template: '{{ state_attr(''climate.thermostat_west'', ''target_temp_low'')
      != states(''input_select.thermostat_west_heat_setting'') }}'
  - platform: template
    value_template: '{{ state_attr(''climate.thermostat_east'', ''hvac_mode'') !=
      states(''input_select.thermostat_east_mode'') }}'
  - platform: template
    value_template: '{{ state_attr(''climate.thermostat_west'', ''hvac_mode'') !=
      states(''input_select.thermostat_west_mode'') }}'
  - minutes: /5
    platform: time_pattern
  action:
  - data_template:
      entity_id: climate.thermostat_east
      target_temp_high: '{{ states(''input_select.thermostat_east_cool_setting'')
        }}'
      target_temp_low: '{{ states(''input_select.thermostat_east_heat_setting'') }}'
    service: climate.set_temperature
  - data_template:
      entity_id: climate.thermostat_west
      target_temp_high: '{{ states(''input_select.thermostat_west_cool_setting'')
        }}'
      target_temp_low: '{{ states(''input_select.thermostat_west_heat_setting'') }}'
    service: climate.set_temperature
  - data_template:
      entity_id: climate.thermostat_east
      hvac_mode: '{{ states(''input_select.thermostat_east_mode'') }}'
    service: climate.set_hvac_mode
  - data_template:
      entity_id: climate.thermostat_west
      hvac_mode: '{{ states(''input_select.thermostat_west_mode'') }}'
    service: climate.set_hvac_mode

I like your idea because it is very simple. Unfortunately, it does not work in my testing. I tried turning it off for only a second and also for a few minutes, but nothing happened when I turned it back on. Nothing in the logs to indicate it fired at all and the “last triggered” time did not change. It’s a shame. I had high hopes. Thanks for your help.

This is a good idea. The problem is that the thermostat’s state does not change. It just silently drops the command. In my automation, I compare the thermostat setting with the helper setting and trigger if they do not match, but if it fails again, it does not check again. Thus my problem. I appreciate your help.

The thing I don’t like about this thread (and your other thread too) is that it’s like a minimum information puzzle except you’ve deleted half the clues.

How about we start again ?
forget devices, forget code, what in the real world are you trying to do ?
What devices do you have ? (heater - how switched ? temperature sensors 1,2,3,78 ???, what thermostats ? (note thermostats can be sensors at the same time))
Do you use a monolithic configuration (all in configuration.yaml) or primary (sensors.yaml, automations.yaml, scripts.yaml, etc.) or do you use packages ?
Have you created any entities ?
What are they ?

True/False ? - You have a thermostat that you want to update the setpoint for and it ‘often’ doesn’t accept the new value ??? (This seems to be a hardware problem, I have already offered a work around on this, the alternative is to change the hardware).

It’s hard to diagnose a problem remotely anyway but when you are forced to guess it’s nigh on impossible. I see you have upped your recent reading from 2 to 4 hours but that’s still not a lot. (diagnosing my duck dns issues took me upto recent reading of 3 days)

This should work. It will be a long automation because I will probably need to have several iterations – at 5, 10, 15, 20 minutes to make sure it finally changes, but it is just copy/paste. I will let you know how it goes. Thanks for your help!

The thing about most, if not all, triggers is, they require a state_changed event to cause them to be evaluated. I.e., in your triggers, device_tracker.paulus (hmm, funny you have the same name as…) has to change from some state other than home to home for them to fire, and the second one further requires that entity to stay in that state for one hour, ten minutes and 5 seconds.

EDIT: Oops, I was looking at the example someone else posted instead of yours. LOL!

1 Like

@Mutt – I’m sorry you can’t seem to understand what I am trying to do. Three other people did and offered good suggestions. I doubt you will understand my reiteration either. I will just have to live without your help. Thanks anyway.

So I had a similar situation once with my Nest thermostat, before the integration switched to stream based from polling based, although it still happens sometimes that a command does not take effect. And in my case the only command I send is to change the mode to/from “ECO” mode.

What I did was to write a script that would change the mode according to an input variable. It would then wait a while (long enough that the changes should have taken effect), and then it checks the state. If it’s correct, then it’s done. If not, then it effectively calls itself again. Later, when it happened much less often I simplified to where instead of trying again it simply sent me a text to tell me it happened.

Following is what I use today, starting with the automation that changes the mode when the house switches to/from “away”:

- alias: Nest Thermostat
  trigger:
    platform: state
    entity_id: input_select.home_mode
  condition:
    condition: template
    value_template: >
      {{ trigger.from_state.state in ['Home', 'Returning']  and
          trigger.to_state.state   in ['Away']                 or
          trigger.from_state.state in ['Away']               and
          trigger.to_state.state   in ['Home', 'Returning'] }}
  action:
    - service: script.turn_off
      entity_id:
        - script.nest_eco_on
        - script.nest_eco_off
        - script.nest_eco_check
    - service_template: >
        {% if trigger.to_state.state in ['Away'] %}
          script.nest_eco_on
        {% else %}
          script.nest_eco_off
        {% endif %}

And here are the scripts:

nest_eco_on:
  alias: Nest Thermostat - Set to ECO mode
  sequence:
    - wait_template: "{{ is_state('timer.nest_thm_wait', 'idle') }}"
    # Only turn on ECO mode if thermostat is not off and not already in ECO
    # mode.
    - condition: template
      value_template: >
        {{ states('sensor.kitchen_thermostat_hvac_mode')
            not in ['off', 'eco'] }}
    # Save current operation mode.
    - service: input_text.set_value
      entity_id: input_text.previous_hvac_mode
      data_template:
        value: "{{ states('sensor.kitchen_thermostat_hvac_mode') }}"
    - service: timer.start
      entity_id: timer.nest_thm_wait
    # Turn on ECO mode.
    - service: climate.set_preset_mode
      entity_id: climate.kitchen
      data:
        preset_mode: eco
    - service: script.nest_eco_check
      data:
        eco_on: true
nest_eco_off:
  alias: Nest Thermostat - Return from ECO mode
  sequence:
    - wait_template: "{{ is_state('timer.nest_thm_wait', 'idle') }}"
    # Only set back to previous mode if thermostat is still in ECO mode.
    - condition: template
      value_template: >
        {{ is_state('sensor.kitchen_thermostat_hvac_mode', 'eco') }}
    - service: timer.start
      entity_id: timer.nest_thm_wait
    # Restore previous operation mode.
    - service: climate.set_hvac_mode
      entity_id: climate.kitchen
      data_template:
        hvac_mode: "{{ states('input_text.previous_hvac_mode') }}"
    - service: script.nest_eco_check
      data:
        eco_on: false
nest_eco_check:
  alias: Nest Thermostat - Check ECO mode
  sequence:
    # Wait for last command to take effect in state.
    # Also wait until thermostat is online.
    - wait_template: >
        {{ is_state('timer.nest_thm_wait', 'idle') and
            is_state('binary_sensor.kitchen_thermostat_online', 'on') }}
    # Delay a bit for state to update in case thermostat was offline.
    - delay: '00:02:00'
    # Are we in the wrong mode?
    - condition: template
      value_template: >
        {{ eco_on and
            not is_state('sensor.kitchen_thermostat_hvac_mode', 'eco') or
            not eco_on and
            is_state('sensor.kitchen_thermostat_hvac_mode', 'eco') }}
    - service: notify.email_phil
      data:
        title: 'Alert:'
        message: ECO mode did not change

As I said, the “check” script used to call the turn on or off script again if it didn’t work, and I think I may have had a max counter or something so it didn’t go on forever.

Hope this helps.

@pnbruckner Brilliant! Thanks for your help.

@pnbruckner, With your inspiration I put together this little script as a proof of concept, but the value_template reports “true” regardess of the values set. What am I missing?

'1587739282724':
  alias: Test
  sequence:
  - condition: template
    value_template: '{{ state_attr('climate.thermostat_east', 'target_temp_high')!= states('input_select.thermostat_east_cool_setting') }}'
  - data:
      message: Ping
    service: notify.sperry_house

Use double quotes outside the template:

value_template: "{{ state_attr('climate.thermostat_east', 'target_temp_high')!= states('input_select.thermostat_east_cool_setting') }}"

Otherwise this is your first pair of quotes:

value_template: '{{ state_attr('

Thanks @tom_l. I gave that try but it still returns true every time.

Put this in the developer tools template editor, and post the results:

{{ state_attr('climate.thermostat_east', 'target_temp_high') }}

{{ states('input_select.thermostat_east_cool_setting') }}

Because you are comparing strings instead of numbers I suspect you might have something like:

23.0 != 23

Which can be solved by converting the strings to numbers:

value_template: "{{ state_attr('climate.thermostat_east', 'target_temp_high')|float != states('input_select.thermostat_east_cool_setting')|float }}"