Adjust the value from slider if set to "0"

I have slider for an automation that controls relay based on electricity spot prices.

My problem is that the way the code is written my min and max values are 1-24h. I wish to add “0” as an option but it brings me some problems if just added to the slider.

How can I edit my config.yaml file in a way that:
-if input slider value is “0” set it to 25.

I have done some experiments that if i get a value higher than 24 the template will give me an undefinied response which would be good for the “0” hour selection due to the fact that it wont activate the automation.

This is the code in yaml file.


>   - platform: template
>     sensors:
>       kytkentaraja_24h:
>         unit_of_measurement: c/kWh
>         value_template: '{{((state_attr(''sensor.nordpool_kwh_fi_eur_3_09_024'',''raw_today'')|sort(attribute=''value''))[int(states(''input_number.slide_24h''))-1].value)|round(3)-0.001}}'

Preformatted text`To add a “0” value option to your automation input slider and set it to 25 if selected, you can use a template sensor and a switch statement in your configuration.yaml file.

Here is an example YAML configuration that adds a “0” value option to the input slider and sets it to 25 if selected:



# Configure the template sensor for the input slider
sensor:
  - platform: template
    sensors:
      input_slider:
        value_template: "{{ states.input_slider.state | int }}"
        friendly_name: "Input Slider"

# Configure the switch statement for the input slider
switch:
  - platform: template
    switches:
      input_slider:
        value_template: "{{ is_state('sensor.input_slider', 25) }}"
        turn_on:
          - service: some_service.some_action
            data:
              some_parameter: 25

In this configuration, the template sensor is used to convert the input slider value to an integer and make it available as a sensor state in Home Assistant. The switch statement is then used to check if the input slider value is equal to 25, and if it is, it will trigger the some_service.some_action service with the data {‘some_parameter’: 25}.

You can customize this configuration by changing the value_template and friendly_name of the template sensor, the value_template, turn_on, and data of the switch statement, and the service and data of the service call. This will allow you to use the input slider value in your automation and set it to 25 if the “0” value option is selected.

Sounds like the real problem is here. Show is what you have. What is that code you’re referring to?

What’s behind this slider? Input slider is an extremely old and deprecated HA integration. If it’s your own thing, what is it? You should be using an input number and set the range 1-24, from what I can see.

And does your data actually have a value for the key 25?

I’m using this https://github.com/custom-components/nordpool integration to pull Nordpool daily prices per hour.

The “kytkentaraja_24h” template sensor is used to sort the hours based on price and input_number.slide24h will pull the value (c/kWh) of chosen hour (-1 and degrade 0,001€ from it) = “x”

This hourly price “x” will be used as a value for automation (if current price is lower than “x” then switch relay on else off)

As far as I understand my data do not have data for key 25. According to my testing, if I try to pull hour 25 from the data the “template sensor called kytkentaraja_24h” does provide me with value “undefined” and it will cause the automation to not meet requirements → meaning that relay will be switched off based on “else rule”.

This is a code I copied from someone else, but I’m trying to make it better for my application.

Can you capture a sample response? I’m not familiar with the integration, but can perhaps help if I see the data.

Re:

You also said:

Why do you care if there’s a value of 25 if it seems like it can’t happen?

And I don’t understand why you want to do this:

Is this an option you want to set explicitly as some kind of default?

What does this automation look like?

I’m on the road atm so wont be able to provide you with everything att.

I can pull 24 different values from nordpool for day, each one of them representing an hourly cost for kWh. If I wish to have my relay on for 4 cheapest hours of day I could pull 4th lowest value from the sorted list of hourly prices and make automation using this price as an price limit.

So that 4th lowest price +0,001€ is the limit and if current price is less then relay ON. (or same results pulling 5th lowest -0,001€)

Using the slider I can choose how many hours i need the relay to be ON per day. If for some reason i want it to be ON all the time then I would choose value 24 (the 24th lowest hour price added with 0,001€) → resulting that every hour meets the criteria and relay is ON for hole day.

What I cant do is to not have it ON at all for the day because if I set the slider to value 0 the value received is for some reason the 24th again. I do not know why.

Meaning that with value 0 it will give same result as with value 24. But if I set slider value to 25 then my result is undefined and end result is that relay will be OFF. That is the only reason I wish to be able to convert value 0 to 25.

Lets face it. I thought that it would be quite simple but it doesnt seem to be that straight forward.

I might have found a different approach for this issue by using this code:

template:
– sensor:
– name: “NordpoolRank”
state: >
{{ (state_attr(‘sensor.nordpool_kwh_fi_eur_3_10_024’, ‘today’) | sort).index(state_attr(‘sensor.nordpool_kwh_fi_eur_3_10_024’, ‘current_price’))+1}}

Creating an automation based on Ranks. Eg, Turn on this when Rank is below X.

But I’m afraid that this will also bring me some problems since I can have an automation based on slider value 1-24h “turn on if rank is below 1” is not intuitive since value 1 would actually mean “0”-hours ON and turn on if rank is below 24 results to 23hours.

So side question. Any idea how to set rule “if below or same”…

I believe that I will solve this issue with and additional toggle switch that will control the automation. Setting that toggle off I will gain same end result as by sliding to zero.