Template in restful sensor URI

Hi,

hope anyone may help me. Probably the solution is easy but Iam to stupid

I have a restful API that querries result based on a date e.g

http://192.168.5.195:8089/api/mealplancount/2020-07-01

I would like to have also values for today and tomorrow so I tried to use

resource_template: http://192.168.5.195:8089/api/mealplancount/{{ now().strftime('%Y-%m-%d') }}

That works great for today but Iam somehow to stupid to get tomorrow.

any help is highly appreciated :smile:

{{ (as_timestamp(now()) + 86400)|timestamp_custom('%Y-%m-%d') }}
1 Like

ty my hero :smiley:

Hi Tom,

it seems that works only for one day

I have the following sensors

  - platform: template
    sensors:
     hotelfruehstueckheute:
      friendly_name: 'Anzahl Fruehstücker Heute'
      value_template: '{{states.sensor.fruehstueckheute.attributes["anzahl_erwachsene"] + states.sensor.fruehstueckheute.attributes["anzahl_kind1"] }}'

  - platform: template
    sensors:
     hotelfruehstueckmorgen:
      friendly_name: 'Anzahl Fruehstücker Morgen'
      value_template: '{{states.sensor.fruehstueckmorgen.attributes["anzahl_erwachsene"] + states.sensor.fruehstueckmorgen.attributes["anzahl_kind1"] }}'

  - platform: template
    sensors:
     hotelfruehstueckplus2:
      #friendly_name: 'Anzahl Fruehstücker +2'
      friendly_name_template: "{{ (as_timestamp(now()) + 172800)|timestamp_custom('%A-%Y-%m-%d') }}"
      value_template: '{{states.sensor.fruehstueckpluszwei.attributes["anzahl_erwachsene"] + states.sensor.fruehstueckpluszwei.attributes["anzahl_kind1"] }}'

Rest Sensors

  - platform: rest
    name: fruehstueckheute
    #resource: http://192.168.5.195:8089/api/mealplancount/2020-07-05
    resource_template: http://192.168.5.195:8089/api/mealplancount/{{ now().strftime('%Y-%m-%d') }}
    scan_interval: 499
    #value_template: "{{ value | replace(- - '':', '') }}"
    #value_template: "{{ value | replace('- -', '') }}"
    headers:
     User-Agent: homeassistant
     Accept: application/ld+json
    json_attributes:
     - "anzahl_erwachsene"
     - "anzahl_kind1"
     - "anzahl_kind2"

  - platform: rest
    name: fruehstueckmorgen
    #resource: http://192.168.5.195:8089/api/mealplancount/2020-07-05
    resource_template: http://192.168.5.195:8089/api/mealplancount/{{ (as_timestamp(now()) + 86400)|timestamp_custom('%Y-%m-%d') }}
    scan_interval: 480
    #value_template: "{{ value | replace(- - '':', '') }}"
    #value_template: "{{ value | replace('- -', '') }}"
    headers:
     User-Agent: homeassistant
     Accept: application/ld+json
    json_attributes:
     - "anzahl_erwachsene"
     - "anzahl_kind1"
     - "anzahl_kind2"

  - platform: rest
    name: fruehstueckpluszwei
    #resource: http://192.168.5.195:8089/api/mealplancount/2020-07-05
    resource_template: http://192.168.5.195:8089/api/mealplancount/{{ (as_timestamp(now()) + 172800)|timestamp_custom('%Y-%m-%d') }}
    scan_interval: 420
    #value_template: "{{ value | replace(- - '':', '') }}"
    #value_template: "{{ value | replace('- -', '') }}"
    headers:
     User-Agent: homeassistant
     Accept: application/ld+json
    json_attributes:
     - "anzahl_erwachsene"
     - "anzahl_kind1"
     - "anzahl_kind2"

That works perfect for the day of creation or restart. But after midnight everything gets messed up.

e.g day on template fruestueckpluszwei stays on Monday and calculates the initial Monday value +1.

The rest api does return the correct values.

If I restart home assistant it reloads the correct days and values. Strange behaviour I have to admint

Please see result set for restart 05.07.2020. I will post the wrong picture tomorrow morning

I wonder if the template sensors aren’t automatically picking up the updates to the rest sensors? Try helping them by supplying the entity_id (docs):

 - platform: template
    sensors:
     hotelfruehstueckheute:
      friendly_name: 'Anzahl Fruehstücker Heute'
      entity_id: sensor.fruehstueckheute
      value_template: '{{states.sensor.fruehstueckheute.attributes["anzahl_erwachsene"] + states.sensor.fruehstueckheute.attributes["anzahl_kind1"] }}'

and similarly for the other two sensors.

2 Likes

I’ll try right not. TY

ty troon ty tom ! now everything works as expected ! my heroes

1 Like

Free breakfast when we show up at your hotel then :grin:

be invited ! also for lunch (restaurant currently closed due to corona :frowning: )

HI,

since the last update the sensors arent working anymore. Does anyone know if they have changed anything ?

 - platform: template
    sensors:
     hotelfruehstueckheute:
      entity_id: sensor.fruehstueckheute
      friendly_name: 'Anzahl Fruehstücker Heute'
      #value_template: '{{states.sensor.fruehstueckheute.attributes["anzahl_erwachsene"] + states.sensor.fruehstueckheute.attributes["anzahl_kind1"] }}'
      value_template: '{{states.sensor.fruehstueckheute.attributes["fruehstuecker"] }}'

  - platform: rest
    name: fruehstueckheute
    #resource: https://api/mealplancount/2020-07-05
    resource_template: https://api/mealplancount/{{ now().strftime('%Y-%m-%d') }}
    scan_interval: 499
    #value_template: "{{ value | replace(- - '':', '') }}"
    #value_template: "{{ value | replace('- -', '') }}"
    headers:
     User-Agent: homeassistant
     Accept: application/ld+json
    json_attributes:
     - "fruehstuecker"



may it be because the duckdns certificate seems to be too old and is rejected ? strange …

SORRY CLOSED

It was the expired SSL certificate in my nginx