About making inexpensive models smarter by providing tools and context. (local models, gpt-5-mini, gpt-4.1-mini, gpt-4o-mini ...)

Ok, I also extended the script so it can handle all basic math operations and some aggregations.

This is really starting to get funny. :partying_face:

How can I assist?

      Do you have a tool called calculator?

Yes, I have access to a calculator tool. How can I assist you with calculations?

      show me the room temps

Here are the current room temperatures:  
- Bathroom: 26.0°C 
- Attic Bathroom: 27.0°C 
- Office: 26.8°C 
- Kitchen: 25.0°C 
- <Child 1> Room: 26.0°C 
- <Child 2> Room: 26.0°C 
- Bedroom: 25.5°C 
- Playroom: 26.98°C 
- WC: 25.5°C 
- Living Room: 25.0°C  

Let me know if you need anything else!

      calculate the average temp of the rooms above

The average temperature of the rooms is approximately 25.98°C. If you need further assistance, just let me know!

      show me the calculation method

The average temperature is calculated 
by adding all the room temperatures together 
and then dividing by the number of rooms.

For the given temperatures: 

1. Add the temperatures: 
  26.0 + 27.0 + 26.8 + 25.0 + 26.0 + 26.0 + 25.5 + 26.98 + 25.5 + 25.0 = 259.78  
2. Divide by the number of rooms (10): 
  259.78 / 10 = 25.978  

So, the average temperature is approximately 25.98°C.

I guess at some time I have to provide access to my InfluxDB with an additional tool. :exploding_head:

edit: Here’s the updated calculator script:

alias: Calculator
icon: mdi:calculator-variant
description: >
  This is your calculator tool for everything that needs to be calculated or
  compared with basic math operations. (LLM Tool) 


  - Calculate with add, sub, mul, div

  - Compare values with min, max

  - Aggregate values with avg


  operations Parameter can be : add, sub, mul, div, min, max, avg. 

  numbers parameter is a JSON string array like " [4, 5, 3.2]"

  Returns value is:
    value: <result>
mode: single
fields:
  operation:
    name: Operation
    required: false
    selector:
      select:
        options:
          - add
          - sub
          - mul
          - div
          - min
          - max
          - avg
  numbers:
    name: Zahlen
    description: JSON-Array, z. B. [8, 2, 3]
    required: false
    selector:
      text: null
sequence:
  - action: logbook.log
    data:
      name: "LLM CALCULATOR: "
      message: "{{ operation, numbers }}"
      entity_id: "{{ this.entity_id }}"
  - variables:
      op: "{{ operation | lower }}"
      nums: "{{ numbers | from_json }}"
      allowed_ops:
        - add
        - sub
        - mul
        - div
        - min
        - max
        - avg
      calc: null
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ op == 'add' }}"
        sequence:
          - variables:
              calc: "{{ nums | sum }}"
      - conditions:
          - condition: template
            value_template: "{{ op == 'sub' }}"
        sequence:
          - variables:
              calc: |
                {% if nums|count == 1 %}
                  {{ nums[0] }}
                {% else %}
                  {{ nums[0] - (nums[1:] | sum) }}
                {% endif %}
      - conditions:
          - condition: template
            value_template: "{{ op == 'mul' }}"
        sequence:
          - variables:
              calc: |
                {% set ns = namespace(prod=1) %} {% for n in nums %}
                  {% set ns.prod = ns.prod * n %}
                {% endfor %} {{ ns.prod }}
      - conditions:
          - condition: template
            value_template: "{{ op == 'div' }}"
        sequence:
          - variables:
              calc: |
                {% set ns = namespace(val=nums[0]) %} {% for n in nums[1:] %}
                  {% if n == 0 %}
                    {% set ns.val = 'Error: Division through zero.' %}
                    {% break %}
                  {% else %}
                    {% set ns.val = ns.val / n %}
                  {% endif %}
                {% endfor %} {{ ns.val }}
      - conditions:
          - condition: template
            value_template: "{{ op == 'min' }}"
        sequence:
          - variables:
              calc: "{{ nums | min }}"
      - conditions:
          - condition: template
            value_template: "{{ op == 'max' }}"
        sequence:
          - variables:
              calc: "{{ nums | max }}"
      - conditions:
          - condition: template
            value_template: "{{ op == 'avg' }}"
        sequence:
          - variables:
              calc: "{{ nums | sum / (nums | count) }}"
    default:
      - variables:
          result: >-
            {{ { 'value': 'Error: invalid operation "' ~ op ~ '" provided. Only
            these operations are allowed: ' ~ (allowed_ops | join(', ')) ~ '.' }
            }}
      - stop: ""
        response_variable: result
  - variables:
      result: "{{ {'value': calc} }}"
  - stop: ""
    response_variable: result

I additionally added this to the LLM prompt:

You are VERY bad at calculations, finding min/max values, comparing numbers or date calculations like when what date might be tomorrow, next week or how many days it is until a given date.
Always, really always use the tools provided when possible to get the solution. Do NOT try to calculate yourself as long as there is another way.

1 Like