Python script - how to get sensor

I have a Python script which parses some data from Slovenian weather service to get forecast for tomorrow and a day after tomorrow. If I call /bin/python3 /config/python_scripts/arso_parse2.py I get data in readable form:

Time/Day: 15.04.2024 17:00 CEST Ponedeljek CEST (popoldne)
Temperatura: 25 °C
Hitrost vetra: 12 m/s
Padavine: 
Vremenska napoved: pretežno jasno ()

Time/Day: 16.04.2024 8:00 CEST Torek CEST (zjutraj)
Temperatura: 13 °C
Hitrost vetra: 0 m/s
Padavine: 
Vremenska napoved: pretežno oblačno ()

Time/Day: 16.04.2024 17:00 CEST Torek CEST (popoldne)
Temperatura: 6 °C
Hitrost vetra: 8 m/s
Padavine: mod
Vremenska napoved: oblačno (plohe)

Which is OK for a start. My goal is to get this data to sensor, so I could use it in announcements or get info over VA…

If I create a sensor in configuration.yaml:


  - platform: command_line
    name: "Temp Arso Pojutrišnjem"
    unique_id: vreme_temp_pojutrisnjem
    command: "python3 /config/python_scripts/arso_parse2.py"
    unit_of_measurement: "°C"
    value_template: "{{ value_json.Temperatura }}"
    json_attributes:
      - Temperatura
      - Napoved
      - Hitrost vetra
      - Padavine
    scan_interval: 3600 

The sensor is not created…

If I call a service in dev tools:

service: python_script.arso_parse2
data: {}

I get this error: (translation: Could not call service python_script.arso_parse2.)

Storitve ni bilo mogoče poklicati python_script.arso_parse2. Error executing script (ImportError): __import__ not found

My log is set to error and couldn’t find any output in log.

Can someone help me with some advice? Thank you in advance :slight_smile:

That data is not JSON, so you can’t treat it like it is.

Best options are: see if you can get HA to talk directly to the weather service and remove the Python script from the picture; or see if you can get the script to output JSON.

You can work with the data format you have, but it’s not ideal.

Could you post the Python script here?

Sure. Python script is:

import requests
from bs4 import BeautifulSoup

# Fetch XML data
url = "https://meteo.arso.gov.si/uploads/probase/www/fproduct/text/sl/fcast_SI_OSREDNJESLOVENSKA_latest.xml"
response = requests.get(url)

# Check if request was successful
if response.status_code == 200:
    # Parse XML content
    soup = BeautifulSoup(response.content, 'xml')

    # Find all metData elements
    met_data_list = soup.find_all('metData')

    # Iterate over each metData element
    for met_data in met_data_list:
        # Extract time or day information
        valid_day = met_data.find('valid_day').text
        valid_daypart = met_data.find('valid_daypart').text
        valid_time = met_data.find('valid').text
        
        # Extract temperature information
        t_var_desc = met_data.find('t_var_desc').text
        t_var_unit = met_data.find('t_var_unit').text
        t = met_data.find('t').text
        
        # Extract wind speed information
        ff_var_desc = met_data.find('ff_var_desc').text
        ff_var_unit = met_data.find('ff_var_unit').text
        ff_value = met_data.find('ff_value').text
        
        # Extract precipitation information
        rr_decode_text = met_data.find('rr_decodeText').text
        
        # Extract weather forecast information
        nn_short_text = met_data.find('nn_shortText').text
        wwsyn_short_text = met_data.find('wwsyn_shortText').text

        # Print the information
        print(f"Time/Day: {valid_time} {valid_day} ({valid_daypart})")
        print(f"Temperatura: {t} {t_var_unit}")
        print(f"Hitrost vetra: {ff_value} {ff_var_unit}")
        print(f"Padavine: {rr_decode_text}")
        print(f"Vremenska napoved: {nn_short_text} ({wwsyn_short_text})")
        print()  # Print newline after each metData element

else:
    print("Failed to fetch XML data from the URL.")

Well, the weather service provides RSS, XML and HTML formats

Use the rest integration to pull the data you want from the XML with a single call. Let me know if you need help with that, but it looks pretty simple. Which data do you want?

2 Likes

Having had a look at it, you can pull the whole lot in with a single rest sensor:

sensor:
  - platform: rest
    name: ARSO forecast
    resource: https://meteo.arso.gov.si/uploads/probase/www/fproduct/text/sl/fcast_SI_OSREDNJESLOVENSKA_latest.xml
    scan_interval: 86400
    value_template: "{{ now() }}"
    json_attributes_path: "$.data"
    json_attributes:
      - metData

That gives this:

then use an automation to update it at the recommended xx:25 time:

trigger:
  - platform: time_pattern
    minutes: 25
action:
  - service: homeassistant.update_entity
    target:
      entity_id: sensor.arso_forecast

and you can pull out the individual items into template sensors if needed. The equivalent of your Python script is:

{% for i in state_attr('sensor.arso_forecast','metData') %}
Time/Day: {{ i['valid'] }} {{ i['valid_day'] }} ({{ i['valid_daypart'] }})
Temperatura: {{ i['t'] }} {{ i['t_var_unit'] }}
Hitrost vetra: {{ i['ff_value'] }} {{ i['ff_var_unit'] }}
Padavine: {{ i['rr_decodeText'] }}
Vremenska napoved: {{ i['nn_shortText'] }} ({{ i['wwsyn_shortText'] }})
{% endfor %}

which gives:

1 Like

Uau, THANK YOU VERY MUCH @Troon for your solution! Really appreciate it!!

Been strugling with value_template and json_attributes_path and couln’t get it working :slight_smile: but your code works!!!

1 Like

I used this tool: Best XML to JSON Converter Online

I copied the XML from the URL, then pasted it in to that tool, converted to JSON and it became much easier to read:

json_attributes_path is the JSONPath of the element that the attributes you want to pull is stored in: here, it’s $.data and you then want the metData structure (list) as an attribute.

1 Like

Much easier to decypher :slight_smile: Thank you!

1 Like

I don’t know any better way to present data for tomorrow and a day after tomorrow forecast as with use of a list since the metData spits out 3 sets of forecast. So I made a template sensor separate for today [0], tomorrow morning [1] and tomorrow afternoon [2] using this:


  - platform: template
    sensors:
      arso_napoved_jutri_popoldne:
        friendly_name: "ARSO napoved vremena jutri popoldne"
        unique_id: arso_napoved_jutri_popoldne
        value_template: >
          {% set forecast_list = state_attr('sensor.arso_forecast', 'metData') %}
          {% if forecast_list and forecast_list|length >= 3 %}
            Jutri popoldne bo {{ forecast_list[2]['nn_shortText'] }}.
          {% else %}
            Za jutri popoldne ni vremenske napovedi.
          {% endif %}

As there is sometimes some additional shot text added to a primary forecast, I used this template to add this or omit if there is null being presented instead of forecast state. For example partly cloudly [and] showers. This ‘showers’ is that part and if the forecast is just partly cloudly and no second part, the second part has null. So I changed to this:

{% set forecast_list = state_attr('sensor.arso_forecast', 'metData') %}
          {% if forecast_list and forecast_list|length >= 2 %}
            {{ forecast_list[1]['nn_shortText'] }}
            {% if forecast_list[1]['wwsyn_shortText'] != 'null' %}
              {% if forecast_list[1]['wwsyn_shortText'] is not none %}
                ({{ forecast_list[1]['wwsyn_shortText'] }})
              {% endif %}
            {% endif %}
          {% else %}
            Za jutri zjutraj ni vremenske napovedi.
          {% endif %}

I havent tested it though because I’m waiting for fresh forecast :slight_smile:

Hi.
I’m wondering if someone can write me a code to insert into HA that would read the current weather from ARSO for Logatec. I don’t need other data because I have my own weather station, which unfortunately gets data about the current weather from wunderground, which is not nearly accurate.
Thanks for your help.

@Trzinka

I’m currently working on building custom component which fetches from ARSO api. But the downside is it’s a sensor entity and not weather entity (so far). So you can use it only for sensor, if you know what I mean. The data provided are current and forecast for next 5 days.
But it shows current weather conditions in slovenian language. If you prefer english (or for configuring weather template, I’m not there yet :slight_smile:

You can test this one:

and let me know of any error or mistakes…the code is from this weekend :slight_smile:

1 Like

But if you prefer current weather conditions only and no forecast data, you can check this custom component, also mine

It has Ljubljana Bežigrad hardcoded, so you would change python script after you download it to your system for location you desire.

1 Like

Hello.
Thanks for the reply.
I downloaded the file and found that I need to add it in devices & services as shown in the image below and then I can select a location.

when i do what i mentioned i get:
ARSO-Logatec entiti

But it’s a little strange because it shows the situation as it is, unlike the official page:

For ARSO-Weather-Conditions-RSS-Feed-Parser displays the following and clearly shows the weather for Ljubljana?


Is it possible to change the location here?

Thanks.

Yes, I’m aware, the data from ARSO API is for forecast (3h, 6h, 24h) and currently I’m having problems with importing values as current weather conditions from api.
I’m working on it…

As for weather state (sunny - > display as jasno) and in weather CC is shown ‘delno oblačno’ it’s different because weather template needs state in english, but for weather entity (arso_weather CC) it’s originaly ok to be in Slovenian. It’s a bit confused, I’m still struggling to find the right way to transform arso api to hass recognized values.

Did you try to change hardocoded value to Logatec in GitHub - andrejs2/ARSO-Weather-Conditions-RSS-Feed-Parser

1 Like

Check the update of CC.

Hi.
Did you try to change hardocoded value to Logatec in [GitHub - andrejs2/ARSO-Weather-Conditions-RSS-Feed-Parser](https://github.com/andrejs2/ARSO-Weather-Conditions-RSS-Feed-Parser/tree/main)

I didn’t, because I don’t know (I have no programming knowledge) and I also realized that I don’t need this add-on, because I have a home weather station, I get all the data from it except the current state, which I get via GitHub - cytech/Home-Assistant-wundergroundpws: Home Assistant custom component sensor for Weather Underground personal weather station users add-on, which has a problem with displaying the current weather condition, which almost never matches the current weather condition in Logatco. (for example, in Maribor it is raining, here the sun is shining, but it shows me a clear night “confusion”).

Your add-on GitHub - andrejs2/arso_sensor: Custom Component for ARSO Weather for Home Assistant is the sensor I’m interested in and currently shows the closest to accurate weather conditions at our end.

I will monitor both add-ons for a little while and give my opinion.

By the way, would it be possible to make an add-on for https://www.akostest.net/sl/newtest/ based on core/homeassistant/components/speedtestdotnet at dev · home-assistant/core · GitHub or anew.

At the moment thanks for the excellent job done.

Well, the speed test from AKOS, it depends if they provide API key for this. But as far as I know, it’s different service as speed dot net. But this is some other topic …

1 Like

Hi Andrej.

Let’s see what I found out.

For sensor.arso_weather_conditions

I can choose between the following weather stations (Logatec is not among them unlike ARSO Weather Integration where I can choose Logatec)
sensor.arso_weather_conditions-station


you have to change the following (the weather station Postojna is currently selected, which suits me better than Ljubljana)

You get the name of the station like this:

And now weather.arso_weather_conditions_template
I think there is something wrong with the code

  - platform: template
    name: "ARSO Weather Conditions Template"
    unique_id: arso_weather_conditions_template_postojna
# start - I think this part is not correct because entity does not know condition but state??? Of course, I'm not sure if this is the cause of the "unknown" status
    condition_template: >-
      {% set condition = states('sensor.arso_weather_conditions') %}
      {% if condition == 'sunny' and is_state('sun.sun', 'above_horizon') %}
        sunny
      {% elif condition == 'sunny' and is_state('sun.sun', 'below_horizon') %}
        clear-night
      {% else %}
        {{ condition }}
      {% endif %}
# end
    temperature_template: "{{ state_attr('sensor.arso_weather_conditions', 'temperature') | float }}"
    temperature_unit: "°C"
    humidity_template: "{{ state_attr('sensor.arso_weather_conditions', 'humidity') | float }}"
    pressure_template: "{{ state_attr('sensor.arso_weather_conditions', 'pressure') | float }}"
    pressure_unit: "mbar"
    wind_speed_template: "{{ state_attr('sensor.arso_weather_conditions', 'wind_speed') | float }}"
    wind_speed_unit: "m/s"
    wind_bearing_template: "{{ state_attr('sensor.arso_weather_conditions', 'wind_bearing') }}"
    visibility_template: "{{ state_attr('sensor.arso_weather_conditions', 'visibility') | float }}"
    visibility_unit: "km"
    attribution_template: "Data provided by Agencija Republike Slovenije za okolje"
    forecast_daily_template: "{{ states('sensor.arso_napoved_danes_2') }}"
    dew_point_template: "{{ state_attr('sensor.arso_weather_conditions', 'dew_point') | float(0) if state_attr('sensor.arso_weather_conditions', 'dew_point') is not none else 'unknown' }} °C"

What are you trying to say? Did the weather sensor report state?
If not, there is a flaw in a code because the weather integration allows only one state - ‘cloudy’ and not combined states ‘oblačno, rosi’ for instance. I’m aware of the flaws in integration.
My new integration I’m just finishing will try to ‘merge’ multiple weather states (‘pojavi’) which ARSO is reporting (clould coverage + weather condition) into only numerus clausus states from weather integration from HA. It’s a bit complicated because ARSO is having realy detailed weather conditions reporting espacally from manned observation stations.

Stay tuned for a new version for ARSO Weather.
Thanks for reporting!

Sorry for the confusion. I just had to do something while I was writing and I’d rather just post than write again. I have now corrected the text. I wrote about Custom ARSO Weather Component for Home Assistant. I wrote with the intention that if you find something useful in the description of the add-on for installation, because I, as a layman, needed for quite some time what you meant when you wrote, if I tried to change the location of the weather station.