pinkywafer / Calendarific

Calendarific holiday sensor for Home Assistant
MIT License
19 stars 7 forks source link

Add days remaining as an attribute to allow easier filtering #13

Closed lonebaggie closed 4 years ago

lonebaggie commented 4 years ago

Would it be possible to an extra unique attribute to the sensor say no_of_days set the same as the state value . This will allow easy filtering with love lace cards , so I can popup an alert with https://github.com/thomasloven/lovelace-auto-entities. Would save me having to make lots of template sensors

pinkywafer commented 4 years ago

I haven't had chance to implement this yet, but see #2 - when implemeted, each sensor would have the prefix calendarific_ so that could be used for easy filtering. (I shall probably make this a customiseable prefix) - Though it should be noted that this can only work during the set up of the sensor, not afterwards, however, the best step in the short term would be to rename each sensor through the front end. Change the sensor name to sensor.calendarific_holiday for example and then you can filter based on calendarific_* I cannot see the need to add an attribute that is the same as the state just for filtering when filtering on sensor name would be more useful.

Please let me know your thoughts

lonebaggie commented 4 years ago

Thankyou for taking time to look at this ;)

I amended the code to add the attribute to test :-

ATTR_DESCRIPTION = "description"
ATTR_DATE = "date"
ATTR_NOD = "no_of_days"

@property 
    def device_state_attributes(self):
        """Return the state attributes."""
        res = {}
        res[ATTR_DATE] = self._attr_date
        res[ATTR_DESCRIPTION] = self._description
        res[ATTR_NOD] = self._state
        return res

The reson it simpler to test the attribute which is currently unique with a value . So to highlight any upcoming holidays I simply test if the value of no_of_days is below a threshold in lovelace-auto-entities. I cannot filter on state as it is not unique and any other visable attribute will not let me know when the hoiliday is due.

filter:
  include:
    - attributes:
        no_of_days: < 8

If I filtered on the name or friendly name . I would still have to test if the holiday is due. Im happy to keep amending your code to add this attribute., if you feel this is unnessecery.

Again thank you again for looking at this

pinkywafer commented 4 years ago

Ok. I'm not going to add this additional attribute as it is only repeating the state.

I cannot guarantee that another integration used will not also implement a no_of_days attribute, and if any does, the filter would fail.

The way around this would be to rename the entity_id of your calendarific sensors to calendarific_sensor_name as planned in #2, then use as:

filter:
  include:
    - entity_id: "*calendarific*"
  exclude:
    - state: >7

However another attribute which could be used to identify the component would be #14 (attribution)

lonebaggie commented 4 years ago

Thank you the above is a much better solution and does not require additional attributes