Advertisement
DevilingMaster

HA - Distributore economico DIESEL

Dec 20th, 2024 (edited)
198
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": "2-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_diesel_id
  27.         unique_id: distributore_economico_diesel_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_diesel_id') }}"
  41.     scan_interval: 86400
  42.     timeout: 60
  43.  
  44.     sensor:
  45.       - name: distributore_economico_diesel_nome
  46.         unique_id: distributore_economico_diesel_nome
  47.         value_template: "{{ value_json.name }}"
  48.         availability: "{{ states('sensor.distributore_economico_diesel_id') not in ('unknown', 'unavailable', 'none') }}"
  49.  
  50.       - name: distributore_economico_diesel_marchio
  51.         unique_id: distributore_economico_diesel_marchio
  52.         value_template: "{{ value_json.brand }}"
  53.         availability: "{{ states('sensor.distributore_economico_diesel_id') not in ('unknown', 'unavailable', 'none') }}"
  54.  
  55.       - name: distributore_economico_diesel_indirizzo
  56.         unique_id: distributore_economico_diesel_indirizzo
  57.         value_template: "{{ value_json.address }}"
  58.         icon: "mdi:map-marker"
  59.         availability: "{{ states('sensor.distributore_economico_diesel_id') not in ('unknown', 'unavailable', 'none') }}"
  60.  
  61.       - name: distributore_economico_diesel_prezzo
  62.         unique_id: distributore_economico_diesel_prezzo
  63.         value_template: >
  64.          {% set diesel_self = value_json.fuels | selectattr('name', 'equalto', 'Gasolio') | selectattr('isSelf', 'equalto', true) | first() %}
  65.           {% set diesel_servito = value_json.fuels | selectattr('name', 'equalto', 'Gasolio') | selectattr('isSelf', 'equalto', false) | first() %}
  66.           {% if diesel_self %}
  67.             {{ diesel_self.price | float(0) }}
  68.           {% elif diesel_servito %}
  69.             {{ diesel_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_diesel_id') not in ('unknown', 'unavailable', 'none') }}"
  77.  
  78.       - name: distributore_economico_diesel_aggiornamento
  79.         unique_id: distributore_economico_diesel_aggiornamento
  80.         value_template: >
  81.          {% set diesel_self = value_json.fuels | selectattr('name', 'equalto', 'Gasolio') | selectattr('isSelf', 'equalto', true) | first() %}
  82.            {% set diesel_servito = value_json.fuels | selectattr('name', 'equalto', 'Gasolio') | selectattr('isSelf', 'equalto', false) | first() %}
  83.            {% if diesel_self %}
  84.              {{ diesel_self.insertDate }}
  85.            {% elif diesel_servito %}
  86.              {{ diesel_servito.insertDate }}
  87.            {% else %}
  88.              unknown
  89.            {% endif %}
  90.         icon: "mdi:update"
  91.         device_class: "timestamp"
  92.         availability: "{{ states('sensor.distributore_economico_diesel_id') not in ('unknown', 'unavailable', 'none') }}"
  93.  
  94. #########################################################
  95. ###### WHEN ID CHANGES, NEW METADATA ARE RETRIEVED ######
  96. #########################################################
  97.  
  98. automation:
  99.   - id: distributore_economico_diesel_on_id_change
  100.     alias: distributore_economico_diesel_on_id_change
  101.     description: "When the most economic station changes, station metadata are downloaded"
  102.     trigger:
  103.       - platform: state
  104.         entity_id:
  105.          - sensor.distributore_economico_diesel_id
  106.     condition: []
  107.     action:
  108.       - service: homeassistant.update_entity
  109.         target:
  110.           entity_id: sensor.distributore_economico_diesel_marchio # All metadata depends on the same rest sensor, one entity is enough to trigger the update on all
  111.         data: {}
  112.  
  113.   - id: distributore_economico_diesel_on_ha_load
  114.     alias: distributore_economico_diesel_on_ha_load
  115.     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"
  116.     trigger:
  117.       - platform: homeassistant
  118.         event: start
  119.     action:
  120.       - delay: "00:00:10"
  121.       - service: homeassistant.update_entity
  122.         target:
  123.           entity_id: sensor.distributore_economico_diesel_marchio
  124.         data: {}
  125.  
  126. #recorder:
  127. #  include:
  128. #    entities:
  129. #      - sensor.distributore_economico_diesel_prezzo
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement