How can I execute a YAML function by pressing a button?

Hello community,
I have written a function that I want to use to program my heating system to heat at certain times and run in frost protection mode at others. I have already configured an interface for this.

When I press “Heizzeiten schreiben” the function should be executed.
How can I call this function when the button is pressed?

This is the code I created

mqtt:
  number:
    - name: "open3e Heizugszeiten setzen"
      unique_id: open3e_heizugszeiten_setzen
      default_entity_id: number.open3e_heizugszeiten_setzen
      device_class: temperature
      command_topic: "open3e/cmnd"
      command_template: >
        {% set heizen_start1 = states('input_datetime.heizen_start1') [:5]  %} 
        {% set heizen_ende1 = states('input_datetime.heizen_ende1') [:5]  %} 
        {% set heizen_start2 = states('input_datetime.heizen_start2') [:5]  %} 
        {% set heizen_ende2 = states('input_datetime.heizen_ende2') [:5]  %} 
        {% set heizen_start3 = states('input_datetime.heizen_start3') [:5]  %} 
        {% set heizen_ende3 = states('input_datetime.heizen_ende3') [:5]  %}
        {% if  ((heizen_start1=='00:00') and (heizen_ende1=='00:00')) %}
        {% set counter_anzahl = '0' %}
        {% elif  ((heizen_start2=='00:00') and (heizen_ende2=='00:00')) %}
        {% set counter_anzahl = '1' %}
        {% elif  ((heizen_start3=='00:00') and (heizen_ende3=='00:00')) %}
        {% set counter_anzahl = '2' %}
        {% else %}
        {% set counter_anzahl = '3' %}
        {% endif %}
        {% set wochentag = states('input_select.wochentag') %}
        {% if (wochentag=='Montag') %}
        {% set wochentagsID = '761' %}
        {% elif (wochentag=='Dienstag') %}
        {% set wochentagsID = '762' %}
        {% elif (wochentag=='Mittwoch') %}
        {% set wochentagsID = '763' %}
        {% elif (wochentag=='Donnerstag') %}
        {% set wochentagsID = '764' %}
        {% elif (wochentag=='Freitag') %}
        {% set wochentagsID = '765' %}
        {% elif (wochentag=='Samstag') %}
        {% set wochentagsID = '766' %}
        {% elif (wochentag=='Sonntag') %}
        {% set wochentagsID = '767' %}
        {% endif %}
        {% set heizzeiten1='' %}
        {% set heizzeiten2='' %}
        {% set heizzeiten3='' %}
        {% if counter_anzahl>'0' %}
        {% set heizzeiten1='{"Start": "'+ heizen_start1 + '", "Stop": "' + heizen_ende1 + '", "Unknown": "0000", "Mode": 3}' %}
        {% endif %}
        {% if counter_anzahl>'1' %}
        {% set heizzeiten2='{"Start": "'+ heizen_start2 + '", "Stop": "' + heizen_ende2 + '", "Unknown": "0000", "Mode": 3}' %}
        {% endif %}
        {% if counter_anzahl>'2' %}
        {% set heizzeiten3='{"Start": "'+ heizen_start3 + '", "Stop": "' + heizen_ende3 + '", "Unknown": "0000", "Mode": 3}' %}
        {% endif %}

        {% set cmddata = '{"Count": ' + counter_anzahl + ', "Schedules": [' + heizzeiten1 + heizzeiten2 + heizzeiten3 + ']' %}
        {% set cmd = {'mode': 'write', 'data':[[wochentagsID, cmddata]]} %}
        {{ cmd | to_json }}
      icon: mdi:clock-time-two-outline

Thanks for your help.
Christopher

I don’t seen the config for any “function” I see config for an entity. You don’t run/execute entities.

You can update them. Use the homeassistant.update_entity action assigned to the button tap action if you want to update your number entity when you press the button.

Thanks, that helped me a lot.
Greetings Christopher

Hello,
I don’t want to use HACS.

After spending days trying to find the correct spelling for writing the values, I noticed that triggering via homeassistant.update_entity does not work.
This is the code for the button:

alias: Zeiten Heizung aendern
description: ""
triggers:
  - trigger: state
    entity_id:
      - input_button.heizzeiten_schreiben
conditions: []
actions:
  - action: homeassistant.update_entity
    metadata: {}
    data:
      entity_id:
        - climate.open3e_heizugszeiten_setzen
mode: single


This is nonsensical:

homeassistant.update_entity is an action, not a trigger.

Yes, the trigger is the button that I press manually.
Or have I misunderstood something?

I’ve made some progress now; the code works after I turned it into a script. However, the output is not quite what I need.

script:
  open3e_heizugszeiten_setzen_script:
    alias: "open3e Heizugszeiten setzen script"
    mode: queued
    sequence:
      - service: mqtt.publish
        data:
          qos: "2"
          retain: false
          topic: open3e/cmnd
          payload: >-
            {% set heizen_start1 = states('input_datetime.heizen_start1') [:5]  %} 
            {% set heizen_ende1 = states('input_datetime.heizen_ende1') [:5]  %} 
            {% set heizen_start2 = states('input_datetime.heizen_start2') [:5]  %} 
            {% set heizen_ende2 = states('input_datetime.heizen_ende2') [:5]  %} 
            {% set heizen_start3 = states('input_datetime.heizen_start3') [:5]  %} 
            {% set heizen_ende3 = states('input_datetime.heizen_ende3') [:5]  %}
            {% if  ((heizen_start1=='00:00') and (heizen_ende1=='00:00')) %}
            {% set counter_anzahl = '0' %}
            {% elif  ((heizen_start2=='00:00') and (heizen_ende2=='00:00')) %}
            {% set counter_anzahl = '1' %}
            {% elif  ((heizen_start3=='00:00') and (heizen_ende3=='00:00')) %}
            {% set counter_anzahl = '2' %}
            {% else %}
            {% set counter_anzahl = '3' %}
            {% endif %}
            {% set wochentag = states('input_select.wochentag') %}
            {% if (wochentag=='Montag') %}
            {% set wochentagsID = '761' %}
            {% elif (wochentag=='Dienstag') %}
            {% set wochentagsID = '762' %}
            {% elif (wochentag=='Mittwoch') %}
            {% set wochentagsID = '763' %}
            {% elif (wochentag=='Donnerstag') %}
            {% set wochentagsID = '764' %}
            {% elif (wochentag=='Freitag') %}
            {% set wochentagsID = '765' %}
            {% elif (wochentag=='Samstag') %}
            {% set wochentagsID = '766' %}
            {% elif (wochentag=='Sonntag') %}
            {% set wochentagsID = '767' %}
            {% endif %}
            {% set wochentagsID = (wochentagsID | string ) %}
            {% set heizzeiten1='' %}
            {% set heizzeiten2='' %}
            {% set heizzeiten3='' %}
            {% if counter_anzahl>'0' %}
            {% set heizzeiten1='{"Start": "'+ heizen_start1 + '", "Stop": "' + heizen_ende1 + '", "Unknown": "0000", "Mode": 3}' %}
            {% endif %}
            {% if counter_anzahl>'1' %}
            {% set heizzeiten2='{"Start": "'+ heizen_start2 + '", "Stop": "' + heizen_ende2 + '", "Unknown": "0000", "Mode": 3}' %}
            {% endif %}
            {% if counter_anzahl>'2' %}
            {% set heizzeiten3='{"Start": "'+ heizen_start3 + '", "Stop": "' + heizen_ende3 + '", "Unknown": "0000", "Mode": 3}' %}
            {% endif %}

            {% set cmddata = '{"Count": ' + counter_anzahl + ', "Schedules": [' + heizzeiten1 + heizzeiten2 + heizzeiten3 + ']}' %}
            {{ '{\"mode\":\"write-raw\",\"data\":[[' + wochentagsID + ',' + cmddata + '\"]]}' }}

The output currently looks like this

{"mode":"write-raw","data":[[761={"Count": 1, "Schedules": [{"Start": "08:00", "Stop": "12:00", "Unknown": "0000", "Mode": 3}]}"]]}

However, I need a ’ after the 761=’

{"mode":"write-raw","data":[[761='{"Count": 1, "Schedules": [{"Start": "08:00", "Stop": "12:00", "Unknown": "0000", "Mode": 3}']}"]]}

Is there a way to insert a simple apostrophe into a string?

Your template doesn’t match it’s supposed output. Where is that = coming from?

Generally speaking, it would be easier and less error prone if you would just build actual objects then convert them to json strings in the final expression like you were doing in the original post, rather than building multiple complex strings. Why did you switch from doing that?

If you need help figuring out how to construct the final object or string, it would be helpful if you provide a known working example.

This is the result I need.
It works in the VM on the CLI where open3e runs with the CAN-2-USB adapter.

open3e -j -w 766='{"Count": 1, "Schedules": [{"Start": "08:10", "Stop": "11:00", "Unknown": "0000", "Mode": 3}]}'

The = is needed in this position; other separators such as , or ; do not work.

I have change the Script

            {% set cmddata = '{"Count": ' + counter_anzahl + ', "Schedules": [' + heizzeiten1 + heizzeiten2 + heizzeiten3 + ']}' %}
            {% set result = wochentagsID + "'" + cmddata + "'" %}
            {% set cmd = {'mode': 'write', 'data':[[result]]} %}
            {{ cmd | to_json }}

That’s the result:

{"mode":"write","data":[["761='{\"Count\": 1, \"Schedules\": [{\"Start\": \"08:00\", \"Stop\": \"12:00\", \"Unknown\": \"0000\", \"Mode\": 3}]}'"]]}

It’s not working :frowning: