danieldotnl / ha-multiscrape

Home Assistant custom component for scraping (html, xml or json) multiple values (from a single HTTP request) with a separate sensor/attribute for each value. Support for (login) form-submit functionality.
MIT License
293 stars 16 forks source link

False positive state change triggers automation #437

Open parautenbach opened 1 month ago

parautenbach commented 1 month ago

Version of the custom_component

8.0.2

Configuration

multiscrape:
  - resource: https://pollencount.co.za/
    scan_interval: 21600  # 60*60*6 or 4 updates per day
    timeout: 30
    sensor:
      - name: Overall Pollen Risk
        icon: mdi:flower-pollen-outline
        select: "#wpv-view-layout-300 > div > div > div.image-resize > div:nth-child(2) > div:nth-child(2) > div"
        attribute: "class"
        value_template: >-
          {# set value = ["pollen-darkorange"] #}
          {%- from 'pollen.jinja' import color_level_map -%}
          {%- set key = value | first -%}
          {{ color_level_map[key] if key in color_level_map else "unknown" }}
        on_error:
          value: last
        attributes:
          - name: Label
            select: "#wpv-view-layout-300 > div > div > div.image-resize > div:nth-child(2) > div:nth-child(2) > div"
            attribute: "class"
            value_template: >-
              {%- from 'pollen.jinja' import color_label_map -%}
              {%- set key = value | first -%}
              {{ color_label_map[key] if key in color_label_map else "Unavailable" }}
            on_error:
              value: last

Automation:

automation:
  - alias: "Pollen Levels Update"
    id: "eeb7f4a7-367d-4aee-b4d5-cf84da3b1323"
    initial_state: true
    trigger:
      # If only entity_id is given, the trigger will fire for all state changes, even if only state attributes change.
      # https://www.home-assistant.io/docs/automation/trigger/#state-trigger
      - platform: state
        entity_id:
          - sensor.overall_pollen_risk
          - sensor.tree_pollen
          - sensor.grass_pollen
          - sensor.weed_pollen
          - sensor.mould_spores
    mode: single
    action:
      - service: notify.family
        data:
          title: "⛑️ Health"
          # https://community.home-assistant.io/t/trigger-to-from-attribute/325345
          # https://community.home-assistant.io/t/access-attribute-of-from-state/380497
          message: >-
            {%- macro build_item(sensor, name) -%}
              {%- set attribute = sensor.split('.')[1] -%}
              {%- set from = trigger.from_state.attributes[attribute] | lower -%}
              {%- set to = trigger.to_state.attributes[attribute] | lower -%}
              {%- if from != to and (from != 'unavailable' or to != 'unavailable') -%}
                {{ name }}: {{ from }} → {{ to }}
              {%- else -%}
                {{ name }}: {{ to }}
              {%- endif -%}
            {%- endmacro -%}
            {%- set sensors = [('sensor.tree_pollen', 'Trees'), ('sensor.grass_pollen', 'Grasses'), ('sensor.weed_pollen', 'Weeds'), ('sensor.mould_spores', 'Mould')] -%}
            {%- set summary = '' -%}
            {%- set from_state_label = trigger.from_state.attributes.label | lower -%}
            {%- set to_state_label = trigger.to_state.attributes.label | lower -%}
            {%- if from_state_label != to_state_label -%}
              {%- set summary = 'Pollen levels changed from ' ~ from_state_label ~ ' to ' ~ to_state_label ~ '.' -%}
            {%- else -%}
              {%- set summary = 'Pollen levels are ' ~ to_state_label ~ '.' -%}
            {%- endif -%}
            {{ summary }}
            {%- for (sensor, name) in sensors %}
              - {{ build_item(sensor, name) }}
            {%- endfor %}
          data:
            group: "environment"
            url: homeassistant://navigate/lovelace/environment

Describe the bug

When I (res)start HA, I get a false positive notification from my automation. Given my automation config, it must be triggering on an attribute change, not the state.

Debug log

Automation trace info for the trigger:

this:
  entity_id: automation.pollen_levels_update
  state: 'on'
  attributes:
    id: eeb7f4a7-367d-4aee-b4d5-cf84da3b1323
    last_triggered: '2024-10-06T11:45:09.215926+00:00'
    mode: single
    current: 0
    friendly_name: Pollen Levels Update
  last_changed: '2024-10-07T15:54:37.348863+00:00'
  last_reported: '2024-10-07T15:54:37.348863+00:00'
  last_updated: '2024-10-07T15:54:37.348863+00:00'
  context:
    id: 01J9KV13X4HTPMYTBVH2MFABGM
    parent_id: null
    user_id: null
trigger:
  id: '0'
  idx: '0'
  alias: null
  platform: state
  entity_id: sensor.overall_pollen_risk
  from_state:
    entity_id: sensor.overall_pollen_risk
    state: '2'
    attributes:
      label: Low
      tree_pollen: Low
      grass_pollen: Very Low
      weed_pollen: Very Low
      mould_spores: Very Low
      friendly_name: Overall Pollen Risk
      unit_of_measurement: index
    last_changed: '2024-10-07T15:54:27.458505+00:00'
    last_reported: '2024-10-07T15:54:27.458505+00:00'
    last_updated: '2024-10-07T15:54:27.458505+00:00'
    context:
      id: 01J9KV0T8262FWVYS3RFCWNBZY
      parent_id: null
      user_id: null
  to_state:
    entity_id: sensor.overall_pollen_risk
    state: '2'
    attributes:
      label: Low
      tree_pollen: Low
      grass_pollen: Very Low
      weed_pollen: Very Low
      mould_spores: Very Low
      icon: mdi:flower-pollen-outline  # <---------- this seems to be the only difference, except for the last_* values
      friendly_name: Overall Pollen Risk
      unit_of_measurement: index
    last_changed: '2024-10-07T15:54:27.458505+00:00'
    last_reported: '2024-10-07T15:54:56.620638+00:00'
    last_updated: '2024-10-07T15:54:56.620638+00:00'
    context:
      id: 01J9KV1PQC2M6XE5Z1V02DZ0DR
      parent_id: null
      user_id: null
  for: null
  attribute: null
  description: state of sensor.overall_pollen_risk
danieldotnl commented 1 month ago

Was this introduced in the latest release?

parautenbach commented 1 month ago

HA or Multiscrape release?

I upgraded HA from 2024.9.1 (or was it 2024.9.3) to 2024.10.1.

I don't remember the previous multiscrape version I upgraded from, but I generally update all my integrations when I upgrade HA.

I didn't notice this issue before, as in, I didn't previously have my pollen automation run soon after an HA restart. Perhaps this always happened, but something in the start-up order or order of async events is causing this. I cannot really say.

Broekman commented 1 week ago

I have the exact same issue which started happening from the latest release v8.0.2. On startup of HA a change of state is triggered while the state is identical to before restarting. The sensor has no attributes. Happens for all sensors, seems related to https://github.com/danieldotnl/ha-multiscrape/pull/417/commits/06cdc8a7ad3a1ee9fa9f5e911b3cea9c8bd87330