Hi all,
Here’s yet another experiment with a custom list widget to render the wind direction by name instead of by angle, and with support for a dynamic icon.
This widget requires no openHAB rules to run, the logic is contained in the widget code.
I documented the transform logic in YAML comments, but unfortunately openHAB removes these comments when you save the widget. I posted the overall logic elsewhere in a Python rule:
Convert wind direction in degrees (0°-360°) into a named wind direction by means of an index.
Since we’re producing a 32-point wind rose, ‘pure’ wind direction names are centred across arcs spanning 1/32th of a full circle. Or, having equal arcs of 1/64th of a full circle at each side as mapping intervals. In this logic:
- north is centred at step 0
- scaled wind angles with values between steps -1 and +1 are mapped to north
- since wind directions span 2 steps, we must count the intervals at odd nonzero step values
Therefore the wind direction name index can be computed as follows:
- Convert a wind direction angle value into 2 * 32 = 64 steps
- Round down this value to the closest integer
- Map this value to [0…64[ using modulo (north may yield values ≥64)
- Convert this value to a 32-step index (use Math.floor as wind name changes at threshold)
- Look up the wind direction name by means of this index in the wind direction name array
You can retrieve custom wind rose icons from:
https://community.openhab.org/t/solved-making-dynamic-icons-work-32-point-wind-rose-example/76326/7?u=shutterfreak
Have fun!
Olivier
Code
uid: wind_direction_list_32
tags:
- list
- wind
props:
parameters:
- description: Widget title
label: Title
name: title
required: false
type: TEXT
- context: item
description: Wind direction angle item to display
label: Item
name: item
required: true
type: TEXT
parameterGroups: []
timestamp: Jun 4, 2021, 8:45:38 PM
component: oh-list-item
config:
## Get the wind angle in degrees:
# wind_angle = Number.parseFloat(items[props.item].state
## Scale wind angle from 360° range to 64 step range:
# scaled_wind_angle = wind_angle / 360.0 * 2 * 32
## North goes from -1 to +1 hence we add an offset so the index starts at 0, 2, 4...
# scaled_wind_angle += 1
# Convert index to 32-step index (use Math.floor) and reduce range to [0; 32[:
# wind_name_index = Math.floor(scaled_wind_angle / 2) % 32
## Use custom windrose icons named 'windrose-<state>.svg' (<state> represents a wind direction in lower case), e.g. from:
## https://community.openhab.org/t/solved-making-dynamic-icons-work-32-point-wind-rose-example/76326/7?u=shutterfreak
icon: >-
=(!Number.isFinite(Number.parseFloat(items[props.item].state))) ? "f7:question_circle" : "oh:windrose-" +
([
"N", "NbE", "NNE", "NEbN", "NE", "NEbE", "ENE", "EbN",
"E", "EbS", "ESE", "SEbE", "SE", "SEbS", "SSE", "SbE",
"S", "SbW", "SSW", "SWbS", "SW", "SWbW", "WSW", "WbS",
"W", "WbN", "WNW", "NWbW", "NW", "NWbN", "NNW", "NbW",
])[ Math.floor( (1 + Number.parseFloat(items[props.item].state) / 360.0 * 2 * 32) / 2) % 32 ].toLowerCase() + ".svg"
title: =props.title
item: =props.item
action: analyzer
actionAnalyzerCoordSystem: time
actionAnalyzerItems: =[props.item]
after: >-
=(!Number.isFinite(Number.parseFloat(items[props.item].state))) ? "-" :
([
"N", "NbE", "NNE", "NEbN", "NE", "NEbE", "ENE", "EbN",
"E", "EbS", "ESE", "SEbE", "SE", "SEbS", "SSE", "SbE",
"S", "SbW", "SSW", "SWbS", "SW", "SWbW", "WSW", "WbS",
"W", "WbN", "WNW", "NWbW", "NW", "NWbN", "NNW", "NbW",
])[ Math.floor( (1 + Number.parseFloat(items[props.item].state) / 360.0 * 2 * 32) / 2) % 32 ]