Automatic state refresh for all entities under "condition:"s in an automation (before conditions are validated)

Hello all,

I have to implement few automations that are very time sensitive and which have condition:s that need to be met.
The issue I have today is that after the Automation is triggered, the sensor state that need to be validated in the conditions sometimes is not up to date and the script is not executed (when it should… if the state would be refreshed).
I could obviously set the scan_interval to 1 sec and that would most likely work, but it is very inefficient, i.e. is consuming a lot of resources, especially when considering that the automation is probably triggered only once a day.

My feature request is to introduce an automatic refresh for all the entities used in the condition:s after the automation is triggered and before validating the conditions themselves. This would guarantee that everything is being validated according to their current state.


Have you tried to move your condition into the action section; and prior to that call homeassistant.update_entity to force the sensors to update?

This way the automation will always fire on the trigger; but delay the condition evaluation to where you have it in the action block… after the manual called update. You maybe able to add another condition in the condition block to further decrease how often this is evaluated.

Thanks @jwelter. l’ll explore that. it sounds like a good idea.
on action 1 I would update the sensor and then on action 2 I would implement a template with IF statement to check the state of the sensor I’ve just updated… right?

It should work but even if it is lower priority I think it makes sense to include this auto_refresh option for the entities in the conditions :). In an automation, who wants to check conditions of an entity that is not up to date?

Hi @jwelter. I’ve tested that aproach but when calling the homeassistant.update_entity I get the following error:

Forced update failed. Component for device_tracker.tv_livingroom not loaded.

Part of the code where I do it:

    - service: homeassistant.update_entity
      entity_id: device_tracker.tv_livingroom

The device_tracker.tv_livingroom is below… it works ok but when I force the update as above i get that message in the log:

  - platform: ping

Yes it seems that device tracker doesn’t support a forced update.

Can you setup a command line sensor to give it a try? That is how mine work.

just tested the binary sensor with platfrom ping (which even gives a bit more info) and it works perfectly so it is either a bug of device tracker (not alowing force update) or a feature for some reason :slight_smile:

1 Like

I know this is an old topic, but could you please post an example how to do this? I have the same problem where my template isn’t updated. Now I added an extra automation before the other automations starts, but I’d rather have it in one automation.

I’m not using this anymore… but it could be something like this:

#### If not on.. turn living room TV on
      - service: script.update_tv_status
      - condition: state
        entity_id: binary_sensor.lr_tv_sensor
        state: 'off'
      - service: switch.toggle
        entity_id: switch.lr_tv_pwr 
##Aux Scripts buttons
      - service: homeassistant.update_entity
        entity_id: binary_sensor.lr_tv_sensor
      - delay: '00:00:02'