custom-components / weatheralerts

A sensor that gives you weather alerts from alerts.weather.gov.
MIT License
123 stars 15 forks source link

input_text limited to 255 Characters so won't Hold Five alert IDs #56

Open kaijk opened 2 years ago

kaijk commented 2 years ago

Version, Current, V1.3? (Note, I renamed entities to the 'motherlode' zone from 'weatheralerts'

Bug Alert IDs are stored in an input_text helper. These are limited to 255 characters total, but the total concatenated string for five alerts is 349 Characters. For example:

urn:oid:2.49.0.1.840.0.f9a6097f6d0e41b9224fd529f005effa05ff9757.001.1 
urn:oid:2.49.0.1.840.0.c6397b712c111522feb786d9bab7d545aa86668d.001.1 
urn:oid:2.49.0.1.840.0.9e404887452349de9b387fa6efa8d1a6ada8541a.002.1 
urn:oid:2.49.0.1.840.0.7f73c6a9ede216c8f21fd80675b4ad8cfd435f81.001.1 
urn:oid:2.49.0.1.840.0.0ad126d6a7c5c2d956b8e75693617d4dbb0cf112.001.1

Log

Logger: homeassistant.components.input_text
Source: components/input_text/__init__.py:274
Integration: Input text ([documentation](https://www.home-assistant.io/integrations/input_text), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+input_text%22))
First occurred: 1:29:37 PM (2 occurrences)
Last logged: 1:29:47 PM
Invalid value: urn:oid:2.49.0.1.840.0.f9a6097f6d0e41b9224fd529f005effa05ff9757.001.1 urn:oid:2.49.0.1.840.0.c6397b712c111522feb786d9bab7d545aa86668d.001.1 urn:oid:2.49.0.1.840.0.9e404887452349de9b387fa6efa8d1a6ada8541a.002.1 urn:oid:2.49.0.1.840.0.7f73c6a9ede216c8f21fd80675b4ad8cfd435f81.001.1 urn:oid:2.49.0.1.840.0.0ad126d6a7c5c2d956b8e75693617d4dbb0cf112.001.1 (length range 0 - 255)

Input_text definition:

input_text:
  motherlode_triggered_ui_alert_ids:
    name: Motherlode Triggered Weather Alert IDs - UI
    icon: mdi:information-variant
    max: 255
    initial: None

Input_text action from automation:

      - service: input_text.set_value
        data: #data_template:
          entity_id: input_text.motherlode_triggered_ui_alert_ids
          value: "{{ state_attr('sensor.motherlode_alert_1', 'alert_id') }} {{ state_attr('sensor.motherlode_alert_2', 'alert_id') }} {{ state_attr('sensor.motherlode_alert_3', 'alert_id') }} {{ state_attr('sensor.motherlode_alert_4', 'alert_id') }} {{ state_attr('sensor.motherlode_alert_5', 'alert_id') }}"
jlverhagen commented 2 years ago

Thanks for the report. The NWS changed the alert ID format at some point and made them much longer. I think I can fix it by adding a second input_text helper and splitting the alert IDs between two input_text helpers. It should only take some minor adjustments to the UI Notification automation.

kaijk commented 2 years ago

Thanks for your attention to this. We happen to have some residual/reappearing winter that is generating a flurry ;) of NWS alerts that make for good testing.

Might you use an attribute to store these data? Attributes are not bound by the 255 character limit for an entity's state .

mjnovotny82 commented 1 year ago

We could pipe in a replace() to remove urn:oid:2.49.0.1.840.0. from the alert IDs in your weatheralerts.yaml file. You would want to add | replace('urn:oid:2.49.0.1.840.0.','') to each of the five [0-4] alert_id sections . This should drop it down to around 234 characters.

Before

      alert_id: >-
        {% if not is_state('sensor.weatheralerts_1', '0') and not is_state('sensor.weatheralerts_1', 'unavailable') and not is_state('sensor.weatheralerts_1', 'unknown') and is_state('sensor.weatheralerts_1_alert_1', 'on') or (is_number(states('sensor.weatheralerts_1')) and (states('sensor.weatheralerts_1')|int > 0)) %}
          {{ states.sensor.weatheralerts_1.attributes.alerts[0].id }}
        {% else %}
          None
        {% endif %}
    # skip some lines #
      alert_id: >-
        {% if not is_state('sensor.weatheralerts_1', 'unavailable') and not is_state('sensor.weatheralerts_1', 'unknown') and is_state('sensor.weatheralerts_1_alert_2', 'on') or (is_number(states('sensor.weatheralerts_1')) and (states('sensor.weatheralerts_1')|int > 1)) %}
          {{ states.sensor.weatheralerts_1.attributes.alerts[1].id }}
        {% else %}
          None
        {% endif %}

After

      alert_id: >-
        {% if not is_state('sensor.weatheralerts_1', '0') and not is_state('sensor.weatheralerts_1', 'unavailable') and not is_state('sensor.weatheralerts_1', 'unknown') and is_state('sensor.weatheralerts_1_alert_1', 'on') or (is_number(states('sensor.weatheralerts_1')) and (states('sensor.weatheralerts_1')|int > 0)) %}
          {{ states.sensor.weatheralerts_1.attributes.alerts[0].id | replace('urn:oid:2.49.0.1.840.0.','') }}
        {% else %}
          None
        {% endif %}
    # skip some lines #
      alert_id: >-
        {% if not is_state('sensor.weatheralerts_1', 'unavailable') and not is_state('sensor.weatheralerts_1', 'unknown') and is_state('sensor.weatheralerts_1_alert_2', 'on') or (is_number(states('sensor.weatheralerts_1')) and (states('sensor.weatheralerts_1')|int > 1)) %}
          {{ states.sensor.weatheralerts_1.attributes.alerts[1].id | replace('urn:oid:2.49.0.1.840.0.','') }}
        {% else %}
          None
        {% endif %}
    # rinse and repeat for [2], [3], [4] #

I have not see urn:oid:2.49.0.1.840.0. changed in year plus of using weatheralerts.

kaijk commented 1 year ago

I'm trying your suggestion @mjnovotny82. We should be having a full compliment of alerts over the next few days.

I'd also considered using an attribute for the data instead of a state, but haven't pursued it.

regularguy01 commented 1 year ago

I fixed this by adding

.split('urn:oid:2.49.0.1.840.0.')[1],

on line 193 of sensor.py

the full line looks like "id": properties.get("id", "null").split('urn:oid:2.49.0.1.840.0.')[1],

The URN (universal registered number) never changes for NWS and is specific to the USA.

Then you don't need yaml