Using a sensor state in YAML dashboard config

Hi,

Is it possible to pull a sensor state into a YAML dashboard config parameter?

Specific example here for Apex Charts, I have this set of temp graphs, currently with the y-axis auto fitting to the data.

Problem is, the axis are different. Would prefer to find the min/max for the set (current 67 and 70 respectively). And then set the min/max for all 6 charts to those values. Tested this with a manually set input number but no bueno.

   yaxis:
      - decimals: 0
        min: input_number.min_temp
        apex_config:
          tickAmount: 3

Seen some other posts referencing syntax like this, but also didn’t work.

{{states.sensor.sensor-name.state}}

Is this a limitation of yaml or apex charts? Or am I missing a way to pull that data in?

Many thanks

Seems a few similar attempts here and here relying on lovelace_gen, but I’m not following if anyone has managed to get this to work.

I’m really confused on what your hangup is? Just make all the charts have a min: 67 and a max: 70?

Or are you trying for something else? Maybe you want it dynamic? If yes…

lovelace_gen does not have access to the state machine, so it’s not possible with lovelace gen.

If you want dynamic min/max, you’d have to use a card that allows templating in fields. Like config-template-card or something that uses Jinja.

Worse case scenario, you can make a min sensor and a max sensor. Then add those to all 6 graphs while changing the opacity of the line to 0.0%.

yes, that is functional with apexcharts:

type: custom:config-template-card
entities:
  - input_select.apex_span
variables:
  span: states['input_select.apex_span'].state
card:

  type: custom:apexcharts-card
  card_mod:
    style: |
      ha-card {
        box-shadow: none;
        margin: -8px -16px -16px -16px;
        padding: 0;
      }
  #header:
  #  title: Verbruik actueel
  update_interval: 1min
  graph_span: ${span}
  series:

be aware though that it can be very heavy on the dashboard/system, as those variables will be evaluated on each and every state change. In the above example, I figured t was safe to do so because the span is set only upon request every now and then.

However, if it is dynamic temp sensor, I don’t believe I would do so, especially not in the amount you are proposing there.

might be no harm trying though…

Petros suggestion would be your best bet: just set it identical for all, with a safe margin

1 Like

Well yea, dynamically setting this is the point. Temps will vary 10-15 degrees F in a given day/week, so statically setting a range that wide to accommodate misses smaller short term trends.

Custom:Config is new to be and seems like the right idea. I am getting the sensor value to pull into template ok, but something (Apex?) is still choking on it.

type: custom:config-template-card
variables:
  MIN_TEMP: states['input_number.min_temp'].state
entities:
  - input_number.min_temp
card:
  type: custom:vertical-stack-in-card
  title: House Temperatures
  cards:
    - square: false
      type: grid
      cards:
        - type: custom:apexcharts-card
          series:
            - entity: sensor.bedroom_current_temperature
          yaxis:
            - min: ${MIN_TEMP}

checking GitHub - RomRider/apexcharts-card: 📈 A Lovelace card to display advanced graphs and charts based on ApexChartsJS for Home Assistant would indicate a number only is required, so 60 should be good.

could it be it needs it to be an actual number, and not a string (as the output of the template in fact would be?)
not sure if the JS notation here is strict or not, but you could give it a try to int it, see 7 ways to convert a String to Number in JavaScript - DEV Community

2 Likes

Miracle worker!

parseInt() w/ Custom:Config FTW!

type: custom:config-template-card
variables:
  MIN_TEMP: parseInt(states['input_number.min_temp'].state)
entities:
  - input_number.min_temp

Below passes a static input number (just for Our Room) as proof of concept. Should be able to create the proper template sensor from here.

Many many thanks for this tip.

Happy Days!
Thanks again @Mariusthvdb

2 Likes

That’s a nice quick view without the extra clutter.

2 Likes

Nice!

Never give up :wink:

1 Like