Number of days of current year (e.g. 300)

Hi All,

For a template I need to have the days of the current year of current date (So for today it would be around 300).

Can someone point me in the right direction?

{{ now().strftime(’%j’) }} gives the days, but it won’t work in my template:

‘{{ (((states.sensor.gas_jaar.state | float) * (0.638045)) + (now().strftime(’%j’)*(0.636316))) | max (0) | round(2) }}’

I now use this: ((now().month-1) * 30 + now().day) to have an estimate

But is there a better way?

Add this to your sensors… and use the sensor in your template.

        friendly_name: 'Day Number'
        value_template: "{{ now().strftime('%j') }}"


Thank you for answering. Is this as a workaround? So it can’t be done directly?

try this:

{{ (((states.sensor.gas_jaar.state | float) * (0.638045)) + (now().strftime('%j')|float *(0.636316))) | max (0) | round(2) }}

Wow! That worked, thank you so much!

1 Like

Sorry, it didn’t work. After checking config it says:
Error loading /config/configuration.yaml: while scanning for the next token
found character ‘%’ that cannot start any token
in “/config/configuration.yaml”, line 425, column 103

You are trying to implement this to your configuration… The template tester works differently… Just paste us to see the part you want to make to work from your .yaml file.

  • platform: template
    value_template: ‘{{ (((states.sensor.gas_jaar.state | float) * (0.638045)) + (((now().month-1) * 30 + now().day)*(0.636316))) | max (0) | round(2) }}’
    friendly_name: ‘Prijs gas per jaar’
    unit_of_measurement: ‘euro’


Here it is, with my “solution”

You need to format it properly so I can read this… use the instructions given on the top of the forum page…
here is an example:

  - platform: nmap_tracker
    interval_seconds: 120
    track_new_devices: true
    home_interval: 1

This is how it should look like.

Nevermind, I think I can see the problem

value_template: '{{ (((states.sensor.gas_jaar.state | float) * (0.638045)) + (now().strftime("%j")|float *(0.636316))) | max (0) | round(2) }}'

Notice the quotes!
The template should start and end with a single quote. If you have a single quote inside the template, it will break it. So you have to replace it using double quotes.

  - platform: template
           value_template: '{{ (((states.sensor.power_jaar.state | float) * (0.198416)) - (((now().month-1) * 30 + 
                   now().day)*(0.2811))) | max (0) | round(2) }}'
           friendly_name: 'Prijs stroom per jaar'
           unit_of_measurement: 'euro'  

The “value_template” is cut off in this post, but will be on one line.

And excuses for bad post

  - platform: template
           value_template: '{{ (((states.sensor.gas_jaar.state | float) * (0.638045)) + (now().strftime("%j")|float *(0.636316))) | max (0) | round(2) }}'
           friendly_name: 'Prijs stroom per jaar'
           unit_of_measurement: 'euro'  

Yup… I think this was your problem. Try this one. :smiley:

1 Like

And now it works. Thank you for taking the time to explain it to me.

Have a very nice weekend!

1 Like