Programing Python sensor platform. Need help finalizing

I am afraid it still doesn’t work, - getting the max sensor the highest index.
I understand now how the code works, and the logic should be fine.
Tried to format the index as int, without luck. For the icon it works. This way, if you use CustomUI, the color can be picked up from the sensor, in the customize file.

sensor.nilu_xxx_yyy_pm25:
      friendly_name: PM 2.5
      templates:
        icon_color: >
          return attributes.color;


state_sensor = {}    
for i in range(len(myData)):
    myData[i]['color'] = "#{0}".format(myData[i]['color'])
    #myData[i]['index'] = format(myData[i]['index'], "d")
    myData[i]['index'] =  int(myData[i]['index'])

For the naming of the sensor I added "Nilu", and the location names in the sensors Friendly name gets capital letters.

name = ('Nilu' + " " + data.attrs[k]['municipality'].lower() + " " + data.attrs[k]['station'].lower() + " ").title()

Also added icon

@property
def icon(self):
“”“Return the icon of the sensor”""
#jquery biohazard atom xamarin-outline
return ‘mdi:vector-polyline’

UPDATE: @clyra
Looks like the max sensor gets the correct data with this:

row_max = ()
for i in range(len(myData)):
  myData[i]['color'] = "#{0}".format(myData[i]['color'])

  component = myData[i]['component'].replace(".","")
  self.attrs[component] = myData[i]

  if len(row_max) < 1:
    row_max = myData[i]

  if myData[i]['index'] > row_max['index']:
    row_max = myData[i]

self.attrs['max'] = row_max

Appreciate all the help and work you have done. The sensor looks solid, so maybe propose to HA this as a new platform(?)

I think I found the bug… What region/station are you using which has a component with index > 1?

fix in the git. Try it, plz.

I must check http://www.luftkvalitet.info to find values to check on. Or check during rush hours for my local station.

Looks like your latest fix work fine : D

Fun fact, the map in HA displays the station by its lat/longitude attributes .

eleverum

Enhanced the python code with adding the index text (attribute “index_txt”). This way all information for a sensor i gathered, and less coding is necessary for the front end.

for i in range(len(myData)):
  myData[i]['color'] = "#{0}".format(myData[i]['color'])
  
  if myData[i]['index'] == 1:
    myData[i].update({'index_txt' : 'Low health risk'})
  elif  myData[i]['index'] == 2:
    myData[i].update({'index_txt' : 'Moderate health risk'})
  elif myData[i]['index'] == 3:
    myData[i].update({'index_txt' : 'Significant health risk'})
  elif  myData[i]['index'] == 4:
    myData[i].update({'index_txt' : 'Serious health risk'})

  component = myData[i]['component'].replace(".","")
  self.attrs[component] = myData[i]

  if i==0:
    self.attrs['max'] = myData[i]
  elif myData[i]['index'] > self.attrs['max']['index']:
    self.attrs['max'] = myData[i]

nice! Now you should learn about git and GitHub and submit a PR :-). Basically it is a diff from your code and mine so I could merge it on GitHub.

  • I was afraid you would say that. - I’ll look into it : )

Do you know how I can pick up the login in language and use it on the Nilu code? I was thinking making the information text in English and Norwegian.

I don’t think it’s possible directly from the API. Even the examples from here is in english (btw, I can’t read in Norwegian too). Of course, you may translate inside the sensor code.

Thats what I was thinking. Check the login language in the sensor code. If Norwegian, use the Norwegian text, for others, use English.

Is the appropriate way to fork your code, and add my tweaking, and then submit a PR?

One question, the newest API return attributes:
fromTime: 2018-11-17T16:00:00+01:00
toTime: 2018-11-17T17:00:00+01:00
timestep: 3600

Is there a way to enhance the code to minimize the API call, so the code will for example call the API at fromTime + timestep + 5 minutes?
This way call the API just when its needed, minimizing the number of API call.

I don’t know… I only know there’s a scan interval which you can set for the sensor, but I’m not aware of a way to schedule a check.

About the code, I think the best way should you fork it and I should put a link to your repository. You should be the new maintainer of this :-). I should visit Norway one day, but until that I have no use for this sensor.

Uploaded your code with my small mods

Well done!! Congratulation :-). I will update my github to point to yours.