Virtual light sensor

Ok thanks. But cloud coverage value is always the same over all api ? % 0 for clear and 100 for cloud ? I don’t know if replace :

 sensor.dark_sky_cloud_coverage 

by

sensor.openweathermap_cloud

is enough?

I don’t use the openweathermap platform, so I’m not sure what the sensor entities are called. However, I did confirm that cloud coverage on that platform is represented the same as it is in DarkSky: 0 meaning no clouds, 100 means complete cloud coverage.

Ok thanks !! so i just need to find another sensor with this parameter to replace darksky cloud coveage sensor.

right. add the openweathermap integration, check and see what the entity id is for cloud coverage, then edit this template to use that entity id.

1 Like

Trying your code as we speak, with the usage of OpenWeather instead of Dark Sky:

- platform: openweathermap
  api_key: my_API_key
  monitored_conditions:
     - clouds

and than replace “sensor.dark_sky_cloud_coverage” by “sensor.huis_cloud_coverage” in the code of @swiftlyfalling .

I will report back, after some days… Thanks!

I’m using openweathermap myself, now, and it’s working wonderfully.

I have been working on the code of @swiftlyfalling and trying to add Solar Irradiance into the code. By that the sunlight% will depend on:

  1. Solar Irradiance senor
  2. Cloud Coverage senor
  3. Sun Elevation senor

I found (assume) that the max Solar Irradiance we can get is 1.000 W/m2 (see here). That means that the calculation can be really simple; just divide the number by 10 to get the percentage. So; 402 W/m2 is equal to 40.2% Irradiance. Next, I subtract the Irradiance% from the Cloud Coverage% to get an adjusted percentage for the impact of clouds on the sunlight.

I have set the max_elevation at 70 degr, because it is maximum 61degr in my place at the world. Find your max here.

The rest of the code is same as @swiftlyfalling wrote:

    sunlight_pct: #https://community.home-assistant.io/t/virtual-light-sensor/31975/34
      friendly_name: "Percentage of Sunlight"
      entity_id:
        - sun.sun
        - sensor.huis_cloud_coverage
        - sensor.br_irradiance
      value_template: >-
        {%- set irradiance = states('sensor.br_irradiance') | float %} 
        {%- set irradiance_pct = irradiance / 10 %} {# max irradiance = 1.000, so  irradiance/10 = pct. See https://en.wikipedia.org/wiki/Solar_irradiance#:~:text=Average%20annual%20solar%20radiation%20arriving,level%20on%20a%20clear%20day #}
        {%- set elevation = state_attr('sun.sun','elevation') | float %}
        {%- set cloud_coverage = states('sensor.huis_cloud_coverage') | float %}
        {%- set adjusted_clouds = cloud_coverage -  irradiance_pct %}
        {%- set cloud_factor = (1 - (0.75 * ( adjusted_clouds / 100) ** 3 )) %}
        {%- set min_elevation = -6 %}
        {%- set max_elevation = 70 %} {# find max_elevation at https://www.suncalc.org/#}
        {%- set adjusted_elevation = elevation - min_elevation %}
        {%- set adjusted_elevation = [adjusted_elevation,0] | max %}
        {%- set adjusted_elevation = [adjusted_elevation,max_elevation - min_elevation] | min %}
        {%- set adjusted_elevation = adjusted_elevation / (max_elevation - min_elevation) %}
        {%- set adjusted_elevation = adjusted_elevation %}
        {%- set adjusted_elevation = adjusted_elevation * 100 %}
        {%- set brightness = adjusted_elevation * cloud_factor %}
        {{ brightness | round }}
      unit_of_measurement: '%'
      device_class: 'illuminance'

And the outcome is a nice virtual light sensor:

Thanks again @swiftlyfalling for your awesome work !!!

4 Likes

How to create the solar irradiance sensor?
How to find the value of max_elevation in the suncalc.org site?
My coordinates are:

latitude: 42.0472856
longitude: 13.4308795

instead of moving your questions from Automations to turn on lights based on actual season
you could also try to find them yourself and check the weather integrations for irradiance of course…

you might end up at Weatherbit, which has it all.

I moved here because i was thinking these are two different topics (also if i can use them in the previous topic). BTW i found the climacell integration has already the solar irradiance, so my problem here is solved, but not the max_elevation…

max_elevation can be found at https://www.suncalc.org/. Type in your coordinates here and hit [apply]:

image

Then scroll down and look for the date for Jun Solstice
image
Adjust the current date/time to this date and set Azimuth at 180degr (by adjusting time with the time-slider)
image
The Altitude will be your max_elevation. Most likely 70degr :smiley:

This explains a bit more:

2 Likes

And there is a perfect integration available for Max elevation.
Have a look at Phil s other work
But you should know I think, as you already use that?

You mean the enhanced sun sensor? Or what?

or what?
No, the enhanced Sun integration is now deprecated and has been replaced by the other Sun integration Phil wrote, as is noted in the first line of the readme.

You really should start reading the documentation …

Yes, you’re right, but do you think it’s easy to read tons of lines of documentation for me with not native english language? :roll_eyes: :pray:

Hi Barman!

Are you still using this? Bumping, because I copied your adaptation before, using the Weatherbit irradiance sensor, which now has almost become useless (because weatherbit diminished the free api calls to 50 a day…)

so Looking at the Buienradar again, made me remember that doesnt provide the irradiance sensor for my location. se also Buienradar missing irradiance · Issue #51581 · home-assistant/core · GitHub

I did however find it in the new Tomorrow.io integration, so a simple replacement was easy. Just letting you know that is available.

For the Buienradar: would you know where to find the Api list of stations? I might want to try and find a nearby station that does carry the irradiance, and setup a second station for that specifically.

I know we should be able to check that, but forgot how we can get to that open data listing.

thanks!

update

Ive found the 1.0 listing here: https://data.buienradar.nl/1.0/feed/xml and found another station not too distant with an actual value for zonintensiteitWM2, setup the second Buienradar integration et voila, now have a BR irradiance sensor :wink:

checking that Station in https://data.buienradar.nl/2.0/feed/json, I notice its called sunpower

so posting that for future reference here.

Hi Marius,
Yes, I am still using it and I indeed thought the 50 a day weatherbit free api calls would be an issue, but my sensor still works well enough. The reason I want to stay at weatherbit is that the weather station is really close to my home… See below the graph before weatherbit lowered the daily updates:

And now with the 50 a day it looks quite the same (although a bit more stepped):

I also updated my code with some changes to gain more control. Below my latest code:

    sunlight_pct: 
      friendly_name: "Percentage of Sunlight"
      icon_template: mdi:brightness-auto
      unit_of_measurement: '%'
      value_template: |
        {%- set factors = namespace(condition='',sun='') %}

        {%- set irradiance = states('sensor.weatherbit_solar_radiation') | float %} 
        {%- set elevation = state_attr('sun.sun','elevation') | float %}
        {%- set cloud_coverage = states('sensor.weatherbit_cloud_coverage') | float %}
        {%- set current_condition = states("weather.weatherbit") %}

        {%- set current_condition = current_condition|lower|replace("lightning-rainy","lightning")|replace("partly cloudy w/ ","")|replace("mostly cloudy w/ ","")|replace("freezing","")|replace("and","")|replace("-", " ")|replace("_", " ")|replace("(","")|replace(")","")|replace(" ", "") %}
        
        {%- set condition_factors = {
          "10000": ("clear", "clearnight", "sunny", "windy", "exceptional"),
          "7500": ("partlycloudy","partlysunny", "mostlysunny", "mostlyclear", "hazy", "hazysunshine", "intermittentclouds"),
          "2500": ("cloudy", "mostlycloudy"),
          "500": ("fog", "rainy", "showers", "pouring", "snowy", "snowyheavy", "snowyrainy", "flurries", "chanceflurries", "chancerain", "chancesleet", "drearyovercast", "sleet"),
          "50": ("hail", "lightning", "tstorms")
        } %}
        {%- for factor in condition_factors if current_condition in condition_factors[factor] %}
          {%- set factors.condition = factor %}
        {%- endfor %}

        {%- set illuminance_pct = (factors.condition |int) / 100  %} {# max illuminance is 10.000, , so  illuminance/100 = pct #}
        {%- set irradiance_pct = irradiance / 10 %} {# max irradiance = 1.000, so  irradiance/10 = pct. See https://en.wikipedia.org/wiki/Solar_irradiance#:~:text=Average%20annual%20solar%20radiation%20arriving,level%20on%20a%20clear%20day #}
        {%- set adjusted_clouds = cloud_coverage -  irradiance_pct - illuminance_pct %}
        {%- set adjusted_clouds = [adjusted_clouds,0.00000001] |max %} {# cannot devide by zero and exclude negative result #}
        {%- set cloud_factor = (1 - (0.75 * ( adjusted_clouds / 100) ** 3 )) %}
        {%- set min_elevation = -6 %}
        {%- set max_elevation = 61 %} {# find max_elevation at https://www.suncalc.org/#}
        {%- set adjusted_elevation = elevation - min_elevation %}
        {%- set adjusted_elevation = [adjusted_elevation,0] | max %}
        {%- set adjusted_elevation = [adjusted_elevation,max_elevation - min_elevation] | min %}
        {%- set adjusted_elevation = adjusted_elevation / (max_elevation - min_elevation) %}
        {%- set adjusted_elevation = adjusted_elevation %}
        {%- set adjusted_elevation = adjusted_elevation * 100 %}
        {%- set result = adjusted_elevation * cloud_factor %}
        {%- set result = [result,100] |min %} {# max 100% #}
        {{ result | round }}

4 Likes

nice, let me check that . what specifically do those factors do, hmm

well, a lot so it seems:

got to fix those defaults though :wink: on float and int.

Anybody else noticing (perhaps @swiftlyfalling?) that sunlight_pct is behaving differently in summer this year?

It looks like the Weatherbit integration isn’t going to be supported in the coming months.

Has anyone found an alternate integration that works well with this sensor?