Templating a GET URL with now().year

Hi there.

I can easily read out the kWh reading out of the rest resource for the year 2019.
But for next year it should change automatically to 2020.
I tried out {{ now().year }} instead of the 2019 in the resource URL. But it didn’t work.
Is there a way to put together an automated URL?

This is my code for now:

Thanks to your help.

resource doesn’t allow templating. You’ll have to update that by hand every year.

Thank you @petro.
Do you have maybe an other idea how i could do it?

You’d probably have to make a custom component for it. There may be other ways that i’m unfamiliar with.

You can try using a Command Line Sensor with a curl command instead. The Command Line Sensor supports templating in the command configuration variable.

ok that’s a good idea. Do you have a sample how to make a curl command line sensor?

How about:

  - platform: command_line
    name: Get Date
    command: curl -s http://date.jsontest.com
    value_template: "{{ value_json.date }}"

If you need to specify username/password, control headers, etc., then you should read up on how to use curl. It’s a very flexible command with lots of options. It might seem a bit overwhelming at first, but it’s a learning curve worth climbing.

One way I’ve used it is to put most of the “static” options in a text file, and then use curl’s -K option to point to it. E.g., the file might contain entries like the following in your scenario:

user: username:password
header = "Content-Type: application/json"
header = "User-Agent: Home Assistant REST sensor"

Then the command might be:

    command: curl -s -K options.txt https://smart-me.com:443/api/MeterValues/XXXXXXX?date={{ now().strftime('%Y-%m-%d') }}T12%3A00%3A01Z 1
1 Like

Thank you. I will try it out.

Hi pnbruckner

Thanks to your comment and some research how to “curl” i’m now able to read out my values.
It’s quite powerful. Now i can do much more things :wink:

And off course i share my code:

  - platform: command_line
    name: Einspeisung Buholzer Anfang Jahr
    command:  curl -s  --user XXXXXXXX:XXXXXXXX https://smart-me.com:443/api/MeterValues/XXXXXXXXXXXXXXX?date={{ now().strftime('%Y') }}-1-1T12%3A00%3A01Z
    value_template: "{{ value_json.CounterReadingExport }}"
    unit_of_measurement: "kWh"

Do you have also an idea how i could do a date picker with a curl command on home-assistant?

I’m not exactly sure what you’re asking here. Do you mean you want to have an element in the frontend where the user can select a date, and then use that in the curl command in the command_line sensor? And by date do you mean YYYY-MM-DD, or just YYYY? Or do you mean something else?

This is my date picker for the start period.

    name: Start Rechnungsperiode
    has_date: true
    has_time: false

This is more for information to realise for myself how it works. Actually not needed.

- platform: template
      friendly_name: "Year Start"
      value_template: '{{ states.input_datetime.bill_start.attributes.year }}'
- platform: template
       friendly_name: "Month Start"
       value_template: '{{ states.input_datetime.bill_start.attributes.month }}'
- platform: template
       friendly_name: "Day Start"
       value_template: '{{ states.input_datetime.bill_start.attributes.day }}'

I used the command_line platform to put all date attributes together according the date picker.

  - platform: command_line
    name: Metering Start Tarif 1
    command:  curl -s  --user xxxxxxx:xxxxxxxx https://smart-me.com:443/api/MeterValues/xxxxxxxxxxxxxxxx?date={{ states.input_datetime.bill_start.attributes.year }}-{{ states.input_datetime.bill_start.attributes.month }}-{{ states.input_datetime.bill_start.attributes.day }}T00%3A00%3A00
    value_template: "{{ value_json.CounterReadingImportT1 }}"
    unit_of_measurement: "kWh"

Hope this information can help also someone else.