Edit lovelace weather-card.js to add a missing attribute/sensor

When populating the original lovelace weather-card with meteo_france weather sensor, the visibility doesn’t show. The attribute is missing from the weather entity but is available in a dedicated sensor.

Screenshot 2021-08-16 at 13.35.48

I would like to know if it’s possible to force weather-card.js to load a specific sensor state ?
(I like the simplicity of this rarely updated card, editing the js code will not be so boring to manage in the future)

I have found the relevant lines in the card’s code but can’t find the proper code to replace "stateObj.attributes.visibility:

      <ha-icon icon="mdi:weather-fog"></ha-icon> 
      ${stateObj.attributes.visibility}
      <span class="unit">
        ${this.getUnit("length")}
      </span>
    </li>

Is it possible at least ?

Thanks for your help.

You can change the templates to whatever sensors you want. Then change the card to point to this weather selection.
Edit: in configuration.yaml

weather:
  - platform: template
    name: My Local Weather
    condition_template: "{{ states('sensor.wf_weather') }}"
    temperature_template: "{{ states('sensor.wf_temperature') | float}}"
    humidity_template: "{{ states('sensor.wf_humidity')| int }}"
    pressure_template: "{{ states('sensor.wf_sea_level_pressure')| float }}"
    wind_speed_template: "{{ ( states('sensor.wf_wind_speed_avg') | float * 18 / 5 ) | round(2) }}"
    wind_bearing_template: "{{ states('sensor.wf_wind_bearing_avg')| int }}"
    visibility_template: "{{ states('sensor.wf_visibility')| float }}"
    forecast_template: "{{ state_attr('sensor.wf_weather', 'hourly_forecast') }}"

If you don’t have visibility from any other source, you can also calculate an estimate based on your elevation and humidity in the air:
First find your max possible visibility:

1.22459 * sqrt (height in feet above sea level) = Distance to the horizon in nautical miles
using 1.2 will yield good accuracy also

3.56972 * sqrt (height in meters above sea level) = Distance to the horizon in km
using 3.6 will yield good accuracy also

Then:
Tair = air temperature
Tdp = Dew Point
In F:
[0.63(Tair - Tdp) - 1.15]/10
In C:
[1.13(Tair - Tdp) - 1.15]/10

With this result multiply it to the max visibility to determine visibility based on conditions (dew point).

The below code is in python taken from a PR I did recently for a Personal Weather Station setup for WeatherFlow Tempest to MQTT to HomeAssistant:

    async def visibility(self, elevation, temp, humidity):
        """Returns the visibility.
           Input:
               Elevation in Meters
               Temperature in Celcius
               Humidity in percent
        """
        if temp is None or elevation is None or humidity is None:
            return None

        dewpoint_c = await self.dewpoint(temp, humidity, True)
        # Set minimum elevation for cases of stations below sea level
        if elevation > 2:
            elv_min = float(elevation)
        else:
            elv_min = float(2)

        # Max possible visibility to horizon (units km)
        mv = float(3.56972 * math.sqrt(elv_min))

        # Percent reduction based on quatity of water in air (no units)
        # 76 percent of visibility variation can be accounted for by humidity accourding to US-NOAA.
        pr_a = float((1.13 * abs(temp - dewpoint_c)-1.15)/10)
        if pr_a > 1:
            # Prevent visibility exceeding maximum distance
            pr = float(1)
        elif pr_a < 0.025:
            # Prevent visibility below minimum distance
            pr = float(0.025)
        else:
            pr = pr_a

        # Visibility in km to horizon
        vis = float(mv * pr)

        if self._unit_system == UNITS_IMPERIAL:
            # Originally was in nautical miles; HA displays miles as imperial, therefore converted to miles
            return round(vis/1.609344, 1)
        return round(vis, 1)

You should be able to make a template sensor from the above, if you need help let me know. But I don’t think you will since you can just point the template sensor to the sensor you can already get for visibility.

1 Like

Thanks for this clever tips @GlennHA !

I managed to create the weather entity but unfortunately the “forecast_template” breaks the entity.

weather:

  • platform: template
    name: Home
    condition_template: “{{ states(‘weather.saint_tropez’) }}”
    temperature_template: “{{ states.weather.saint_tropez.attributes.temperature | float}}”
    humidity_template: “{{ states.weather.saint_tropez.attributes.humidity | float}}”
    pressure_template: “{{ states.weather.saint_tropez.attributes.pressure | float}}”
    wind_speed_template: “{{ states.weather.saint_tropez.attributes.wind_speed | float}}”
    wind_bearing_template: “{{ states.weather.saint_tropez.attributes.wind_bearing | float}}”
    visibility_template: “{{ states(‘sensor.saint_tropez_visibility’) | float}}”
    #forecast_template: “{{ state_attr(‘weather.saint_tropez’, ‘forecast’) }}”

Screenshot 2021-08-16 at 19.14.13

Do you have any idea on how to properly format the forecast_template ?

edit: same error not fixed https://www.gitmemory.com/issue/home-assistant/core/47450/791954181

Try this:

forecast_template: "{{ state_attr('weather.saint_tropez', 'daily_forecast') }}"

options are:
hourly_forecast
or
daily_forecast
Looks like you have a daily, unless there is more that I can’t see, so try daily_forecast

edit: or try {{ states(‘weather.saint_tropez’, ‘forecast’’)}}

“{{ states.weather.saint_tropez.attributes.forecast }}” makes the entity disappear.

“{{ state_attr(‘sensor.saint_tropez’, ‘forecast’) }}” & hourly_forecast & daily_forecast are not working but the weather entity doesn’t disappear.

Well…on a good note you have visibility, so that worked.
I’m sure there is some simple answer that is alluding me at the moment.

Edit just in case you didn’t see it: https://www.home-assistant.io/integrations/weather.template/

1 Like

Thanks for your help @GlennHA