bruxy70 / Garbage-Collection

🗑 Custom Home Assistant sensor for scheduling garbage collection (or other regularly re-occurring events - weekly on given days, semi-weekly or monthly)
MIT License
382 stars 90 forks source link

Dealing with unknown date for sensor #391

Closed pdobrien3 closed 2 years ago

pdobrien3 commented 2 years ago

Before you submit a new bug report, please check that

Write your question here

I use the companion app actionable notifications to send me a notification on friday and saturday to see if the yard was mowed and there are grass clippings that need to go out. I haven't quite figured out how this custom component works and how to deal with a sensor on home assistant restart.

This is my current sensor:

- trigger:
    - platform: event
      event_type: mobile_app_notification_action
      event_data:
        action: UPDATE GARBAGE INTEGRATION
  sensor:
    - name: 'grass trigger sensor'
      state: >-
        {% if now().isoweekday() == 5 %}  
          {{(as_timestamp(now()) + (48*3600)) | timestamp_custom('%Y-%m-%d')}} 
        {% elif now().isoweekday() == 6 %} 
          {{(as_timestamp(now()) + (24*3600)) | timestamp_custom('%Y-%m-%d')}} 
        {% else %}
          {{(as_timestamp(now()) | timestamp_custom('%Y-%m-%d')) }}          
        {% endif %}  

This is my automation:

- id: 'u2'
  alias: Update Grass Garbage Integration
  trigger:
    platform: event
    event_type: garbage_collection_loaded
    event_data:
      entity_id: sensor.grass
  action:
    - service: garbage_collection.add_date
      data:
        entity_id: '{{ trigger.event.data.entity_id }}'
        date: "{{ states('sensor.grass_trigger_sensor') }}"
    - service: garbage_collection.update_state
      data:
        entity_id: sensor.grass
  mode: single

At some point during the week, sensor.grass gets a state of null. I have tried adding:

  - platform: homeassistant
  - event: start

as a trigger for sensor.grass_trigger_sensor but then it adds the current date for every home assistant restart. otherwise I get these errors at startup:

Logger: homeassistant.components.automation.update_grass_garbage_integration
Source: helpers/script.py:1397
Integration: Automation ([documentation](https://www.home-assistant.io/integrations/automation), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+automation%22))
First occurred: 6:31:43 AM (18 occurrences)
Last logged: 6:34:33 AM

Update Grass Garbage Integration: Error executing script. Invalid data for call_service at pos 1: Could not parse date for dictionary value @ data['date']
Logger: homeassistant.components.automation.update_grass_garbage_integration
Source: components/automation/__init__.py:519
Integration: Automation ([documentation](https://www.home-assistant.io/integrations/automation), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+automation%22))
First occurred: 6:31:43 AM (18 occurrences)
Last logged: 6:34:33 AM

Error while executing automation automation.update_grass_garbage_integration: Could not parse date for dictionary value @ data['date']

What is the best way to deal with a null date in sensor.grass_trigger_sensor?

bruxy70 commented 2 years ago

Suppose add a condition that the sensor state must not be Null?

Do I get it right you get a notification on your phone asking whether you want to update garbage collection. If you say yes, it will then trigger the sensor update with the current day (or in two days or tomorrow if it is Friday or Saturday). So it will say garbage collection is today. And after the day is finished it it will show None as there will not be any next collection date defined? Not sure I understand this use-case. But if this is how you want this to work, then ok.

pdobrien3 commented 2 years ago

Yes, you hit the nail on the head. So a condition on the automation that the template sensor is not null should work. I actually got my first working date this Saturday so I am waiting to determine the state of all sensors involved tomorrow.

pdobrien3 commented 2 years ago

Things seem to have stabilized. No idea what changes fixed things. I think trigger based template sensors maintaining state and some of the code stuff you have done. Also, actually getting a valid date into the calendar. Anyway, I think all good at this stage. Thanks