Create a condition that's true if 2 separate numeric values are identical for a specific period

Hi all,
in a script, I want to create a condition that is true if 2 numeric values (of different entities) are identical for a specific period of time.

I tried State-condition but it doesn’t seem to accept templates or any other way to compare 2 different entities.
I tried Template-condition but I can’t find a way to specify a time-period.
I tried Numeric State-condition but I can only specify above and/or bellow, there is no way to set something as equal and again I can’t find a way to set a period.

thanks in advance for any help and ideas :heavy_heart_exclamation:

Have you tried creating a template binary sensor that is “on” when the sensor values are the same and “off” when they are not? You could then use that as a simple state condition in your script.

2 Likes

And you might want to post some code. It’s always easier to build upon an example than up from zero. :slight_smile:

1 Like

The following example posts a notification when the values of both sensors are the same for a minimum of 1 minute.

alias: example
trigger:
  - platform: state
    entity_id:
      - sensor.first
      - sensor.second
    to:
    for:
      minutes: 1
condition: []
  - condition: template
    value_template: "{{ states('sensor.first') == states('sensor.second') }}"
action:
  - service: notify.persistent_notification
    data:
      title: Sensor values are the same.
      message: "Value is: {{ trigger.to_state.state }}"
5 Likes

Thank you all for the quick replies, much appreciated!!

I had that in mind but it seemed kinda overkill to do that for a single part in a script (I hate clutter), so I scrapped this idea for the moment.

I only worked with single conditions, there is not much code to share really.

condition: template
value_template: "{{ is_state_attr('climate.thermostat1', 'temperature',
  states('input_number.thermostat1_temperature_ui')|float(0)) }}"

This is the code I created for the template-condition. It does what I want except the part with the time-period.

condition: state
entity_id: climate.thermostat1
attribute: temperature
for:
  hours: 0
  minutes: 0
  seconds: 10
state: ""

This is the code for the state-condition I created. It seems like I can’t use templates or entity-states in state:, which I would need.

Is this an automation? Hmmmm :thinking: Interesting idea but I need it in a script. I might be able to convert the concept.

1 Like

RIGHT! I would never have thought of that! Just had to make it separate dependencies :man_facepalming: In the meantime I managed to figure out another solution but I will use your’s because I like it clean and simple and mine has a little flaw. Thank you man :heavy_heart_exclamation:

In case somebody else comes across this thread I want to share this solution, even if I won’t use it. For whatever reason, the value_template: of a trigger allows to set a time period (in contrast to condition), so I made use of that by restructuring this part of the script. It is not as clean as @123’s and it only makes sense if one wants to terminate the script if the condition are met (like I do) but with the advantage that the script ends in the exact moment the 10 second mark is reached and it doesn’t have to wait for another circle. Another downside is that it won’t terminate the script if the conditions are met already on execution.

parallel:

# This one keeps sending the temperature-value to the thermostat every 20 seconds
  - repeat:
      count: "3600"
      sequence:
        - service: climate.set_temperature 
          #stuff
        - delay:
            seconds: 20

# This one waits until the thermostat temperature is set correctly (for 10 seconds) and terminates the script
  - repeat: 
      count: "1"
      sequence:
        - wait_for_trigger: 
            - platform: template
              value_template: >-
                {% if is_state('climate.thermostat1','heat') and
                is_state_attr('climate.thermostat1', 'temperature',
                states('input_number.thermostat1_temperature_ui')|float(0)) %}
                true {%- else -%} false {%- endif %}
              for:
                seconds: 10
        - service: script.turn_off
          data: {}
          target:
            entity_id: script.thermostat1
1 Like

I tried it but unfortunately it is not working like that, it always returns “CONDITION DID NOT PASS”, I guess it is expecting an empty value.

Yea, it isn’t a good solution imo, the flaw I described is not acceptable. I already did some changes, involving a binary_sensor-template as suggested by @Stiltjack.

I did some more tests but I just can’t get it to work, I really wonder why that is, because I prefer your solution. :thinking: Here is the full script I used for testing, could you do a quick test for me, please? I am on version 2023.9.1 btw. Could you share your full automation/script as well? I would like to test it.

sequence:
  - if:
      - condition: state
        entity_id: climate.thermostat1
        attribute: temperature
        for:
          seconds: 10
        state: ""
    then: []

Thank you again!

I removed my posts because I couldn’t reproduce the initial test results that I had reported. Therefore I must have made a mistake during initial testing.

Your test results are correct; the State Condition I had suggested can’t be used for your application.

That’s too bad, thanks anyway! I wish the devs would enable setting a period for more/all conditions, not just a few of them. That would make this kind of thing SO much easier.

Basic template:

templates:
  - binary_sensor:
      - name: "THERMOSTAT1 synced"
        state: {{ is_state_attr('climate.thermostat', 'temperature', states('input_number.thermostat_temperature_ui')|float(0)) }}
        delay_on:
          seconds: 10

This is a basic template for a sensor that is true if the input_number-state & and the temperature-attribute of the thermostat are identical for a period of 10 seconds. This is a working solution to my initial problem.

Explanation:
is_state_attr([entity], [attribute], [other_value])
Returns true if an attribute of an entity is equal to another value.
states([entity])|float(0)
Returns the value of an entity and coverts it into a number.

My template:

- binary_sensor:
    - name: "THERMOSTAT1 synced"
      state: >
        {{ is_state('switch.thermostat1_mainswitch_ui','on') and is_state('binary_sensor.window1_opening','off') and is_state('climate.thermostat1','heat') and is_state_attr('climate.thermostat1', 'temperature', states('input_number.thermostat1_temperature_ui')|float(0))
        or is_state('switch.thermostat1_mainswitch_ui','on') and is_state('binary_sensor.window1_opening','on') and is_state('climate.thermostat1','heat') and is_state_attr('climate.thermostat1', 'temperature', 5)
        or is_state('switch.thermostat1_mainswitch_ui','off') and is_state('climate.thermostat1','off') }}

In case anyone is interested, here is the full binary_sensor-template I use to check if my window-state & helper entities (to use for the UI) are in sync with my thermostat. As soon as anything becomes out of sync, it returns false and this can be used to start an automation that syncs the state of the mentioned helper-entities to the thermostat and also terminate the script as soon as it is synced.

Even if this solution needs an entity, I think that is alright because it decreases the complexity of the automation/script by A LOT and increases flexibility and reliability.