Compare sensor values in an automation

Hello, can someone help me please?
I want the thermostatic valve to be switched off (for at least 10 minutes) when the flow cools down or the return is only 3 ° colder than the flow. otherwise switch on

Temp(v [2 minutes ago])> temp(v) OR Temp(v)-3<Temp( r )

{{sensor.shelly1_f4cfa2e47733_temperature_2 [2 minutes ago]}} > {{sensor.shelly1_f4cfa2e47733_temperature_2}} OR {{sensor.shelly1_f4cfa2e47733_temperature_2 [-3]}} < {{sensor.shelly1_f4cfa2e47733_temperature}}

what should the yaml look like?

- id: '1611841355429'
  alias: thermostatic
  description: Thermostatic valve off when the flow is too cool
  trigger:
  - platform: numeric_state
    entity_id: sensor.shelly1_f4cfa2e47733_temperature_2
?
?
?
?
    entity_id: sensor.shelly1_f4cfa2e47733_temperature
?
?
?
?
  action:
  - type: turn_off
    device_id: 785919c471331cf6ca327fa83d5136d7
    entity_id: switch.shelly1_f4cfa2e47733
    domain: switch
  mode: single

thanks

How do I get a value X minutes ago from sensor.shelly1_f4cfa2e47733_temperature_2

That really won’t be easy.

You will need to create another entity to hold the value of your current temperature that only gets updated periodically via an automation.

you can use a time pattern trigger to update the entity periodically. While the “periodically” might be assumed logically to be every 2 minutes that wouldn’t meet your needs since the sensor value will only ever be at most 2 minutes old, but the value could be only a few seconds old depending on where you are in the update cycle. So you will need something that will hold a history of values that you can pick out the one that as close to 2 minutes as reasonably possible.

I recommend using a custom integtration called hass-variables (https://github.com/Wibias/hass-variables)

then you can create an automation that sets the variable state every, let’s say 30 seconds, and then shift the state to an attribute to act kind of like a shift register.

variable:
  shelly_temp:
    value: 'none'
    restore: true

then the automation:

- alias: updatre shelly temp history
    initial_state: 'on'
    trigger:
      - platform: time_pattern
        seconds: '/30'
    action:
      - service: variable.set_variable
        data:
          variable: shelly_temp
          attributes: 
            history_1: "{{ states('variable.shelly_temp') }}"
            history_2: "{{ state_attr('variable.shelly_temp, 'history_1') }}"
            history_3: "{{ state_attr('variable.shelly_temp', 'history_2') }}"
            history_4: "{{ state_attr('variable.shelly_temp', 'history_3') }}"
            history_5: "{{ state_attr('variable.shelly_temp', 'history_4') }}"
      - service: variable.set_variable
        data:
          variable: shelly_temp
          value: "{{ states('sensor.shelly1_f4cfa2e47733_temperature_2') }}"

then since the value of the variable history attribute gets updated every 30 seconds then the value from 2 minutes ago should be history_3 (i think…or maybe history_4).

then use can use that variable attribute in your calculation.

it won’t ever be from exactly 2 minutes ago but it will be from between 1:30 and 2 minutes ago.

you can obviously tighten that down further but you will need to make the list of history_x attributes long enough to catch all of the register shifts. that list could get pretty long the more often you update the variable.

Like I said it’s a bit involved but I don’t know of any other way to do it to capture a historical distinct value.

2 Likes

@finity: I copied the code into automations.yaml. then the following error message appears

Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/automations.yaml”, line “initial_state: ‘on’”
, column 18

So I changed it as follows

- alias: updatre shelly temp history
  initial_state: 'on'
  trigger:
    - platform: time_pattern
      seconds: '/30'
  action:
    - service: variable.set_variable
      data:
        variable: shelly_temp
        attributes: 
          history_1: "{{ states('variable.shelly_temp') }}"
          history_2: "{{ state_attr('variable.shelly_temp, 'history_1') }}"
          history_3: "{{ state_attr('variable.shelly_temp', 'history_2') }}"
          history_4: "{{ state_attr('variable.shelly_temp', 'history_3') }}"
          history_5: "{{ state_attr('variable.shelly_temp', 'history_4') }}"
    - service: variable.set_variable
      data:
        variable: shelly_temp
        value: "{{ states.sensor.shelly1_f4cfa2e47733_temperature_2.state }}"

Now there is no error message, but I have no values in shelly temp

i have first

value: “{{states (‘sensor.shelly1_f4cfa2e47733_temperature_2’)}}”

used, no data. Then I have

value: “{{states.sensor.shelly1_f4cfa2e47733_temperature_2.state}}”

tried also no data

Sorry that was a copy/paste error on my part but you fixed it.

aside from that do you have any values in the “sensor.shelly1_f4cfa2e47733_temperature_2” when you look at the sensor in your states page?

or any values for “sensor.shelly1_f4cfa2e47733_temperature”?

it’s impossible for me to know exactly which entities to use since I can’t see the real life states in your config so I just used the “_2” sensor as an example.

1 Like

@finity Thank you!
the device is:

shelly1-F4CFA2E47733

the sensor is:

sensor.shelly1_f4cfa2e47733_temperature_2

so do you see any value in that sensor in your states page?

@finity:
from

sensor.shelly1_f4cfa2e47733_temperature_2

I see worth it
shelly temp 003

not in

shelly temp
shelly temp 004

Sorry, another typo on my part:

- alias: updatre shelly temp history
  initial_state: 'on'
  trigger:
    - platform: time_pattern
      seconds: '/30'
  action:
    - service: variable.set_variable
      data:
        variable: shelly_temp
        attributes: 
          history_1: "{{ states('variable.shelly_temp') }}"
          history_2: "{{ state_attr('variable.shelly_temp', 'history_1') }}"
          history_3: "{{ state_attr('variable.shelly_temp', 'history_2') }}"
          history_4: "{{ state_attr('variable.shelly_temp', 'history_3') }}"
          history_5: "{{ state_attr('variable.shelly_temp', 'history_4') }}"
    - service: variable.set_variable
      data:
        variable: shelly_temp
        value: "{{ states.sensor.shelly1_f4cfa2e47733_temperature_2.state }}"

I accidentally deleted a quotation mark in the template for history_2.

You should have also gotten an error in your logs for “invalid template…” for that automation.

that’s how I found the problem.

I suggest you familiarize yourself with the Trend Binary Sensor. It may be useful for your current requirement (or possibly future projects).

It can be configured to report when a sensor’s values are following a certain trend. For example, when a temperature sensor is reporting decreasing temperature (and you can define the rate of decrease).

Thanks, now it works with shelly_temp.

Now I have written:

- id: '1611841355430'
  alias: Heating off when the flow temperature drops
  initial_state: 'on'
  trigger:
    - platform: time_pattern
      minutes: /1
  condition:
    - condition: template
      value_template: "{{ ( states.shelly_temp.state  ) > ( states. sensor.shelly1_f4cfa2e47733_temperature_2.state ) }}"
  action:
  - type: turn_off
    device_id: 785919c471331cf6ca327fa83d5136d7
    entity_id: switch.shelly1_f4cfa2e47733
    domain: switch
  mode: single

Is that OK, it doesn’t turn off!
shelly temp 005

Try changing value_template to this:

    value_template: "{{ states('variable.shelly_temp') | float > states('sensor.shelly1_f4cfa2e47733_temperature_2') | float }}"
  
1 Like

@123:Thanks,
now turn it off.

to switch on:

- id: '1611841355431'
  alias: Heating off when the flow temperature increases and has 3 more
  trigger:
  - platform: time_pattern
    minutes: /1
  condition:
  - condition: template
    value_template: '{{ states(''variable.shelly_temp'') | float < states(''sensor.shelly1_f4cfa2e47733_temperature_2'')
      | float }}'
  - condition: and
    conditions:
    - condition: template
      value_template: '{{ states(''sensor.shelly1_f4cfa2e47733_temperature_2'')
        | float -3 > states(''sensor.shelly1_f4cfa2e47733_temperature'')
        | float }}'
  action:
  - type: turn_off
    device_id: 785919c471331cf6ca327fa83d5136d7
    entity_id: switch.shelly1_f4cfa2e47733
    domain: switch
  initial_state: 'on'
  mode: single

Is that right?

If you want it to “switch on” then you will need to change this:

  action:
  - type: turn_off

to this:

  action:
  - type: turn_on
1 Like