Advertisement
DevilingMaster

HA - Distributore economico BENZINA

Dec 20th, 2024 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 5.34 KB | None | 0 0
  1. rest:
  2.  #############################################################
  3.   ###### FROM SEARCH ENGINE RETRIEVE CHEAPEST STATION ID ######
  4.   #############################################################
  5.  
  6.   - resource: https://carburanti.mise.gov.it/ospzApi/search/zone
  7.     method: POST
  8.     payload_template: >
  9.      {
  10.         "points": [
  11.           {
  12.             "lat": {{ state_attr("zone.home", "latitude") }},
  13.             "lng": {{ state_attr("zone.home", "longitude") }}
  14.           }
  15.         ],
  16.         "radius": 10,
  17.         "fuelType": "1-x",
  18.         "priceOrder": "asc"
  19.       }
  20.     headers:
  21.       Content-Type: application/json
  22.     scan_interval: 3600
  23.     timeout: 60
  24.  
  25.     sensor:
  26.       - name: distributore_economico_benzina_id
  27.         unique_id: distributore_economico_benzina_id
  28.         value_template: >
  29.          {% set first_result = value_json.results | first() %}
  30.           {% if first_result %}
  31.             {{ first_result.id }}
  32.           {% else %}
  33.             unknown
  34.           {% endif %}
  35.  
  36.   ###############################################
  37.   ###### FROM STATION ID RETRIEVE METADATA ######
  38.   ###############################################
  39.  
  40.   - resource_template: "https://carburanti.mise.gov.it/ospzApi/registry/servicearea/{{ states('sensor.distributore_economico_benzina_id') }}"
  41.     scan_interval: 86400
  42.     timeout: 60
  43.  
  44.     sensor:
  45.       - name: distributore_economico_benzina_nome
  46.         unique_id: distributore_economico_benzina_nome
  47.         value_template: "{{ value_json.name }}"
  48.         availability: "{{ states('sensor.distributore_economico_benzina_id') not in ('unknown', 'unavailable', 'none') }}"
  49.  
  50.       - name: distributore_economico_benzina_marchio
  51.         unique_id: distributore_economico_benzina_marchio
  52.         value_template: "{{ value_json.brand }}"
  53.         availability: "{{ states('sensor.distributore_economico_benzina_id') not in ('unknown', 'unavailable', 'none') }}"
  54.  
  55.       - name: distributore_economico_benzina_indirizzo
  56.         unique_id: distributore_economico_benzina_indirizzo
  57.         value_template: "{{ value_json.address }}"
  58.         icon: "mdi:map-marker"
  59.         availability: "{{ states('sensor.distributore_economico_benzina_id') not in ('unknown', 'unavailable', 'none') }}"
  60.  
  61.       - name: distributore_economico_benzina_prezzo
  62.         unique_id: distributore_economico_benzina_prezzo
  63.         value_template: >
  64.          {% set benzina_self = value_json.fuels | selectattr('name', 'equalto', 'Benzina') | selectattr('isSelf', 'equalto', true) | first() %}
  65.           {% set benzina_servito = value_json.fuels | selectattr('name', 'equalto', 'Benzina') | selectattr('isSelf', 'equalto', false) | first() %}
  66.           {% if benzina_self %}
  67.             {{ benzina_self.price | float(0) }}
  68.           {% elif benzina_servito %}
  69.             {{ benzina_servito.price | float(0) }}
  70.           {% else %}
  71.             unknown
  72.           {% endif %}
  73.         icon: "mdi:gas-station-in-use"
  74.         unit_of_measurement: "€"
  75.         device_class: "monetary"
  76.         availability: "{{ states('sensor.distributore_economico_benzina_id') not in ('unknown', 'unavailable', 'none') }}"
  77.  
  78.       - name: distributore_economico_benzina_aggiornamento
  79.         unique_id: distributore_economico_benzina_aggiornamento
  80.         value_template: >
  81.          {% set benzina_self = value_json.fuels | selectattr('name', 'equalto', 'Benzina') | selectattr('isSelf', 'equalto', true) | first() %}
  82.           {% set benzina_servito = value_json.fuels | selectattr('name', 'equalto', 'Benzina') | selectattr('isSelf', 'equalto', false) | first() %}
  83.           {% if benzina_self %}
  84.             {{ benzina_self.insertDate }}
  85.           {% elif benzina_servito %}
  86.             {{ benzina_servito.insertDate }}
  87.           {% else %}
  88.             unknown
  89.           {% endif %}
  90.         icon: "mdi:update"
  91.         device_class: "timestamp"
  92.         availability: "{{ states('sensor.distributore_economico_benzina_id') not in ('unknown', 'unavailable', 'none') }}"
  93.  
  94. #########################################################
  95. ###### WHEN ID CHANGES, NEW METADATA ARE RETRIEVED ######
  96. #########################################################
  97.  
  98. automation:
  99.   - id: distributore_economico_benzina_on_id_change
  100.     alias: distributore_economico_benzina_on_id_change
  101.     description: "When the most economic station changes, station metadata are downloaded"
  102.     trigger:
  103.       - platform: state
  104.         entity_id: sensor.distributore_economico_benzina_id
  105.     action:
  106.       - service: homeassistant.update_entity
  107.         target:
  108.           entity_id: sensor.distributore_economico_benzina_marchio # All metadata depends on the same rest sensor, one entity is enough to trigger the update on all
  109.         data: {}
  110.  
  111.   - id: distributore_economico_benzina_on_ha_load
  112.     alias: distributore_economico_benzina_on_ha_load
  113.     description: "Due to race-condition, during startup when station ID is loaded, the automation to fetch metadata is not triggered. For this particular case we need to trigger it manually"
  114.     trigger:
  115.       - platform: homeassistant
  116.         event: start
  117.     action:
  118.       - delay: "00:00:10"
  119.       - service: homeassistant.update_entity
  120.         target:
  121.           entity_id: sensor.distributore_economico_benzina_marchio
  122.         data: {}
  123.  
  124. #recorder:
  125. #  include:
  126. #    entities:
  127. #      - sensor.distributore_economico_benzina_prezzo
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement