Help creating an automation

I’m currently migration some of my node-red automations. In this case there’s one that I don’t know how to replicate in HA.
It’s a slow coast for a stop EV charging action.
There’s a button that says “Stop charge” :grin:
When you push it:

  1. The value for the charge power limiter is stored.
  2. From there, every 1s, 230w are substracted to that value, and published to mqtt.
  3. When this setting arrives to 500w, the subtraction is stopped.
  4. Waits for a minute.
  5. And then restores the original value of the setting (and publishes)

The effect is that the charge stops without feeding back in excess my home battery system, and as a side effect, the car ejects the hose-plug. Then after a minute all is restored and you can plug and charge again, so golden!

Any ideas in how to do this?

from the docs:

trigger:
  - platform: state
    entity_id: button.my_button
action:
  - service: notify.frenck
    data:
      message: "My button has been pressed!"

so you could:

trigger:
  - platform: state
    entity_id: button.stop_charge
action:
  - service: input_number.set_value
     target:
        entity_id: input_number.charge_power
      data:
        value: "{{ states('sensor.charge_power') }}"
  - service: input_number.set_value
    target:
      entity_id: input_number.new_charge_power
    data:
      value: "{{ states('input_number.charge_power') }}"
  - repeat:
      sequence:
        - service: mqtt_publish_service_here
          data: state of input_number.new_charge_power
        - service: input_number.set_value
           target:
             entity_id: input_number.charge_power
           data:
             value: "{{ states('input_number.new_charge_power') | float - 230 }}"
        - delay: "00:00:01"
      until:
        - condition: numeric_state
          entity_id: input_number.new_charge_power
          below: 500
  - delay: "00:01:00"
  - service: input_number.set_value
    target:
      entity_id: input_number.new_charge_power
    data:
      value: "{{ states('input_number.charge_power') }}"
  - publish_mqtt_service_here

completely quickly thrown together and untested.

And be aware that it won’t survive restarts or reloading automations.

but hopefully it will get you started.

1 Like

Awesome! Thanks. I was wondering if it could be possible to use a variable and avoid creating a new entity.

that’s might be possible but I’ve never really used variables in automations so I don’t know if those are internally storable or not.

I’ve tried, but couldn’t make it work. It would had been possible if buttons could be defined in yaml. Like this:

button:
  - name: evcharger_stopcharging
    icon: mdi:stop
    tap_action:
      action: call-service
      service: script.evcharger_charge_stop
      data:
        previous_power_limit: "{{ states('number.evcharger_settings_ev_limit')|int(7330) }}"

Passing the current value as a parameter could had simplified the while thing, but it’s not possible.
(I am building all of this in packages, so I have a nice SoC an can share it.)

I had to convert the script to an automation, creating dummy entities…
So I finally done like you suggested:

automation:
  - id: evcharger_charge_stop
    alias: evcharger_charge_stop
    trigger:
      - platform: state
        entity_id: input_button.evcharger_stopcharging
    action:
      - service: input_number.set_value
        target:
          entity_id: input_number.evcharger_settings_powerlimit_previousvalue
        data:
          value: "{{ states('number.evcharger_settings_ev_limit') }}"
      - repeat:
          sequence:
            - service: number.set_value
              target:
                entity_id: number.evcharger_settings_ev_limit
              data:
                value: "{{ states('number.evcharger_settings_ev_limit')|int(7330) - 230 }}"
            - delay: "00:00:01"
          until:
            - condition: numeric_state
              entity_id: number.evcharger_settings_ev_limit
              below: 500

      - delay: "00:01:00"
      - service: number.set_value
        target:
          entity_id: number.evcharger_settings_ev_limit
        data:
          value: "{{ states('input_number.evcharger_settings_powerlimit_previousvalue')|int }}"
          # value: "{{ previous_power_limit }}"

I’ve also tried to give the charge power limit entity a custom attribute, but I couldn’t set it from a script.
I suppose HA still has a long path to go.

PD: So many thanks for your help. Also I converted the entity to a mqtt-number entity so mqtt publishing is automatically managed.

right.

the example I posted was an automation and not a script since it had a trigger in it.

glad you got it working tho.

I finally came out with this, courtesy of “variables” section:

script:
  evcharger_charge_stop:
    variables:
      limit_power_previous: "{{ states('number.evcharger_settings_limit_power')|int(7330) }}"
    sequence:
      - service: number.set_value
        target:
          entity_id: number.evcharger_settings_limit_power
        data:
          value: "{{ states('sensor.energy_acloads_ev_power')|float(7440) }}"
      - repeat:
          while:
            - condition: numeric_state
              entity_id: number.evcharger_settings_limit_power
              above: 500
          sequence:
            - service: number.set_value
              target:
                entity_id: number.evcharger_settings_limit_power
              data:
                value: "{{ states('number.evcharger_settings_limit_power')|float(7330) - 230 }}"
            - delay:
                seconds: 1
      - delay:
          minutes: 5
      - service: number.set_value
        target:
          entity_id: number.evcharger_settings_limit_power
        data:
          value: '{{ limit_power_previous }}'

Now I can call it from a regular button or from another automation. And doesn’t need the button or input_number to be defined.
:grinning_face_with_smiling_eyes: :grinning_face_with_smiling_eyes: :grinning_face_with_smiling_eyes:

1 Like