More attributes in a sensor. How to?

I have this template that shows the history events on a day:

- platform: template
  sensors:
    storia_italia:
      value_template: "{{ states.calendar.storia_ditalia.attributes.message }}"
      friendly_name: "Accadde Oggi"

But i noticed it shows only one events also if there are more than one.
The calendar.storia_ditalia entity is this:

Schermata 2020-04-16 alle 11.48.08

So the template is right, but how to see in the calendar all the events listed in my google calendar?

I have this in google_calendars.yaml about that calendar:

- cal_id: [email protected]
  entities:
  - device_id: storia_ditalia
    ignore_availability: true
    name: "Storia d'Italia"
    track: true
    max_results: 30

How to have the calendar shows all the events in that day?

This hass object only contains 1 event. You have to ask the API if you want more.

Thanks… how to ask API?

This is what I used to get events from API in Python.

from requests import get
import json

ha_url = 'http://192.168.1.X:8123'
calendar = "calendar.contacts"
start_date = datetime.strftime(datetime.now() - timedelta(1), '%Y-%m-%dT00:00:00')
end_date = datetime.strftime(datetime.now() + timedelta(1), '%Y-%m-%dT00:00:00')
token = '123456'
apiurl = "{}/api/calendars/{}?start={}Z&end={}Z".format(ha_url,calendar,start_date,end_date)
headers = {'Authorization': "Bearer {}".format(token), 'content-type': 'application/json', 'charset': 'utf-8'}

r = get(apiurl, headers=headers, verify=False)
list = json.loads(r.text)
print(list)

Hmmm… thanks but it’s too difficult for me…

Where do you want to “see” them?

Simply listed between the events of the day… in a calendar card.
actually i am using atomic-calendar-revive card and using this sensor:

- platform: template
  sensors:
    storia_italia:
      value_template: "{{ states.calendar.storia_ditalia.attributes.message }}"
      friendly_name: "Accadde Oggi"

Then I’m not sure what the problem is.

I use the same calendar card and it will show all events, even more than one event in a day.

The “calendar card” downloads all the events from the API.
While it’s only possible to get the first event from the attributes.

yeah, I get that.

But the OP only wanted to display the other events in the calendar card. But it should already be doing that (mine does). So the right answer should be to investigate why the calendar card isn’t working as expected as opposed to trying to create another sensor for every every event of the calendar.

I believe that @maurizio53 wants to access all calendar events from template platform (through calendar attributes).

Maybe i was not so clear due to my poor english… in the atomica-calendar-revice card i get all the events but only in defaultmode 2 as in the calendar screen (the one with all days of the months listed) clicking on a day i don’t get the events from that calendar…
About the sensor i know it is ok as the attributes of a calendar offer only the first event and i was asking how to get a sensor which will show as attributes all the events…

Yes, you’re right and i am aware of this, but i was asking if it could be possible to get a sensor with all events from the API as attributes.

Yes you got it !!! :ok_hand: :pray:

Hi, is this a python script? or custom_component?

it’s a custom script that’s ran on something outside of HA. It cannot run in HA.

I use a RESTful sensor that reads my Google calendar.

sensor:
  - platform: rest
    name: Anniversaires
    authentication: basic
    headers: {'Authorization': "Bearer XXX"}
    scan_interval: 60
    resource_template: "http://192.168.1.95:8124/api/calendars/calendar.anniversaires?start={{ now().strftime('%Y-%m-%d') }}T00:00:00Z&end={{ (as_timestamp(now()) + (7*24*3600)) | timestamp_custom('%Y-%m-%d', True) }}T00:00:00Z"
    value_template: '[{%- for bd in value_json -%}{%- if not loop.first %},{% endif -%}{"event": "{{ bd.summary }}", "date": "{{ bd.start.date }}"}{%- endfor -%}]'
1 Like

Thanks, but a strange thing happens.
In models

{{ now().strftime('%Y-%m-%d') }}T00:00:00Z 

return correct date 2022-01-12T00:00:00Z

but in sensor

start: 2022-01-11T00:00:00+01:00

it’s normal?

What sensor are you talking about ?

my sensor.anniversaires

[{“event”: “Jean - Anniversaire”, “date”: “2022-01-17”}]

Unfortunately I get error in HA when I have too many events.

Logger: homeassistant
Source: core.py:912 
First occurred: 19:54:07 (13 occurrences) 
Last logged: 20:01:09

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 734, in _update_entity_states
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 492, in async_update_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 629, in _async_write_ha_state
    self.hass.states.async_set(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1222, in async_set
    state = State(
  File "/usr/src/homeassistant/homeassistant/core.py", line 912, in __init__
    raise InvalidStateError(
homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity ID: sensor.lavoro. State max length is 255 characters.