lozzd / octopus-energy-rates-card

This lovelace card for Home Assistant displays the Octopus Energy rate prices per each 30 minute slot
MIT License
65 stars 22 forks source link

Possibility to support variables? #9

Closed coleburg closed 8 months ago

coleburg commented 1 year ago

Would it be possible to support variables? ie

entity: sensor.octopus_energy_electricity_num_num_current_rate type: custom:octopus-energy-rates-card cols: 2 showday: true showpast: false mediumlimit: {{states('sensor.agile_avg_rate')}}

Would be a great addition.

thank you

coleburg commented 1 year ago

any thoughts @lozzd ? thank you

andynash commented 1 year ago

Yep this would be great, I'd like to use variables for all 3 limits.

lozzd commented 1 year ago

Do you have an example card that uses this? I'm not sure what's needed from me to make that work, so need to check their code...

andynash commented 1 year ago

I guess the Mushroom Template card might be a good example?

That card supports templates, so some example Jinja for the "Primary" field for that card looks like this:

primary: >- Heating {% if is_state('input_boolean.heating','on') %} Enabled {% else %} Disabled {% endif %}

github-dpmracing commented 1 year ago

Did anyone have any thoughts on this? I played around with various different formats to achieve setting the limits via variables but none seemed to work. I’m not sure what Lovelace is supposed to support within a custom card or whether there was some odd syntax

E.g. entity: sensor.octopus_energy_electricity_xxx_Xxx_current_rate type: custom:octopus-energy-rates-card cols: 2 showday: true showpast: false hour12: false highlimit: 30 mediumlimit: {{ states('input_number.energyprice_medium') | multiply(100) }}

stevetrease commented 11 months ago

I’d be very interested in this feature too.

coleburg commented 11 months ago

Do you have an example card that uses this? I'm not sure what's needed from me to make that work, so need to check their code...

I just used the example in the tutorial. which is something like this

entity: sensor.octopus_energy_electricity_19m1stuffhere_current_rate type: custom:octopus-energy-rates-card cols: 2 showday: true showpast: false mediumlimit: 20

I would just like to be able to replace the mediumlimit: 20 with something like {{states('sensor.agile_avg_rate')}}. the agile_avg_rate being calculated daily from the Octopus API.

lozzd commented 10 months ago

I've been googling around and I think the following should work:

mediumlimit: [[[ states('sensor.agile_avg_rate') ]]]

I just tested and it works. This is standard feature of Lovelace btw, no changes needed for the card :)

github-dpmracing commented 10 months ago

Thanks @lozzd for looking. I've been playing with this this morning and although it seems to do "something" it doesn't seem to be working for me. @stevetrease I note you've thumbs upped this, is it working for you or was that a "thankyou for the info" type thumbs up?

In my instance I have a helper variable set to a value of 10 and then attempting to use it in the lovelace config as:-

mediumlimit: [[[ states('input_number.testvariable') ]]]

Although this seemed to change colours it didn't provide the same as putting

mediumlimit: 10

I then tried

mediumlimit: rubbish

and it provided the same colours as my first attempt.

If I shove "{{ states('input_number.testvariable') }}" into the template test evaluation it's correctly evaluating as 10 and a number

I'm guessing the [[[ states('sensor.agile_avg_rate') ]]] is evaluating as something the card doesn't understand or out of the context so the card is defaulting to something (which isn't the same default if you remove the mediumlimit entirely).

Maybe it's something mad I'm doing, hence why I'm wondering whether anyone else has got it working?

For completeness, my full card config

currentEntity: event.octopus_energy_electricity_XXXXXXXX_current_day_rates
pastEntity: event.octopus_energy_electricity_XXXXXXXXXX_previous_day_rates
futureEntity: event.octopus_energy_electricity_XXXXXXXXXX_next_day_rates
type: custom:octopus-energy-rates-card
cols: 2
showday: true
showpast: false
highlimit: 28
mediumlimit: [[[ states('input_number.testvariable') ]]]
hour12: false
stevetrease commented 10 months ago

@.stevetrease I note you've thumbs upped this, is it working for you or was that a "thankyou for the info" type thumbs up?

Sorry, no, I had not tried it. But will do now.

stevetrease commented 10 months ago

No, I do not think it is working properly.

I've setup three input number helps: low limit, medium limit and highlimit and two cards - one with the three limits fixed in the card config and one where all three limits are set to [[[ states('input_number.XXXlimit') ]]]

The one with the limits set via [[[ states () ]]] does not update in the same way.

The dashboard code I am using is:

Screenshot 2023-11-28 at 12 13 55

The results are:

Screenshot 2023-11-28 at 12 10 23

As you can see the cell colouring does not match the three limits set at the top.

Interestingly, the code entered as [[[ states() ]]] in the YAML editor renders as - - - states () when edited.

github-dpmracing commented 10 months ago

Thanks @stevetrease , more head scratching to be done then by the looks of things. I've been playing around with defining "variables" in the card and then using var[0] as well but that doesn't seem to work.

I'll keep poking around as I'd really love to get this working fully. I do really like this card @lozzd thanks for your efforts, especially with the enforced changes from BottleTopDave

p.s. I also noted the rewriting of the yaml

stevetrease commented 10 months ago

I’ve submitted a pull request (https://github.com/lozzd/octopus-energy-rates-card/pull/40) to allow an entity to be used to emphasis cells below a “dynamic value”. Essentially cells below the value of the specified sensor are highlighted with bold, black text. You might want to try that.

corvus2606 commented 8 months ago

As this doesn't seem to be getting much attention lately, here's my solution.

I have an aggregate sensor tracking the average price over the last rolling 24 hours called sensor.electric_average_unit_rate

I installed the config template card addon and configured as below:

type: custom:config-template-card
view_layout:
  grid-area: header3
entities:
  - sensor.electric_average_unit_rate
card:
  type: custom:octopus-energy-rates-card
  pastEntity: event.octopus_energy_electricity_**********_*************_previous_day_rates
  currentEntity: event.octopus_energy_electricity_**********_*************_current_day_rates
  futureEntity: event.octopus_energy_electricity_**********_*************_next_day_rates
  targetTimesEntity: binary_sensor.octopus_energy_target_house_intermittent_night
  cols: 5
  showpast: true
  showunits: true
  showday: true
  hour12: false
  combinerate: true
  cheapest: true
  mediumlimit: ${ states['sensor.electric_average_unit_rate'].state * 0.8 }
  highlimit: ${ states['sensor.electric_average_unit_rate'].state * 1.1}
corvus2606 commented 8 months ago

My latest pull request #61 allows an entity to be specified with multipliers for medium and high limits which natively gives the same functionality as my earlier comment

limitEntity: sensor.electric_average_unit_rate
mediumLimitMultiplier: 0.8
highLimitMultiplier: 1.1
lozzd commented 8 months ago

This is merged and live in v0.5.0 - I will close this for now but please reopen if this doesn't work for folks usecases