Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rest:
- #############################################################
- ###### FROM SEARCH ENGINE RETRIEVE CHEAPEST STATION ID ######
- #############################################################
- - resource: https://carburanti.mise.gov.it/ospzApi/search/zone
- method: POST
- payload_template: >
- {
- "points": [
- {
- "lat": {{ state_attr("zone.home", "latitude") }},
- "lng": {{ state_attr("zone.home", "longitude") }}
- }
- ],
- "radius": 10,
- "fuelType": "1-x",
- "priceOrder": "asc"
- }
- headers:
- Content-Type: application/json
- scan_interval: 3600
- timeout: 60
- sensor:
- - name: distributore_economico_benzina_id
- unique_id: distributore_economico_benzina_id
- value_template: >
- {% set first_result = value_json.results | first() %}
- {% if first_result %}
- {{ first_result.id }}
- {% else %}
- unknown
- {% endif %}
- ###############################################
- ###### FROM STATION ID RETRIEVE METADATA ######
- ###############################################
- - resource_template: "https://carburanti.mise.gov.it/ospzApi/registry/servicearea/{{ states('sensor.distributore_economico_benzina_id') }}"
- scan_interval: 86400
- timeout: 60
- sensor:
- - name: distributore_economico_benzina_nome
- unique_id: distributore_economico_benzina_nome
- value_template: "{{ value_json.name }}"
- availability: "{{ states('sensor.distributore_economico_benzina_id') not in ('unknown', 'unavailable', 'none') }}"
- - name: distributore_economico_benzina_marchio
- unique_id: distributore_economico_benzina_marchio
- value_template: "{{ value_json.brand }}"
- availability: "{{ states('sensor.distributore_economico_benzina_id') not in ('unknown', 'unavailable', 'none') }}"
- - name: distributore_economico_benzina_indirizzo
- unique_id: distributore_economico_benzina_indirizzo
- value_template: "{{ value_json.address }}"
- icon: "mdi:map-marker"
- availability: "{{ states('sensor.distributore_economico_benzina_id') not in ('unknown', 'unavailable', 'none') }}"
- - name: distributore_economico_benzina_prezzo
- unique_id: distributore_economico_benzina_prezzo
- value_template: >
- {% set benzina_self = value_json.fuels | selectattr('name', 'equalto', 'Benzina') | selectattr('isSelf', 'equalto', true) | first() %}
- {% set benzina_servito = value_json.fuels | selectattr('name', 'equalto', 'Benzina') | selectattr('isSelf', 'equalto', false) | first() %}
- {% if benzina_self %}
- {{ benzina_self.price | float(0) }}
- {% elif benzina_servito %}
- {{ benzina_servito.price | float(0) }}
- {% else %}
- unknown
- {% endif %}
- icon: "mdi:gas-station-in-use"
- unit_of_measurement: "€"
- device_class: "monetary"
- availability: "{{ states('sensor.distributore_economico_benzina_id') not in ('unknown', 'unavailable', 'none') }}"
- - name: distributore_economico_benzina_aggiornamento
- unique_id: distributore_economico_benzina_aggiornamento
- value_template: >
- {% set benzina_self = value_json.fuels | selectattr('name', 'equalto', 'Benzina') | selectattr('isSelf', 'equalto', true) | first() %}
- {% set benzina_servito = value_json.fuels | selectattr('name', 'equalto', 'Benzina') | selectattr('isSelf', 'equalto', false) | first() %}
- {% if benzina_self %}
- {{ benzina_self.insertDate }}
- {% elif benzina_servito %}
- {{ benzina_servito.insertDate }}
- {% else %}
- unknown
- {% endif %}
- icon: "mdi:update"
- device_class: "timestamp"
- availability: "{{ states('sensor.distributore_economico_benzina_id') not in ('unknown', 'unavailable', 'none') }}"
- #########################################################
- ###### WHEN ID CHANGES, NEW METADATA ARE RETRIEVED ######
- #########################################################
- automation:
- - id: distributore_economico_benzina_on_id_change
- alias: distributore_economico_benzina_on_id_change
- description: "When the most economic station changes, station metadata are downloaded"
- trigger:
- - platform: state
- entity_id: sensor.distributore_economico_benzina_id
- action:
- - service: homeassistant.update_entity
- target:
- 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
- data: {}
- - id: distributore_economico_benzina_on_ha_load
- alias: distributore_economico_benzina_on_ha_load
- 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"
- trigger:
- - platform: homeassistant
- event: start
- action:
- - delay: "00:00:10"
- - service: homeassistant.update_entity
- target:
- entity_id: sensor.distributore_economico_benzina_marchio
- data: {}
- #recorder:
- # include:
- # entities:
- # - sensor.distributore_economico_benzina_prezzo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement