Template sensor help read value from input_number

i am trying to make some of the attributes in this template editable from the frontend, but i cant get the syntax correct.

its the states.input_number.periode_1_antal_timer.state. that troubles me.

template:
  -  sensor:
      # The actual cheapest hour sensor. If multiple entries are required, copy and set unique_id and configure attributes.
      - name: "Billig_el_periode_1"
        unique_id: billig_el_periode_1
        device_class: timestamp
        state: >
          {%- set sensor = (this.attributes.get('sensor', 'sensor.nordpool') | string) -%}
          {%- set numberOfSequentialHours = (this.attributes.get('number_of_sequential_hours',1) | int) -%}
          {%- set lastHour = (this.attributes.get('last_hour',23) | int) -%}
          {%- set firstHour = (this.attributes.get('first_hour', 0) | int) -%}
          {%- set startingToday = (this.attributes.get('starting_today', false) | bool) -%}
          {%- if state_attr(sensor, 'tomorrow_valid') == true -%}
            {%- set arr = state_attr(sensor, 'today') + state_attr(sensor, 'tomorrow') -%}
            {%- set ns = namespace(counter=0, list=[], cheapestHour=today_at("00:00"), cheapestPrice=999.00) -%}
            {%- if startingToday == true -%}
              {%- set ns.starting = firstHour -%}
            {%- else -%}
              {%- set ns.starting = firstHour + 24 -%}
            {%- endif -%}
              {%- set ns.ending = lastHour + 24 -%}
            {%- for i in range(ns.starting + numberOfSequentialHours, ns.ending+1) -%}
              {%- set ns.counter = 0.0 -%}
              {%- for j in range(i-numberOfSequentialHours, i) -%}
                {%- set ns.counter = ns.counter + arr[j] -%}
              {%- endfor -%}
              {%- set ns.list = ns.list + [ns.counter] -%}
              {%- if ns.counter < ns.cheapestPrice -%}
                {%- set ns.cheapestPrice = ns.counter -%}
                {%- set ns.cheapestHour = today_at("00:00") + timedelta( hours = (i - numberOfSequentialHours)) -%}
              {%- endif -%}
            {%- endfor -%}
            {{ ns.cheapestHour }}
            {%- set ns.cheapestPrice = ns.cheapestPrice / numberOfSequentialHours -%}
          {%- endif -%}
        attributes:
          # CHANGE-ME: Set your personal configurations in here {{ states('input_number.periode_1_antal_timer')| float }} (states('input_number.periode_1_antal_timer'))
          number_of_sequential_hours: states.input_number.periode_1_antal_timer.state  # Amount of sequantial cheapest hours in search
          first_hour: 00 # Search starting hour
          last_hour: 23 # Search ending hour
          starting_today: false # Is the first_hour today (true / false). If false, first_hour needs to be before last_hour.
          sensor: sensor.nordpool # Nord pool sensor id. Check it ouf from your integrations page!
          fail_safe_starting: '00:00' # If nordpool fetch fails, starting time to make the calendar entry
´´´

these are the ones i have made input_numbers for. but i just cant get it to compute…

number_of_sequential_hours: >
  {{ states('input_number.periode_1_antal_timer') }}

Keep in mind that this most likely will not work on your main sensor because it will always resolve after the main state is resolved.

The only proper way to handle this IMO…

template:
  - trigger:
    - platform: state
      entity:
      - sensor.nordpool
      - input_number.periode_1_antal_timer
      - input_number.periode_1_antal_first_hour
      - input_number.periode_1_antal_last_hour
      variables: 
        number_of_sequential_hours: >
          {{ states('input_number.periode_1_antal_timer') }}
        first_hour: >
          {{ states('input_number.periode_1_antal_first_hour') }} 
        last_hour: >
          {{ states('input_number.periode_1_antal_last_hour') }}
        starting_today: false 
        sensor: sensor.nordpool
  -  sensor:
      - name: "Billig_el_periode_1"
        unique_id: billig_el_periode_1
        device_class: timestamp
        state: >
            {%- if state_attr(sensor, 'tomorrow_valid') == true -%}
            {%- set arr = state_attr(sensor, 'today') + state_attr(sensor, 'tomorrow') -%}
            {%- set ns = namespace(counter=0, list=[], cheapestHour=today_at("00:00"), cheapestPrice=999.00) -%}
            {%- if starting_today -%}
              {%- set ns.starting = first_hour -%}
            {%- else -%}
              {%- set ns.starting = first_hour + 24 -%}
            {%- endif -%}
              {%- set ns.ending = last_hour + 24 -%}
            {%- for i in range(ns.starting + number_of_sequential_hours, ns.ending+1) -%}
              {%- set ns.counter = 0.0 -%}
              {%- for j in range(i-number_of_sequential_hours, i) -%}
                {%- set ns.counter = ns.counter + arr[j] -%}
              {%- endfor -%}
              {%- set ns.list = ns.list + [ns.counter] -%}
              {%- if ns.counter < ns.cheapestPrice -%}
                {%- set ns.cheapestPrice = ns.counter -%}
                {%- set ns.cheapestHour = today_at("00:00") + timedelta( hours = (i - number_of_sequential_hours)) -%}
              {%- endif -%}
            {%- endfor -%}
            {{ ns.cheapestHour }}
            {%- set ns.cheapestPrice = ns.cheapestPrice / number_of_sequential_hours -%}
          {%- endif -%}

I didn’t go through the code. TBH, it seems alittle verbose. I recall doing something similar to this and it wasn’t this complicated.

thanks. i did not write the code. maybe thats the problem.

thanks i will look into your solution.

cant get it working. damn!

  - trigger:
    - platform: state
      entity_id:
      - sensor.nordpool
      - input_number.periode_1_antal_timer
      - input_number.periode_1_start_tid
      - input_number.periode_1_slut_tid    
      variables: 
        number_of_sequential_hours: >
          {{ states('input_number.periode_1_antal_timer') }}
        first_hour: >
          {{ states('input_number.periode_1_start_tid') }} 
        last_hour: >
          {{ states('input_number.periode_1_slut_tid') }}
        starting_today: false 
        sensor: sensor.nordpool
  -  sensor:
      - name: "Billig_el_periode_1"
        unique_id: billig_el_periode_1
        device_class: timestamp
        state: >
          {%- if state_attr(sensor, 'tomorrow_valid') == true -%}
          {%- set arr = state_attr(sensor, 'today') + state_attr(sensor, 'tomorrow') -%}
          {%- set ns = namespace(counter=0, list=[], cheapestHour=today_at("00:00"), cheapestPrice=999.00) -%}
          {%- if starting_today -%}
            {%- set ns.starting = first_hour -%}
            {%- else -%}
              {%- set ns.starting = first_hour + 24 -%}
            {%- endif -%}
              {%- set ns.ending = last_hour + 24 -%}
            {%- for i in range(ns.starting + number_of_sequential_hours, ns.ending+1) -%}
              {%- set ns.counter = 0.0 -%}
              {%- for j in range(i-number_of_sequential_hours, i) -%}
                {%- set ns.counter = ns.counter + arr[j] -%}
              {%- endfor -%}
              {%- set ns.list = ns.list + [ns.counter] -%}
              {%- if ns.counter < ns.cheapestPrice -%}
                {%- set ns.cheapestPrice = ns.counter -%}
                {%- set ns.cheapestHour = today_at("00:00") + timedelta( hours = (i - number_of_sequential_hours)) -%}
              {%- endif -%}
            {%- endfor -%}
            {{ ns.cheapestHour }}
            {%- set ns.cheapestPrice = ns.cheapestPrice / number_of_sequential_hours -%}
          {%- endif -%}

keep getting this.

Stopped because an error was encountered at 6. juni 2023 kl. 21.54.49 (runtime: 0.02 seconds)

Error rendering data template: ValueError: Template error: float got invalid input ‘None’ when rendering template ‘{%- set sensorId = ‘sensor.billig_el_periode_1’ -%} {{ (as_timestamp(states(sensorId)) + (3600 * state_attr(sensorId, ‘number_of_sequential_hours’) | float)) | timestamp_local }}’ but no default was specified

the first part uses variables: and the second uses atrributes. hmm. is that campateble?

None of that code is in what I pasted for you to use.

I know but the sensor.billig_el_periode_1 is not working and the error is from the automation that processes the sensor. there is no info in the sensor …

If you’re only getting an error from the automation, then sensor.billig_el_periode_1 is working. That error is 100% unrelated to the sensor. It’s a code error in your automation.

i use the same automation for a sensor with hardcodes values. it works. and in developer / states i can see the sensor is empty. i have it running on 2 other servers and works there too. i just need to make it easier to change for people without coding experience. (as myself apparently)…

i get this error from the sensor code:

Template variable error: ‘sensor’ is undefined when rendering ‘{%- if state_attr(sensor, ‘tomorrow_valid’) == true -%} {%- set arr = state_attr(sensor, ‘today’) + state_attr(sensor, ‘tomorrow’) -%} {%- set ns = namespace(counter=0, list=[], cheapestHour=today_at(“00:00”), cheapestPrice=999.00) -%} {%- if starting_today -%} {%- set ns.starting = first_hour -%} {%- else -%} {%- set ns.starting = first_hour + 24 -%} {%- endif -%} {%- set ns.ending = last_hour + 24 -%} {%- for i in range(ns.starting + number_of_sequential_hours, ns.ending+1) -%} {%- set ns.counter = 0.0 -%} {%- for j in range(i-number_of_sequential_hours, i) -%} {%- set ns.counter = ns.counter + arr[j] -%} {%- endfor -%} {%- set ns.list = ns.list + [ns.counter] -%} {%- if ns.counter < ns.cheapestPrice -%} {%- set ns.cheapestPrice = ns.counter -%} {%- set ns.cheapestHour = today_at(“00:00”) + timedelta( hours = (i - number_of_sequential_hours)) -%} {%- endif -%} {%- endfor -%} {{ ns.cheapestHour }} {%- set ns.cheapestPrice = ns.cheapestPrice / number_of_sequential_hours -%} {%- endif -%}’

maybe this needs something? sensor: sensor.nordpool?

Remove this dash completely. Make sure the word sensor has the same number of spaces as the trigger: line above. i.e. same indentation level. Treat - as a “space” when counting in front of trigger.

that fixed the error in the log . state is unavalible. do i need to wait for a change in the variables?

it shouldn’t be unavailable. Only unkonwn if it’s waiting for a state. Post the configuration you’re using

it is unknown.

nordpool is fine. i have values in the sensor.nordpool

i am having really bad yaml karma these days

thanks for your time and help. i think it will work now. will have to wait for tomorrow to see…

-andy

Then when you change one of the input_numbers or when sensor.nordpool updates, the template sensor will provide a valid value assuming that whoever wrote that code built it well.

FYI, people do not see your responses when you reply to the thread.

You’ll get better support.

1 Like