Windrose Chart using BarPolar Chart #369
Replies: 16 comments 26 replies
-
Wow, really cool!
There's a way! type: custom:plotly-graph
fn: |
$ex vars.the_function = (vars, minSpeed, maxSpeed)=>{
[ the code here]
}
entities:
- entity: sensor.gw2000b_wind_direction
r: "$ex vars.the_function(vars, 0, 0)"
- entity: sensor.gw2000b_wind_direction
r: "$ex vars.the_function(vars, 0, 5)"
... There's actually another option too using entity defaults. |
Beta Was this translation helpful? Give feedback.
-
I Revised the first post to include fixes and to incorporate your suggestions to make the javascript function generic. |
Beta Was this translation helpful? Give feedback.
-
I really want to steal this... LOL But when I place this code I'm attaching into the yaml editor of the plotly card, I get "bad indentation of a mapping entry (13-4). Can you look at what is wrong here? The code function isn't working for this, so pardon the text file... I'm not sure how else to do it cleanly. Thanks! |
Beta Was this translation helpful? Give feedback.
-
I certainly appreciate all the help. There is a pretty steep learning curve to all this for us "non-coders". Those of you who volunteer your efforts are never appreciated enough, it seems. Your changes are progress. It looks cleaner after your updates than the original post did, as in nothing is cut off now by the legend like the initial code. I still got the bad indentation error in this area though...
That's line 13 in the code. If I increase two spaces on that line the chart will display, but the labels don't show. Instead it shows the degrees, like so... I'm not sure what is breaking the labels with that indentation issue.. |
Beta Was this translation helpful? Give feedback.
-
I've updated the first post to add specific colors by including the following after each marker:
autocolorscale: false
color: blue # your choice Feel free also to change the wind speed ranges. Leave (0, 0) if you with to have the no-wind ball at the center and follow the convention that the Thanks for your interest in, debugging, and suggesting enhancements to my example! |
Beta Was this translation helpful? Give feedback.
-
Add this: layout:
legend:
orientation: h
font:
color: black See: https://plotly.com/javascript/reference/barpolar/#barpolar-legend |
Beta Was this translation helpful? Give feedback.
-
Outstanding! I appreciate everything. I'm sure I'll be around asking more newbie questions, but once I figure out the basic reusable components, I will be better able to tinker and learn. |
Beta Was this translation helpful? Give feedback.
-
Added this, because the zoom functions are pretty unnecessary for this card...
|
Beta Was this translation helpful? Give feedback.
-
The code in the first post is revised to:
|
Beta Was this translation helpful? Give feedback.
-
Can one of you put two of these cards side-by-side in a grid card or horizontal stack and see if you are able to reproduce a strange issue I'm having? The cards wiggle and shrink until they are unreadable. |
Beta Was this translation helpful? Give feedback.
-
And I thought I'd have to spend weeks trying to help you with that. I think that sums up our skill levels pretty well... LOL That does help, thank you. |
Beta Was this translation helpful? Give feedback.
-
Updated the code to show a more prominent Current Wind Direction Marker, to refresh more frequently, and to work better for both my mobile and my desktop Weather dashboard displays. (I'm getting a better sense of JS itself and how it works in Plotly (HA). I have always needed a real world project to learn a programming language. Just ask my poor Fortran professor and the CS Lab staff who fed my punch cards into the reader.) |
Beta Was this translation helpful? Give feedback.
-
Updated code to show "current" wind speed and direction and some other changes/fixes. See First Post. |
Beta Was this translation helpful? Give feedback.
-
I added a marker showing the maximum wind gust and its direction. This snippet added at the end of the yaml should make it work if you are not using the full (updated) configuration above. - entity: sensor.gw2000b_wind_gust
internal: true
filters:
- resample: 15s
dn: $fn ({ ys, vars }) => { vars.windGusts = ys } # Plot a marker for the direction of the maximum wind gust during the time window
- entity: ""
type: barpolar
base: 0
opacity: 0.6
# See: https://stackoverflow.com/questions/11301438/return-index-of-greatest-value-in-an-array
# Note: this will return the FIRST index that matches the maximum windspeed. will it be unique and "the" right "last" direction? Hard to say. TBD
fn: "$ex vars.maxGustIndex = vars.windGusts.reduce((iMax, x, i, arr) => x > arr[iMax] ? i : iMax, 0)"
gn: $ex vars.maxGust = vars.windGusts[vars.maxGustIndex]
# slice below creates an array, [1] is just a nominal array to help with the length of the marker
hn: $ex vars.maxGustDirection = vars.windDirections[vars.maxGustIndex].slice(-1)
r: $ex vars.r = vars.windRose( vars.maxGustDirection, [1], 0, 1000 ).map((a) => a * (Math.max(Math.max(...vars.totals), 15) / 100))
theta: $ex vars.theta
name: "$ex `Max Gust Bearing<br>${vars.maxGustDirection}° at ${parseFloat(vars.maxGust).toFixed(1)} ${vars.uom}`"
marker:
autocolorscale: false
line:
width: 0.75
color: black
pattern:
fillmode: replace
shape: "."
bgcolor: lightgrey
fgcolor: black |
Beta Was this translation helpful? Give feedback.
-
I just discovered that since HA does not write data to the DB, except when data change that arrays of direction and wind speed entities will unlikely align on indexes. I'll need to figure out how to refactor the code to use time |
Beta Was this translation helpful? Give feedback.
-
I was using HA returns strings. I hadn't cast the array to Now the code also finds the most recent maximum value, not the first. Maybe the wind direction has changed from first to last. Code in first post is updated. |
Beta Was this translation helpful? Give feedback.
-
UPDATED: 02 May 2024
UPDATED 28 April 2024
UPDATED(2) 16 April 2024
Note the image below has a
rotation: 35
. The code below shows the default of90
to make it more generic.UPDATED 16 April 2024
UPDATED 24 March 2024.
Thanks dbuezas for the approach to use a generic function to return the "r" wind speed percentages. I applied that approach to the directional "theta" data as well.
Here we go:
...
I found a Plotly JS example to create a Wind Rose Chart based on the barpolar chart, I've created my own in HA. The challenge was to generate the arrays of wind speed and wind direction data.
The plotly
barpolar
chart requires arrays of wind speed percentages ("r") for various user-defined wind speed ranges, and the associated compass directions ("theta") for those wind-speed ranges.hours_to_show:
defines the lookback window for the chart. Eitherwind speed
, orwind gust
sensors can be used for the windspeed data.This implementation will (optionally) create a "Ball" at the center representing the percentage of the time that zero wind happened, since zero windspeed has no source direction.
For HA, we provide two sensors, one for
wind speeds
, and another forwind directions
(in degrees from North).The 'r' and 'theta' arrays come from a javascript function (see below) that processes wind speed and direction time series and returns the wind speed percentage array by cardinal direction. Directions are a static array.
[SOLVED] It would be VERY nice not to have to repeat the javascript function for each entity, and have the function callable instead from each entity.
Here is the UPDATED configuration.
Beta Was this translation helpful? Give feedback.
All reactions