RomRider / apexcharts-card

📈 A Lovelace card to display advanced graphs and charts based on ApexChartsJS for Home Assistant
MIT License
1.2k stars 82 forks source link

Access to the HA states object in an EVAL function #615

Open parautenbach opened 1 year ago

parautenbach commented 1 year ago

Checklist

Is your feature request related to a problem? Please describe. I have a highly dynamic chart. My original idea was to allow and EVAL function to set the min and max of a radialBar chart, but I've since figured I don't need that, so instead I'd like to access another entity's value from within a formatter's EVAL: function block.

My example: I have a radialBar chart that shows the percentage of fair weather for the current day. My HA template sensor doesn't take clear skies at night into account, since I'm only interested in daytime fair weather. The consequence is that the percentage of fair weather time per day is then a function of the daylength, which is dynamic. In other words, if the daylength today is 13h15 and the fair weather was 10h12, then it's 76.98%, but tomorrow the daylength will be different. I'd like to use an EVAL function then to access my daylength sensor in order to set the max field for the radialBar chart.

Describe the solution you'd like A states object with access to all entities. Many custom cards, e.g., the famous custom button card and card-mod allow this.

Describe alternatives you've considered I've tried a number of hacks, but nothing that could achieve this.

Additional context Nothing I can think of.

github-actions[bot] commented 10 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 10 days.

parautenbach commented 10 months ago

.

will2022 commented 9 months ago

I think I might have a similar feature request. It would be nice if I could show or hide the entity in the graph depending on the value of an entity state. I'm unsure if there's a way to achieve this.


series:
  - entity: sensor.some_sensor
    show:
      in_chart: '{{state_attr("sensor.some_sensor", "boolean_state")}}'```
github-actions[bot] commented 7 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 10 days.

parautenbach commented 7 months ago

It would still be nice to get more dynamic bahaviour. :-)

github-actions[bot] commented 5 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 10 days.

parautenbach commented 5 months ago

.

jorritjaap commented 5 months ago

@parautenbach As an alternative I am using the show object to hide the senor from the chart, but use it in the fucntion.

type: custom:apexcharts-card
series:
  - entity: sensor.solar_house_consumption_w
    show:
      in_chart: false

Maybe that can work for you use case too?

parautenbach commented 5 months ago

Thanks, but that's not quite what's needed here.

Perhaps a picture would help:

Screenshot 2024-06-04 at 10 22 51

Here I'm displaying both a percentage of the day that's fair weather as well as the duration. Day length obviously varies over a year, so the problem is that what's 100% changes.

I managed to get this working at the time, but completely forgot to circle back here.

This post on the community forum helped.

This is my current solution, which is fine, but can definitely be more readable and digest with direct access to the states object:

              - type: custom:apexcharts-card
                series:
                  - entity: sensor.fair_weather_time
                    transform: "return x / parseFloat(hass.states['sensor.home_sun_daylight'].state) * 100;"
                    color: var(--custom-color-yellow)
                    min: 0
                    max: 100
                chart_type: radialBar
                apex_config:
                  title:
                    text: Fair Weather
                  legend:
                    show: false
                  plotOptions:
                    radialBar:
                      startAngle: -170
                      endAngle: 170
                      dataLabels:
                        name:
                          show: false
                        value:
                          show: true
                          fontSize: 10px
                          offsetY: -4
                          # https://community.home-assistant.io/t/apexcharts-card-a-highly-customizable-graph-card/272877/2608
                          # alternative chart: https://github.com/AmoebeLabs/flex-horseshoe-card
                          formatter: |
                            EVAL:function (percentage) {
                              // would've loved to make this snippet more readible, but getElementsByTagName returns some kind of live object
                              // which means you need to resolve the whole thing; can't make no hass object first.
                              const daylight = parseFloat(document.getElementsByTagName('home-assistant')[0].hass.states['sensor.home_sun_daylight'].state);
                              const value = percentage / 100 * parseFloat(daylight);
                              const hour = Math.floor(value);
                              const minute = Math.round((value - hour) * 60);
                              return [`${hour}h${minute.toString().padStart(2, '0')}m`, `(${percentage.toFixed(0)}%)`];
github-actions[bot] commented 3 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 10 days.

parautenbach commented 3 months ago

Please. :-)

github-actions[bot] commented 1 month ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 10 days.

parautenbach commented 1 month ago

.