Python Script to alter yaml

Hi

I am trying to alter the modbus.yaml when the user changes an input on the UI (input_number.victron_IP)
It doesnt look like I can do this using an automation, however can I do this with a python script?

I have never used python scripts before so please be kind

Many thanks

you can indeed, but you still might need to reload for it to take effect. i recently did something similar for mqtt in appdaemon:

import socket
import yaml
import os

# Function to get the local IP address
def get_local_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        s.connect(('10.255.255.255', 1))
        IP = s.getsockname()[0]
    except Exception:
        IP = '127.0.0.1'
    finally:
        s.close()
    return IP

# Get the current IP
current_ip = get_local_ip()

# Load the appdaemon configuration file
config_file = '/appdaemon/appdaemon.yaml'
with open(config_file, 'r') as file:
    config = yaml.safe_load(file)

# Replace the with the current IP
config['appdaemon']['plugins']['MQTT']['client_host'] = current_ip

# Write the updated configuration back to the file
with open(config_file, 'w') as file:
    yaml.safe_dump(config, file)

you can this to continuously loop or it up as a crontab job so it starts on boot/checks every x mins/hours

you can check from a terminal by running python nameoffile.py if inside the DIR (change nameoffile to the file name)

this is great news, many thanks. I have to admit I have never done a python script before so forgive me. I have tried to run this but it errors on line 22 (config_file = ‘/appdaemon/appdaemon.yaml’). When I look in the directory I can however see a file called this?

How would I go about opening a file and then replacing specific lines with something from an input_number filed on the UI?

TIA

line 22 (config_file = ‘/appdaemon/appdaemon.yaml’).

If the Python script is in another area/folder, this one will come up. copy and replace it will the full path. i.e. /home/pi/HA/appdaemon/appdaemon.yaml Basically you need to make sure your python file knows where exactly it is on the pi.

“How would I go about opening a file and then replacing specific lines with something from an input_number filed on the UI?” there is a few different methods for this, but appdaemon has something called `listen_state’ that listens for a change on your entity before firing a script.

import appdaemon.plugins.hass.hassapi as hass


class updateSomeText(hass.Hass):
    def initialize(self):

        # Define the file path and the key to update
        self.yaml_file_path = "/appdaemon/apps/configFile.yaml" #this one again if its in the app Damon file you can get away with the short file path, but outside the folder you will need to direct it
        
        # Define the input_number entity to read from - this you can do or define it directly in the listen state.. i think its tidier this way
        self.input_number_entity = "input_number.example_input"
        # Listen for changes in the input_number entity
        self.listen_state(self.updateSomeYAML, self.input_number_entity)

    def updateSomeYAML(self, entity, attribute, old, new, kwargs):
        # Read the current value from the input_number entity
        value = self.get_state(self.input_number_entity)

        # Load the YAML file
        with open(self.yaml_file_path, "r") as file:
            yaml_data = yaml.safe_load(file)

        # Update the value in the YAML data
        yaml_data['someVariable'] = value

        # Write the updated YAML data back to the file
        with open(self.yaml_file_path, "w") as file:
            yaml.dump(yaml_data, file)

        self.log(f"Updated YAML file: {self.yaml_file_path}")

I haven’t tested the above but the general work flow is there.

OK, I must be doing something wrong. I have installed AppDaemon through HACS, but when I use terminal to run the basic hello.py I get an error

"no module named ‘appdaemon’ "

Loading the UI however shows I am running version 4.4.2

Check your pathway. It sounds like it’s in a different folder.

you can always make a test file and change the paths to that to check.


I am confused, it looks like I have it right, but I must be missing something key. Is there anything obvious?