Australian Weather Forecast using BOM Public FTP

Ah ok, these asses decided to use spaces in the names. First off, there was a typo in my original template to you. It should have been this:

{{ state_attr('sensor.bom_forecast_[location]_{}'.format(5 - now().weekday()), 'max_temp_c') }}

If that still doesn’t work, try this:

{{ state_attr('sensor.bom_forecast_[location]_{}'.format(5 - now().weekday()), 'Max Temp C') }}

And worse case if that doesn’t work:

{{ states.sensor['bom_forecast_[location]_{}'.format(5 - now().weekday())].attributes['Max Temp C'] }}

Whoa there dude. It’s @BrendanMoran’s first go at this and he’s done way more than I ever could. For which a lot of us here are very grateful as the scraper we were using has started to be blocked. He’s still working on this custom component too.

In regards to your templates, I get ‘None’, ‘None’ and and 'Error rendering template: UndefinedError: ‘None’ has no attribute ‘attributes’ respectively.

I reckon we give Brendan a bit of time to have a look at it.

Edit: scratch that. The last two templates work. I forgot to put my location in [location].

Thanks for your help @petro.

My mistake, but if the attributes had the correct name format (no capitals and underscores) when referenced you wouldn’t be having these issues. Using spaces in the names doesn’t allow you to use it as an object unless the scraper formats and changes it to remove capitals and add underscores. But if that were also the case, when accessing the attributes, i’d expect the keys to reflect that change. Just my 2 cents, because it adds confusion.

I’m sure he’ll take that on board with the next revision.

And as always, your assistance is much appreciated too.

To sum up @Mahko_Mahko, either of these two templates should do what you want:

{{ state_attr('sensor.bom_forecast_[location]_{}'.format(5 - now().weekday()), 'Max Temp C') }}

{{ states.sensor['bom_forecast_[location]_{}'.format(5 - now().weekday())].attributes['Max Temp C'] }} 

Just dont forget to replace [location] with the location your sensors use.

If you want the keys to match the attributes, change this function in the custom component

    def device_state_attributes(self):
        """Return the state attributes of the sensor."""
        attr = {
            ATTR_ICON: self._bom_forecast_data.get_reading('icon', self._index),
            ATTR_ATTRIBUTION: CONF_ATTRIBUTION,
        }
        for condition in self._conditions:
            attribute = self._bom_forecast_data.get_reading(condition, self._index)
            if attribute != 'n/a':
                attr[SENSOR_TYPES[condition][1].lower().replace(' ','_')] = attribute
        if self._name:
            attr['name'] = self._name

        weather_forecast_date_string = self._bom_forecast_data.get_start_time_local(self._index).replace(":","")
        weather_forecast_datetime = datetime.datetime.strptime(weather_forecast_date_string, "%Y-%m-%dT%H%M%S%z")
        attr[ATTR_FRIENDLY_NAME] =  weather_forecast_datetime.strftime("%a, %e %b")            
        
        attr["product_id"] = self._product_id
        attr["product_location"] = PRODUCT_ID_LAT_LON_LOCATION[self._product_id][2]
        
        return attr
1 Like

I guess my goal with the friendly mode was to make it as user friendly and aesthetically pleasing as possible, which is my reasoning for using capitalisation and spaces for attributes. But you are right, that is quite a non-standard way to do things when compared to all other components and will create confusion down the track.
No offence taken Tom.
Petro, I understand you didn’t know the background of this discussion but calling a contributor an ass is a horrible default stance to take. We should be lifting each other up by default, work together, ya know?

1 Like

I didn’t mean to call you an ass. I assumed this was a standard component and didn’t realize this was in a custom component thread. I’ve had run in’s with the dev’s. Any change I have made has always been met with an insane amount of criticism from the dev’s. I assumed that this went through that process and they made this conscious decision to have a miss matching attribute system. TLDR: I’m salty with ‘some’ dev’s and I’m sorry for directing that at you.

3 Likes

Thanks for your (fast) help all. I’ll probably implement this weekend and post some final automations/scripts.

And just a further reinforcement that I’m very thankful to @BrendanMoran for his excellent work on this. I don’t know any python, but I’m inspired to have a crack at something too having seen what is possible with some determination. Nice one.

1 Like

Thanks for the apology and I’ll take it as a compliment that you thought it was a standard component. All good.

3 Likes

@petro really rocks and he is absolutely awesome with templates and stuff. Can’t speak too highly of him. Misunderstandings abound. Group hug guys!

Awesome work here too Brendan. I’m not blocked on BOM and I changed the scan interval to a couple of hours just in case but if I do get blocked I’ll be using this then. Thanks.

1 Like

So here’s my final script for telling me the weekends weather forecast, thanks to help from the above posts.

Because I have my friendly format set up like this in my config

friendly: True
friendly_state_format: '{min} to {max} degrees. {chance_of_rain} chance of rain. {summary}'

Then I just put this script in my rising/sleep routines to get a weekend forecast from Wed to Fri.

forecast_this_weekend:
  sequence:
  - condition:
    condition: template
    value_template: "{{now().weekday() > 1 and now().weekday() < 5}}"
  - service: notify.google_assistant
    data_template:
      message: On Saturday, the weather summary is {{ states.sensor['bom_forecast_melbourne_{}'.format(5 - now().weekday())].state }} On Sunday, the weather summary is {{ states.sensor['bom_forecast_melbourne_{}'.format(6 - now().weekday())].state }}

I’ve only done some basic testing, coz I’m and in a bit of a rush + testing is a little harder on the weekend, but it seems to be doing the job of announcing the weather over my Google Home (via Google Assistant Webserver).

Thanks again @tom_l & @petro.

1 Like

Jinja has the power to make this work (also easier to read):

  - condition:
    condition: template
    value_template: "{{ 1 < now().weekday() < 5 }}"

Oh yeah, that’s way more concise/elegant. Forgot about that year 10 math syntax! Ta.

1 Like

Hey, I’m starting to tinker with Lovelace UI, and the weather cards are pretty nice. From what I understand your source needs to be be a “weather component” rather than “sensors”. Any thoughts on feasibility/effort/demand/value of changing or forking this as a weather component? I really dunno how that all works, just testing the waters… Lovelace UI Weather Forecast

Edit: Maybe I need to read some of the other threads on the topic a bit more carefully, I think there may be a way… I’ll look closer later, but will leave the post for thought fodder.

Another edit: It didn’t look so straight forward, so I’ve given up on it for the moment.

I’m flat out with work and family for the next 2 months or so (moving house, plus a baby and a toddler running around) so I am going to have to put updates to this custom component on hold for a while.

3 Likes

Hi Brendan, just FYI, my log is being flooded with these errors:

Error doing job: Task exception was never retrieved

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 312, in async_update_ha_state
    self.entity_id, state, attr, self.force_update, self._context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 903, in async_set
    context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 673, in __init__
    "State max length is 255 characters.").format(entity_id))
homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: sensor.bom_hobart_tomorrow_detailed_summary_1. State max length is 255 characters.

Might have to limit the length of the scraped data when you get a chance to have a look at this again.

Hey @BrendanMoran that’s fantastic and way better than my scrape version! It worked straight away for me using hassio :grinning:

Any chance you could add fire_danger and uv_alert as additional fields? That’d be ace :sunglasses:

Firstly, a huge kudos to @BrendanMoran for developing this sensor. I’d started something like this a couple of months ago but couldn’t have achieved what Brendan has (as I have no prior Python experience) so thanks for all of your efforts Brendan.

One of the main things I wanted to achieve was to see if there was rainfall predicted in the next few days so that I could prevent my HA controlled sprinklers from coming on.
When I first tried Brendan’s sensor, it allows a great display with the friendly sensors but to use the min_max sensor to get the highest level of predicted rainfall, I needed easy access to the chance_of_rain value which is difficult as an attribute. So I made a mod to this sensor to allow both the friendly and separate sensors to be created at the same time rather than having one or the other.

If it is of use to anyone else, I’ll try and figure out how to submit the change to GitHub for inclusion in Brendan’s sensor.

HI Justin,

Sounds like a really useful addition. Now what are you like at pulling out the uv_alert and fire_danger fields?

:smile: